/*--------------------------------------------------------------
Rentopian Sync Frontend Scripts
Author: Rentopian
Website: https://rentopian.com
Copyright 2019, Rentopian Inc. All Rights Reserved.
----------------------------------------------------------------*/

jQuery(document).ready(function($) {

  // set customer input full address in cart page
  $.ajax({
    type: 'GET',
    url: rentalObj.url,
    data: {
      action: 'rental_replace_full_address_in_checkout',
    },
    success: function(data) {
    },
    error: function(data) {
      console.log(data);
    }, complete: function(datas) {
      var $totals_column = $(".woocommerce-shipping-totals td");
      var $fullAddressPlaceHolder = $totals_column.find("strong");
      var $fullAddressDestinationPlaceHolder = $totals_column.find(".woocommerce-shipping-destination");
      var wcCalc = $(".woocommerce-shipping-calculator");

      var data = datas.responseJSON;
      if (data !== undefined && data) {
        var theZip = '';

        // priority with full delivery address input
        // if (data.user_logged_in === 0) {

          if (data.address === '') {
            // var address_parts = JSON.parse(getCookie('rental_google_map_address'));
            // var city, state, country, zip, address;
            
            // for(var i in address_parts) {
            //   if (address_parts[i]['key'] === 'city') {
            //     city = address_parts[i]['value'];
            //   } else if (address_parts[i]['key'] === 'state') {
            //     state = address_parts[i]['value'];
            //   } else if (address_parts[i]['key'] === 'country') {
            //     country = address_parts[i]['value'];
            //   } else if (address_parts[i]['key'] === 'zip') {
            //     zip = address_parts[i]['value'];
            //   } else if (address_parts[i]['key'] === 'address') {
            //     address = address_parts[i]['value'];
            //   }
            // }
    
            
            // if (city !== undefined && city !== '') {
            //   wcCalc.find("#calc_shipping_city").val(city);
            // }
            // if (state !== '') {
            //   wcCalc.find("#calc_shipping_state").val(state).trigger('change');
            // }
            // if (country !== '') {
            //   wcCalc.find("#calc_shipping_country").val(country).trigger('change');
            // }

            // if (data.zip !== '') {
            //   wcCalc.find("#calc_shipping_postcode").val(data.zip);
            //   theZip = data.zip;
            // } else {
            //   if (zip !== undefined && zip !== '') {
            //     wcCalc.find("#calc_shipping_postcode").val(zip);
            //     theZip = zip;
            //   }
            // }

            // if (address !== '') {
            //   $fullAddressPlaceHolder.html("");
            //   $fullAddressPlaceHolder.append(address + " " + city + " " + state + " " + country + " " + theZip)
              
            //   $fullAddressDestinationPlaceHolder.html(" Shipping to " + "<strong>" + address + " " + city + " " + state + " " + country + " " + theZip + "</strong>.");
            // }

          } else {
            // custom address/zip code(not from google map)
            wcCalc.find("#calc_shipping_city").val('');
            wcCalc.find("#calc_shipping_state").val('').trigger('change');
            wcCalc.find("#calc_shipping_country").val('US').trigger('change');
            

            if (data.zip !== '') {
              wcCalc.find("#calc_shipping_postcode").val(data.zip);
              theZip = data.zip;
            }
            $fullAddressPlaceHolder.html("");
            $fullAddressPlaceHolder.append(data.address + " " + theZip)

            $fullAddressDestinationPlaceHolder.html(" Shipping to " + "<strong>" + data.address + " " + theZip + "</strong>.");
          }
         
        // }
        
        
      } else {

        wcCalc.find("#calc_shipping_city").val('');
        wcCalc.find("#calc_shipping_state").val('').trigger('change');
        wcCalc.find("#calc_shipping_country").val('US').trigger('change');
        wcCalc.find("#calc_shipping_postcode").val('');
        $fullAddressPlaceHolder.html("");
        $fullAddressDestinationPlaceHolder.html("");
        $("ul #shipping_method").css({'display' : 'none'});
        
      }

    }
  });


  // textual labels
  var $cartHeader = $(".entry-title");
  block($cartHeader);
  $.ajax({
    type: 'GET',
    url: rentalObj.url,
    data: {
      action: 'rental_replace_cart_page_textual_labels',
    },
    success: function(data) {
    },
    error: function(data) {
      console.log(data);
    }, complete: function(data) {

      var data_json = data.responseJSON;
      if (data_json.cart_label !== '') {
        
        $cartHeader.text(data_json.cart_label)

        var $updateCartBtn = $("button[name=update_cart]");
        if ($updateCartBtn.length) {
          $updateCartBtn.text("Update "+data_json.cart_label);
        }
      }

      unblock($cartHeader);
    }
  });

});

