/* Global variables */ 
var ifqpf_filter_products,
ifqpf_load_more,
ifqpf_before_ajax_send,
ifqpf_display_response,
ifqpf_get_filter_form_data,
ifqpf_search,
search_minlength;

window.ifqpf_filter_data = '';

var load_more = true;

var ajaxurl = ifqpf_public_object.ajaxurl;
var loader = '<div class="ifqpf-loader"></div>';
search_minlength = parseInt(ifqpf_public_object.search_min_input);
var is_enabled_button = ifqpf_public_object.is_enabled_button;
var ifqpf_prefix = ifqpf_public_object.prefix;
var enabled_filters_names = ifqpf_public_object.enabled_filters_names;
var posts_per_page_load_more = ifqpf_public_object.posts_per_page_load_more;
var store_filter_in_url = ifqpf_public_object.store_filter_in_url;

jQuery(document).ready(function() {
   jQuery("#ifqpf_submit").on("click", function(e){

      /* Main ajax call for filter products */
      ifqpf_filter_products = function(serialize_data = null, is_return = false){

            var page = jQuery("#ifqpf_page").val();
            var offset = jQuery("#ifqpf_offset").val();
            var nonce_val = ifqpf_public_object['nonce'];

            if ( !ifqpf_is_empty(serialize_data) ) {
                  window.ifqpf_filter_data = serialize_data;
                  serialize_data = serialize_data+'&page='+page;
            }
            
            serialize_data = serialize_data+'&action=ifqpf_filter_products';

            if ( !ifqpf_is_empty(offset) ) {
                  serialize_data = serialize_data+"&offset="+offset;
            }

            if(!ifqpf_is_empty(nonce_val)){
                  serialize_data = serialize_data+"&nonce="+nonce_val;
            }

            var ajax_filter = jQuery.ajax({
                  type: "POST",
                  url: ifqpf_public_object.ajaxurl,
                  nonce: ifqpf_public_object.nonce,
                  data: serialize_data,
                  dataType: "json",
                  beforeSend: function(){
                        ifqpf_before_ajax_send();
                        ifqpf_update_filter_url();
                  },
                  success: function (response) {
                        jQuery('html, body').animate({ scrollTop: 0 }, 0);
                        ifqpf_display_response(response);
                        if (typeof ifqpf_javascript_after_ajax === 'function') {
                            ifqpf_javascript_after_ajax();
                        } else if (typeof ifqpf_javascript_after_ajax === 'string' && ifqpf_javascript_after_ajax.trim()) {
                            eval(ifqpf_javascript_after_ajax);
                        }
                  },
                  error: function(){

                  }
            });

            if ( is_return ) {
                  return ajax_filter;
            }

      }

      /* Load more products with infinite scrool */
      ifqpf_load_more = function(){

            var serialize = window.ifqpf_filter_data;
            var max_page = parseInt(jQuery("#ifqpf_max_page").val());
            var page = jQuery("#ifqpf_page").val();
            var offset = jQuery("#ifqpf_offset").val();
            var nonce_val = ifqpf_public_object['nonce'];

            if ( page > max_page ) {
                  return false;
            }

            var additional_param = 'action=ifqpf_filter_products&page='+page+'&ifqpf_load_more=true'+"&nonce="+nonce_val;

            if( !ifqpf_is_empty(serialize) ){
                  serialize = serialize+'&'+additional_param;
            }else{
                  serialize = additional_param;
            }

            if ( !ifqpf_is_empty(offset) ) {
                  serialize = serialize+"&offset="+offset;
            }

            jQuery.ajax({
                  type: "POST",
                  url: ifqpf_public_object.ajaxurl,
                  data: serialize,
                  dataType: "json",
                  beforeSend: function(){
                        load_more = false;
                        ifqpf_before_ajax_send();
                  },
                  success: function (response) {
                        ifqpf_display_response(response, true);
                        load_more = true;
                        if (typeof ifqpf_javascript_after_ajax === 'function') {
                            ifqpf_javascript_after_ajax();
                        } else if (typeof ifqpf_javascript_after_ajax === 'string' && ifqpf_javascript_after_ajax.trim()) {
                            eval(ifqpf_javascript_after_ajax);
                        }
                  },
                  complete: function(){
                        jQuery('body').removeClass('no-scroll');
                  },
                  error: function(){
                        
                  }
            });

      }

      /* Search product */
      ifqpf_search = function(value){

            var serialize_data = ifqpf_get_filter_form_data();
            ifqpf_clear_filter_data();
            return ifqpf_filter_products(serialize_data, true);

      }

      /* Before filter ajax call */
      ifqpf_before_ajax_send = function(){

            if ( ifqpf_is_empty(is_enabled_button) ){
                  jQuery(".ifqpf-from").addClass("loading");
            }
            jQuery('.ifqpf-main-wrap > ul').addClass("ifqpf-loading");
            jQuery(".ifqpf-main-wrap > ul").append(loader);
            jQuery('.ifqpf-mobile-overlay').removeClass('ifqpf-mobile-active');
            jQuery('.ifqpf-form-wrap').removeClass('ifqpf-mobile-active');
            jQuery(".woocommerce-pagination").remove();

      }

      /* Display response after filterd data */
      ifqpf_display_response = function(response, is_load_more=false){

            if ( ifqpf_is_empty(is_enabled_button) ){
                  jQuery(".ifqpf-from").removeClass("loading");
            }
            jQuery('.ifqpf-main-wrap > ul').removeClass("ifqpf-loading");
            jQuery(".ifqpf-main-wrap > ul .ifqpf-loader").remove();

            if ( response.hasOwnProperty('products_html') && response.products_html !== '' ) {
                  if ( is_load_more ) {
                        jQuery(".ifqpf-main-wrap > ul").append(response.products_html);  
                  }else{
                        jQuery(".ifqpf-main-wrap > ul").html(response.products_html);
                  }
                  
            }

            var count_product = jQuery('li.product.status-publish').length;                   

            if(response.max_page == 0){
                  jQuery(".quick-result-count-display").html('Showing '+count_product+' products out of '+count_product+' products'); 
            }  else {
                  jQuery(".quick-result-count-display").html('Showing '+count_product+' products out of '+response.total_found_posts+' products');
            }

            if(response.max_page == 0 && count_product == 0){
                  jQuery(".quick-result-count-display").html('');
            }

            if(response.max_page == 0){
                  jQuery(".msgerror").html('No more products found!');
                  jQuery('#loadmorebtn').hide();
            }
            if(response.max_page == 0 && count_product == 0){
                  jQuery(".msgerror").html('');
            } 
            if(response.max_page > 0){
                  jQuery(".msgerror").html('');
                  jQuery('#loadmorebtn').show();
            }
            if(response.total_found_posts == count_product){
                jQuery('#loadmorebtn').hide();
                jQuery(".msgerror").html('No more products found!');
            }
            if(response.max_page == 1){
                  jQuery(".msgerror").html('');
            }
            if(response.total_found_posts == 0){
                  jQuery(".msgerror").html('');
            }
            if ( response.hasOwnProperty('pagination_html') && response.pagination_html !== '' ) {
                  jQuery("nav.woocommerce-pagination").html(response.pagination_html);
            }

            if ( response.hasOwnProperty('max_page') ) {
                  jQuery("#ifqpf_max_page").val(response.max_page);
            }

            if ( response.hasOwnProperty('result_count') ) {
                  jQuery("p.woocommerce-result-count").replaceWith(response.result_count);
            }

            if ( response.hasOwnProperty('page') ) {
                  jQuery("#ifqpf_page").val(response.page);
            }

            if ( response.hasOwnProperty('offset') ) {
                  jQuery("#ifqpf_offset").val(response.offset);
            }

            ifqpf_update_pag_filter_url();
            
      }

      /* Clear hidden input */
      ifqpf_clear_filter_data = function(){
            jQuery("#ifqpf_max_page").val("");
            jQuery("#ifqpf_page").val("1");
            jQuery("#ifqpf_offset").val("");
      }

      /* Get filter form data in serialize format */
      ifqpf_get_filter_form_data = function(is_serializeArray=false){
           var serialize;
            if( is_serializeArray ){
                 serialize = jQuery(".ifqpf-from").serializeArray();
            }else{
                  serialize = jQuery(".ifqpf-from").serialize();
            }
           return serialize;
      }

      /* Update url for filter */
      ifqpf_update_filter_url = function() {
            
            if( store_filter_in_url === 'yes' ){

                  var data = ifqpf_get_filter_form_data(true);
                  var url_data = ifqpf_get_url_form_data(data);

                  ifqpf_remove_param_current_url(url_data);

                  if( Object.keys(url_data).length > 0 ){

                        jQuery.each(url_data, function(param_name, param_value) {
                              
                              window.history.replaceState('', '', ifqpf_upd_url_param(window.location.href, param_name, param_value));

                        });

                  }    

                  window.history.pushState("data","Title",window.location.href);

            }

      }

      /* Add pagination parameters to url */
      ifqpf_update_pag_filter_url = function() {

            if( store_filter_in_url === 'yes' ){

                  var ifqpf_max_page = jQuery("#ifqpf_max_page").val();
                  var ifqpf_page = jQuery("#ifqpf_page").val();
                  var ifqpf_offset = jQuery("#ifqpf_offset").val();

                  window.history.replaceState('', '', ifqpf_upd_url_param(window.location.href, 'ifqpf_max_page', ifqpf_max_page));
                  window.history.replaceState('', '', ifqpf_upd_url_param(window.location.href, 'ifqpf_page', ifqpf_page));
                  window.history.replaceState('', '', ifqpf_upd_url_param(window.location.href, 'ifqpf_offset', ifqpf_offset));
                  window.history.replaceState('', '', ifqpf_upd_url_param(window.location.href, 'ifqpf_page_limit', posts_per_page_load_more)); 

                  window.history.pushState("data","Title",window.location.href);
                  
            }

      }

      window.ifqpf_filter_data = ifqpf_get_filter_form_data();

      // Code for shop products ul class columns override
      var ifqpf_shop_columns = jQuery('#ifqpf_shop_columns').val();
      var classList = jQuery('.ifqpf-main-wrap > ul').attr('class').split(/\s+/);
      var finalval_shop_columns = 'columns-'+ifqpf_shop_columns;

      var columns_arr = ['columns-1', 'columns-2', 'columns-3', 'columns-4', 'columns-5', 'columns-6'];

      var classList = jQuery('.ifqpf-main-wrap > ul').attr('class').split(/\s+/);
      for (var i = 0; i < classList.length; i++) {
            if (jQuery.inArray(classList[i], columns_arr) > -1)
            {
                  jQuery('.ifqpf-main-wrap > ul').removeClass(classList[i]);
                  jQuery('.ifqpf-main-wrap > ul').addClass(finalval_shop_columns);
            }
      }        
    }); 
});

