/*global jQuery*/ /*jslint this:true*/ /*global window*/ /*global console*/ /*jslint long:true*/

(function ($) {
  "use strict";

  var bookyourtravel_admin_panel;

  $(document).ready(function () {
    bookyourtravel_admin_panel.init();
  });

  bookyourtravel_admin_panel = {
    init: function () {
      $(".licence-activation .button-activate").off('click');
      $(".licence-activation .button-activate").on('click', function (e) {
        const purchase_code = $('.licence-activation #licence_purchase_code').val();

        if (purchase_code) {
          e.preventDefault();
          $('.licence-activation .processing').show();

          var dataObj = {
            'action'		: 'activate_1_theme_ajax_request',
            'nonce' 		: BookYourTravelAjax.nonce,
            'purchase_code'		: purchase_code,
          };

          jQuery.ajax({
            url: BookYourTravelAjax.ajaxurl,
            data: dataObj,
            success:function(rawData) {
              const data = JSON.parse(rawData);

              if (data && data['body']) {
                const body = JSON.parse(data['body']);

                console.log(body);

                if (body && body['error']) {
                  $('.licence-activation .error').show();
                  $('.licence-activation .processing').hide();
                } else if (body && body['item'] && body['item']['wordpress_theme_metadata'] && body['item']['wordpress_theme_metadata']['theme_name'] && body['item']['wordpress_theme_metadata']['theme_name'] === 'BookYourTravel') {
                  dataObj = {
                    'action'		: 'activate_2_theme_ajax_request',
                    'nonce' 		: BookYourTravelAjax.nonce,
                    'purchase_code'		: purchase_code,
                  };

                  jQuery.ajax({
                    url: BookYourTravelAjax.ajaxurl,
                    data: dataObj,
                    success:function(data) {
                      $('.licence-activation .error').hide();
                      $('.licence-form').hide();
                      $('.licence-activation .success').show();
                      $('.licence-activation .processing').hide();

                      setTimeout(function(){
                        window.location.reload();
                      }, 500);
                    }
                  });
                } else {
                  $('.licence-activation .error').show();
                  $('.licence-activation .processing').hide();
                }
              } else {
                $('.licence-activation .error').show();
                $('.licence-activation .processing').hide();
              }
            },
            error: function(errorThrown){
              console.log(errorThrown);
              $('.licence-activation .error').show();
              $('.licence-activation .processing').hide();
            }
          });
        }
      });

      $(".licence-deactivation .button-deactivate").off('click');
      $(".licence-deactivation .button-deactivate").on('click', function (e) {
        e.preventDefault();
        $('.licence-deactivation .processing').show();

        var dataObj = {
          'action'		: 'activate_3_theme_ajax_request',
          'nonce' 		: BookYourTravelAjax.nonce,
        };

        jQuery.ajax({
          url: BookYourTravelAjax.ajaxurl,
          data: dataObj,
          success:function(rawData) {
            $('.licence-deactivation .processing').hide();
            setTimeout(function(){
              window.location.reload();
            }, 500);
          },
          error: function(errorThrown){
            console.log(errorThrown);
            $('.licence-deactivation .processing').hide();
            $('.licence-deactivation .error').show();
          }
        });
      });
    },
  };
}(jQuery));
