var Wick = window.Wick || {};

Wick.VatToggle = {
  VAT_COOKIE: 'isVatChecked',
  TOGGLE_VAT_EVENT_NAME: 'toggleVat',
  HIDDEN_CLASS_NAME: 'd-none',
  el: {
    switchVatLabel: '.switch-vat-label',
    includingVat: '.including-vat',
    excludingVat: '.excluding-vat',
    switchVatInput: '.switch-vat input',
    isHidden: 'd-none',
    switchLabelCheckedClass: 'switch-label__checked',
  },

  toggle: function () {
    const newSwitcherValue = !Wick.VatToggle.isOn();

    Wick.VatToggle.setLabelState();
    Wick.VatToggle.setSwitcherState(newSwitcherValue);

    Wick.VatToggle.setCookie(Wick.VatToggle.VAT_COOKIE, newSwitcherValue);

    const event = Wick.VatToggle.createToggleEvent(newSwitcherValue);
    window.dispatchEvent(event);
  },

  setLabelState: function() {
    Wick.VatToggle.toggleLabelClass();
  },

  setSwitcherState: function(isToggled) {
    $(Wick.VatToggle.el.switchVatInput).attr('checked', isToggled);
  },

  createToggleEvent: function(isToggled) {
    return new CustomEvent(Wick.VatToggle.TOGGLE_VAT_EVENT_NAME, {
      detail: { toggled: isToggled },
    });
  },

  toggleLabelClass: function () {
    $(Wick.VatToggle.el.switchVatLabel).toggleClass(Wick.VatToggle.el.switchLabelCheckedClass);
  },

  isOn: function () {
    return !!Wick.VatToggle.getCookie(Wick.VatToggle.VAT_COOKIE);
  },

  toggleDisplayClass: function () {
    const includingVatEl = Wick.VatToggle.el.includingVat;
    const excludingVat = Wick.VatToggle.el.excludingVat;
    const hiddenClassName = Wick.VatToggle.HIDDEN_CLASS_NAME;

    $(includingVatEl).toggleClass(hiddenClassName);
    $(excludingVat).toggleClass(hiddenClassName);
  },

  getCookie: function(cookieName) {
    const cookie = decodeURIComponent(document.cookie)
      .split(';')
      .map((cookie = '') => cookie.trim())
      .find((cookie = '') => cookie.startsWith(`${cookieName}=`));

    if(cookie) {
      const cookieValue = cookie.replace(`${cookieName}=`, '');
      return covertStringBooleanToBoolean(cookieValue);
    }

    return '';
  },

  setCookie: function(cookie, value) {
    const cookiesString = (`${cookie}=${encodeURIComponent(value)};path=/`);
    document.cookie = cookiesString;
  },

  init: function() {
    const isVatShouldBeToggled = Wick.VatToggle.isOn();
    const isVatToggled = !!$(Wick.VatToggle.el.switchVatInput).attr('checked');

    if(isVatShouldBeToggled) {
      Wick.VatToggle.setLabelState();
    }
    Wick.VatToggle.setSwitcherState(isVatShouldBeToggled);

    $(Wick.VatToggle.el.switchVatInput).on('change', Wick.VatToggle.toggle);

    $(window).on(Wick.VatToggle.TOGGLE_VAT_EVENT_NAME, function (e) {
      Wick.VatToggle.toggleDisplayClass();
    })

    if(isVatShouldBeToggled !== isVatToggled) {
      const event = Wick.VatToggle.createToggleEvent(isVatShouldBeToggled);
      window.dispatchEvent(event);
    }
  }
};

function covertStringBooleanToBoolean(stringBoolean) {
  if (stringBoolean === 'true') {
    return true;
  }
  if (stringBoolean === 'false') {
    return false;
  }
  return stringBoolean;
}

$(document).ready(function () {
  Wick.VatToggle.init();
});