/* Check if give value is empty or not */
function ifqpf_is_empty(value){

      var is_empty = false;

      if( value === null || value === '' || typeof value === 'undefined' ){
            is_empty  = true;
      }

      return is_empty;
}

/* Update url parameters */
function ifqpf_upd_url_param(url, param, paramVal){
      var TheAnchor = null;
      var newAdditionalURL = "";
      var tempArray = url.split("?");
      var baseURL = tempArray[0];
      var additionalURL = tempArray[1];
      var temp = "";

      if (additionalURL) 
      {
            var tmpAnchor = additionalURL.split("#");
            var TheParams = tmpAnchor[0];
            TheAnchor = tmpAnchor[1];
            if(TheAnchor)
                  additionalURL = TheParams;

            tempArray = additionalURL.split("&");

            for (var i=0; i<tempArray.length; i++)
            {
                  if(tempArray[i].split('=')[0] != param)
                  {
                        newAdditionalURL += temp + tempArray[i];
                        temp = "&";
                  }
            }        
      }
      else
      {
            var tmpAnchor = baseURL.split("#");
            var TheParams = tmpAnchor[0];
            TheAnchor  = tmpAnchor[1];

            if(TheParams)
                  baseURL = TheParams;
      }

      if(TheAnchor)
            paramVal += "#" + TheAnchor;

      var rows_txt = ( !ifqpf_is_empty(paramVal) ) ? temp + "" + param + "=" + paramVal : '';
      return baseURL + "?" + newAdditionalURL + rows_txt;
}

