/* global gtag */
// let initialized = false;
// CookieBar.initialized;
var CookieBar = {
   initialized: false,

   init: function() {
      if (CookieBar.initialized) {
         return;
      }
      CookieBar.initialized = true;
      this._create();
      var me = this;

      $('[data-cookie-bar=open]').click(function() {
         me.getSimple().hide();
         me.getAdvanced().show();
         me._show();
         return false;
      });

      // show advanced
      this.getSimple()
         .find('[data-cookie-bar=details]')
         .click(function() {
            if (me.getCookieBar().data('cookiebar-preselect') === 'y') {
               me.preselectAll();
            }
            me.getSimple().hide();
            me.getAdvanced().show();
            return false;
         });

      // simple accept
      this.getSimple()
         .find('[data-cookie-bar=simpleAccept]')
         .click(function() {
            me.simpleAccept();
            return false;
         });

      // simple reject
      this.getSimple()
         .find('[data-cookie-bar="reject"]')
         .click(function() {
            me.customAccept(false);
            return false;
         })

      // advanced accept
      this.getAdvanced()
         .find('[data-cookie-bar=advancedAccept]')
         .click(function() {
            me.advancedAccept();
            return false;
         });

      // select all / deselect all
      this.getAdvanced()
         .find('[data-cookie-bar="custom"]')
         .click(function() {
            me.customAccept($(this).data('cookie-custom') === 'allow-all');
            return false;
         });
   },

   getCookieBar: function() {
      return $('#cookieBara');
   },

   getSimple: function() {
      return this.getCookieBar().find('[data-cookie-bar="simple"]');
   },

   getAdvanced: function() {
      return this.getCookieBar().find('[data-cookie-bar="advanced"]');
   },

   getAdvancedCheckbox: function() {
      return this.getAdvanced().find('input[name=social-media]');
   },

   _create: function() {
      let cookieBar = this._getCookies().getItem('cookie-bar');
      if (cookieBar === null) {
         this.getAdvanced().hide();
         this._show();
      } else if (cookieBar == 1) {
         CookieBar.simpleAccept();
      } else {
         let storages = cookieBar.split(',');
         $('input[value="ad_storage"]').prop('checked', storages.includes('ad_storage'));
         $('input[value="personalization_and_functionality_storage"]')
            .prop('checked', storages.includes('personalization_and_functionality_storage'));
         $('input[value="analytics_storage"]').prop('checked', storages.includes('analytics_storage'));
      }
   },

   _getCookies: function() {
      return wpj.storage.cookies;
   },

   _setCookieBar: function(value) {
      let date = new Date();
      date.setMonth(date.getMonth() + (value ? 12 : 6));

      this._getCookies().setItem('cookie-bar', value, date);
   },

   _isFocus: function() {
      return !!this.getCookieBar().closest('.focus.focus-cookiebara').length;
   },

   _show: function() {
      if (this._isFocus()) {
         var $focus = $('.focus.focus-cookiebara');
         $focus.focus({
            addBodyPadding: true,
            closeOnBgClick: false
         });
         $focus.focus('show');
      } else {
         this.getCookieBar().show();
      }
   },

   _close: function() {
      if (this._isFocus()) {
         var $focus = $('.focus.focus-cookiebara.active');
         $focus.focus('hide');
      } else {
         this.getCookieBar().hide();
      }
   },

   preselectAll: function() {
      this.getAdvanced().find('input:checkbox[name=social-media]').each(function() {
         $(this).prop('checked', true);
      });
   },

   simpleAccept: function() {
      const $this = this;
      setTimeout(function() {
         $this.preselectAll();
         CookieBar.advancedAccept();
      }, 100);
      this._close();
   },

   customAccept: function(isAllowed) {
      this.getAdvanced().find('input:enabled').prop('checked', isAllowed);
      CookieBar.advancedAccept();
   },

   advancedAccept: function() {
      let consents = [];

      $('input:checkbox[name=social-media]:checked').each(function() {
         consents.push($(this).val());
      });

      if (window.wpj.cookie_bar_send) {
         let consents_props = {
            ad_storage: consents.includes('ad_storage') ? 'granted' : 'denied',
            ad_user_data: consents.includes('ad_storage') ? 'granted' : 'denied',
            ad_personalization: consents.includes('ad_storage') ? 'granted' : 'denied',
            personalization_storage: consents.includes('personalization_and_functionality_storage') ? 'granted' : 'denied',
            functionality_storage: 'granted',
            analytics_storage: consents.includes('analytics_storage') ? 'granted' : 'denied'
         }

         gtag('consent', 'update', consents_props);

         dataLayer.push({'event': 'kupshopConsentsChange', 'consents': consents_props});
      }
      this._setCookieBar(consents.length ? consents : 0);
      document.dispatchEvent(new CustomEvent("consentsChange"));

     this._close();
   }
};

$(window).on('load', function() {
   CookieBar.init();
});

wpj.onReady.push(function() {
   CookieBar.init();
});
