var Wick = Wick || {};
Wick.DefaultStoreProto = {
  el: {
    searchStoreForm: '.search-store',
    $searchStoreWrapInput: $('.search-store__input'),
    searchStoreErrorClass: 'search-store__input-error',
    searchStoreInput: '.search-store #postcode',
    $searchStoreError: $('.form-row__error'),
    $storeUnavailableSection: $('.store-unavailable'),
    $listOfStoreForm: $('.list-of-store'),
    btnCancel: '.my-account_cancel-store',
    btnLocation: '.btn--location',
    iconLocation: '.btn--location .icon',
    errorIconLocation: '.btn--location .icon-error',
    $bigWins: $('.bw'),
    $bwActions: $('.bw_act'),
    $sDetails: $('.s-details_hide'),
    sDetailsHide: 's-details_hide',
    $storeMoreLink: $('.store__more a'),
  },
  showStoreList() {
    Wick.DefaultStoreProto.el.$searchStoreError.hide();
    Wick.DefaultStoreProto.el.$storeUnavailableSection.hide();
    Wick.DefaultStoreProto.el.$listOfStoreForm.show();
    Wick.DefaultStoreProto.el.$searchStoreWrapInput.removeClass(Wick.DefaultStoreProto.el.searchStoreErrorClass);

    if (Wick.DefaultStoreProto.el.$bigWins.css('display') === 'none') {
      Wick.DefaultStoreProto.el.$bigWins.show();
    }

    if (Wick.DefaultStoreProto.el.$bwActions.css('display') === 'none') {
      Wick.DefaultStoreProto.el.$bwActions.show();
    }
  },
  showErrorState() {
    Wick.DefaultStoreProto.el.$searchStoreError.show();
    Wick.DefaultStoreProto.el.$storeUnavailableSection.show();
    Wick.DefaultStoreProto.el.$listOfStoreForm.hide();
    Wick.DefaultStoreProto.el.$searchStoreWrapInput.addClass(Wick.DefaultStoreProto.el.searchStoreErrorClass);
  },
  showUnavailableState() {
    Wick.DefaultStoreProto.el.$storeUnavailableSection.show();
    Wick.DefaultStoreProto.el.$listOfStoreForm.hide();

    Wick.DefaultStoreProto.el.$sDetails.filter(function () {
      let isNotElementHide = !$(this).hasClass(Wick.DefaultStoreProto.el.sDetailsHide);

      if (isNotElementHide) {
        $(this)
          .closest(Wick.DefaultStoreProto.el.$bigWins)
          .find(Wick.DefaultStoreProto.el.$storeMoreLink)
          .text('Store Info');
      }

      return isNotElementHide;
    }).addClass(Wick.DefaultStoreProto.el.sDetailsHide);
  },
  clearSearchInput() {
    $(Wick.DefaultStoreProto.el.searchStoreInput).val('');
    Wick.DefaultStore.checkVisibilityOfClearButton();
  },
  toggleErrorStateForLocation() {
    const iconIsVisible = $(Wick.DefaultStoreProto.el.iconLocation).is(':visible');

    if (iconIsVisible) {
      $(Wick.DefaultStoreProto.el.iconLocation).hide();
      $(Wick.DefaultStoreProto.el.errorIconLocation).show();
    } else {
      $(Wick.DefaultStoreProto.el.iconLocation).show();
      $(Wick.DefaultStoreProto.el.errorIconLocation).hide();
    }
  },
  init() {
    $(Wick.DefaultStoreProto.el.searchStoreForm).on('submit', (event) => {
      event.preventDefault();
      const value = Wick.DefaultStore.getPostCode();
      if (value.length) {
        Wick.DefaultStoreProto.showStoreList();
      } else {
        Wick.DefaultStoreProto.showErrorState();
      }
    });
    $(Wick.DefaultStoreProto.el.btnCancel).on('click', (event) => {
      event.preventDefault();
      Wick.DefaultStoreProto.clearSearchInput();
      Wick.DefaultStoreProto.showUnavailableState();
    })
    $(Wick.DefaultStoreProto.el.btnLocation).on('click', (event) => {
      event.preventDefault();
      Wick.DefaultStoreProto.toggleErrorStateForLocation();
    })
  }
}

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