/* Replace array to array key */
function ifqpf_replace_array_to_key(string){

      var last_array = string.substr(string.lastIndexOf("["), string.length);
      last_array_key = last_array.replace(/[[\]]/g, '');

      if( ifqpf_is_empty(last_array_key) ){
            new_string = string.replace(/\s/g,'').replace(/\]\[/g, '_').replace(/\[/g, '_').replace(/\]/g, '_').replace(/\_\_/g, '');
      }else{
            new_string = string.replace(/\s/g,'').replace(/\]\[/g, '_').replace(/\[/g, '_').replace(/\]/g, '').replace(/\_\_/g, '');
      }

      return new_string;
}

/* Add prefix to sting */
function ifqpf_add_prefix_to_string(string){

      if( !string.startsWith(ifqpf_prefix) ){
            string = ifqpf_prefix + '_' + string;
      }

      return string;
}

/* Ge data from url */
function ifqpf_get_url_form_data(data){

      var url_data = {};

      for (var i = 0; i < data.length; i++) {
            
            var param = data[i];  
            var param_name = param.name;
            var param_value = param.value; 
            var param_values = [];         

            if( param_name.lastIndexOf("[") === -1 ){

                  param_name = ifqpf_add_prefix_to_string(param_name);

            }else{

                  for (var j = 0; j < data.length; j++) {
                        
                        if( data[j].name === param_name ){
                            param_values.push(data[j].value);  
                        }

                  }

                  if( param_values.length ){

                        param_name = ifqpf_replace_array_to_key(param_name);
                        param_name = ifqpf_add_prefix_to_string(param_name);
                        param_value = param_values.join(',');

                  }

            }

            if( !ifqpf_is_empty(param_value) ){
                  url_data[param_name] = param_value;
            }

      }

      return url_data;

}

