/**
 * File customizer.js.
 *
 * Theme Customizer enhancements for a better user experience.
 *
 * Contains handlers to make Theme Customizer preview reload changes asynchronously.
 */

function convertHex(hex) {
  hex = hex.replace("#", "");
  r = parseInt(hex.substring(0, 2), 16);
  g = parseInt(hex.substring(2, 4), 16);
  b = parseInt(hex.substring(4, 6), 16);

  result = "" + r + "," + g + "," + b + "";
  return result;
}

(function ($) {
  //color
  wp.customize("primary_color", function (value) {
    value.bind(function (to) {
      console.log(to);
      color = convertHex(to);
      document.body.style.setProperty("--primary-color", color);
    });
  });

  wp.customize("secondary_color", function (value) {
    value.bind(function (to) {
      console.log(to);
      color = convertHex(to);
      document.body.style.setProperty("--secondary-color", color);
    });
  });

  wp.customize("dark_color", function (value) {
    value.bind(function (to) {
      console.log(to);
      color = convertHex(to);
      document.body.style.setProperty("--dark-color", color);
    });
  });

  wp.customize("light_color", function (value) {
    value.bind(function (to) {
      console.log(to);
      color = convertHex(to);
      document.body.style.setProperty("--light-color", color);
    });
  });

  wp.customize("text_color", function (value) {
    value.bind(function (to) {
      console.log(to);
      color = convertHex(to);
      document.body.style.setProperty("--text-color", color);
    });
  });

  // Site title and description.
  wp.customize("blogname", function (value) {
    value.bind(function (to) {
      $(".site-title a").text(to);
    });
  });
  wp.customize("blogdescription", function (value) {
    value.bind(function (to) {
      $(".site-description").text(to);
    });
  });

  // Site title color
  wp.customize("bs_site_title_color_option", function (value) {
    value.bind(function (to) {
      $(".site-title a").css("color", to);
    });
  });

  // Site title font family
  wp.customize("bs_site_identity_font_family", function (value) {
    value.bind(function (to) {
      $("head").append(
        "<link href='https://fonts.googleapis.com/css?family=" +
          to +
          ":200,300,400,500,600,700,800,900|' rel='stylesheet' type='text/css'>"
      );
      $("header .site-title a").css("font-family", to);
    });
  });

  // Site title size
  wp.customize("bs_site_title_size", function (value) {
    value.bind(function (to) {
      $(".site-title a").css("font-size", to + "px");
      $("header .custom-logo-link img").css("height", to * 2 + "px");
    });
  });

  // font	family

  wp.customize("body_font_family", function (value) {
    value.bind(function (to) {
      $("head").append(
        "<link href='https://fonts.googleapis.com/css?family=" +
          to +
          ":200,300,400,500,600,700,800,900|' rel='stylesheet' type='text/css'>"
      );
      document.body.style.setProperty("--body-font", to);
    });
  });

  wp.customize("heading_font_family", function (value) {
    value.bind(function (to) {
      $("head").append(
        "<link href='https://fonts.googleapis.com/css?family=" +
          to +
          ":200,300,400,500,600,700,800,900|' rel='stylesheet' type='text/css'>"
      );
      document.body.style.setProperty("--heading-font", to);
    });
  });

  wp.customize("topheading_font_family", function (value) {
    value.bind(function (to) {
      $("head").append(
        "<link href='https://fonts.googleapis.com/css?family=" +
          to +
          ":200,300,400,500,600,700,800,900|' rel='stylesheet' type='text/css'>"
      );
      document.body.style.setProperty("--topheading-font", to);
    });
  });

  wp.customize( 'relax_spa_heading_font_weight', function( value ) {
    value.bind( function( to ) {
      $( 'h1, h2, h3, h4, h5, h6' ).css( 'font-weight', to );
    } );
  } );

  wp.customize( 'relax_spa_heading_1_size', function( value ) {
    value.bind( function( to ) {
      $( 'h1' ).css( 'font-size', to + 'px' );
    } );
  } );

  wp.customize( 'relax_spa_heading_2_size', function( value ) {
    value.bind( function( to ) {
      $( 'h2' ).css( 'font-size', to + 'px' );
    } );
  } );

  wp.customize( 'relax_spa_heading_3_size', function( value ) {
    value.bind( function( to ) {
      $( 'h3' ).css( 'font-size', to + 'px' );
    } );
  } );

  wp.customize( 'relax_spa_heading_4_size', function( value ) {
    value.bind( function( to ) {
      $( 'h4' ).css( 'font-size', to + 'px' );
    } );
  } );

  wp.customize( 'relax_spa_heading_5_size', function( value ) {
    value.bind( function( to ) {
      $( 'h5' ).css( 'font-size', to + 'px' );
    } );
  } );

  wp.customize( 'relax_spa_heading_6_size', function( value ) {
    value.bind( function( to ) {
      $( 'h6' ).css( 'font-size', to + 'px' );
    } );
  } );

  wp.customize( 'relax_spa_topheading_size', function( value ) {
    value.bind( function( to ) {
      $( '.sub-title' ).css( 'font-size', to + 'px' );
      $( 'h1 .sub-title' ).css( 'font-size', to + 'px' );
    } );
  } );

  wp.customize("font_size", function (value) {
    value.bind(function (to) {
      $("html, :root").css("font-size", to);
    });
  });

  wp.customize("body_font_weight", function (value) {
    value.bind(function (to) {
      $("body").css("font-weight", to);
    });
  });

  wp.customize("body_line_height", function (value) {
    value.bind(function (to) {
      $("body").css("line-height", to);
    });
  });

  //who we are
  wp.customize("bs_heading_for_who_we_are", function (value) {
    value.bind(function (to) {
      $(".about h2.main-title").text(to);
    });
  });

  //why choose us
  wp.customize("bs_heading_for_why_choose_us", function (value) {
    value.bind(function (to) {
      $(".why-choose h2.title").text(to);
    });
  });

  wp.customize("bs_top_heading_for_why_choose_us", function (value) {
    value.bind(function (to) {
      $(".why-choose span.sub-title").text(to);
    });
  });

  //testimonial
  wp.customize("bs_heading_for_testimonial", function (value) {
    value.bind(function (to) {
      $(".testimonial h2.title").text(to);
    });
  });

  wp.customize("bs_top_heading_for_testimonial", function (value) {
    value.bind(function (to) {
      $(".testimonial span.sub-title").text(to);
    });
  });

  //expert
  wp.customize("bs_heading_for_teams", function (value) {
    value.bind(function (to) {
      $(".team h2.title").text(to);
    });
  });

  wp.customize("bs_top_heading_for_teams", function (value) {
    value.bind(function (to) {
      $(".team span.sub-title").text(to);
    });
  });

  //offers
  wp.customize("bs_heading_for_offer", function (value) {
    value.bind(function (to) {
      $(".discount .discount-holder h2").text(to);
    });
  });

  wp.customize("bs_content_for_offer", function (value) {
    value.bind(function (to) {
      $(".discount .discount-holder p").text(to);
    });
  });

  wp.customize("bs_read_more_label", function (value) {
    value.bind(function (to) {
      $(".discount .discount-holder a").text(to);
    });
  });

  //pricing
  wp.customize("bs_tickets_heading", function (value) {
    value.bind(function (to) {
      $(".pricing h2.title").text(to);
    });
  });

  wp.customize("bs_tickets_top_heading", function (value) {
    value.bind(function (to) {
      $(".pricing span.sub-title").text(to);
    });
  });

  //copyright_text
  wp.customize("bs_copyright_text", function (value) {
    value.bind(function (to) {
      $(".footer .copyright p span.editable").text(to);
    });
  });
})(jQuery);