/* Remove parameters from url */
function ifqpf_remove_param(param_name, URL) {
    var rtn = URL.split("?")[0],
        param,
        params_arr = [],
        queryString = (URL.indexOf("?") !== -1) ? URL.split("?")[1] : "";
    if (queryString !== "") {
        params_arr = queryString.split("&");
        for (var i = params_arr.length - 1; i >= 0; i -= 1) {
            param = params_arr[i].split("=")[0];
            if (param === param_name) {
                params_arr.splice(i, 1);
            }
        }
        if (params_arr.length) rtn = rtn + "?" + params_arr.join("&");
    }
    return rtn;
}

/* Remove parameters from url by enabled filters */
function ifqpf_remove_param_current_url(url_data) {
      
      for (const filter in enabled_filters_names) {
           
            if( !url_data.hasOwnProperty(filter) ){
                  window.history.replaceState('', '', ifqpf_remove_param(filter, window.location.href));
            }

      }

      window.history.pushState("data","Title",window.location.href);
}

/* Get parameters from url */
function ifqpf_get_URL_param(param_name=''){

      var vars = [], hash, value = '';
      var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

      for(var i = 0; i < hashes.length; i++) {

            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];

      }

      if( vars.hasOwnProperty(param_name) ){
            value = vars[param_name];
      }

      return value;
}

jQuery(document).ready(function () {  
      jQuery("<span class='msgerror'></span>").insertAfter(".ifqpf-main-wrap > ul"); 
      jQuery( "<div class='loadmorebtn_cls'><input type='button' value='Load more' id='loadmorebtn'/></div>" ).insertAfter( ".ifqpf-main-wrap > ul" );
      jQuery( "p.quick-result-count-display" ).insertBefore( jQuery( ".ifqpf-main-wrap > ul") );
      jQuery('#loadmorebtn').hide();
      jQuery("#loadmorebtn").click(function(){
            if ( load_more ) {
                  ifqpf_load_more_new();
            }
      });      
});

jQuery(document).on('ifqpf_ajax_completed', function () {
    if (
        typeof window.ifqpf_javascript_after_ajax === 'string' &&
        window.ifqpf_javascript_after_ajax.trim()
    ) {
        eval( window.ifqpf_javascript_after_ajax );
    }
});
