import {callAction} from '../utils/http-action';
import {ROUTES} from '../utils/routes';
import {showLoader} from '../utils/loader';

const charityEl = {
  charityItem: '.charity-new',
  charityMultiple: 'charity-multiple',
  charityInput: '#donation',
  charityTitleWrap: '.title-wrap',
  donationCart: '.donation-cart',
  donationCartBtn: '.donation-cart__btn',
  donationCartHolder: '.donation-cart__holder',
  donationCartHide: 'donation-cart__hide',
  donationCartValue: '.donation-cart__value',
  charityAdded: 'charity-added',
}

export function updateCharity() {
  //checkbox action
  if ($('#donation').attr('disabled') === 'disabled') {
    $('#donation').removeAttr('disabled')
  } else {
    $('#donation').prop('disabled', (_, value) => !value);
  }

  const context = getCurrentCharityItem(this);
  const productCode = getCurrentProductCode(context);

  showLoader('.basket-loader');

  if (context.hasClass(charityEl.charityAdded)) {
    $(this).removeAttr('checked');
  } else {
    $(this).attr('checked', 'checked');
    charityActionForAdd(productCode);
  }
}

function getCurrentCharityItem(context) {
  return $(context).closest(charityEl.charityItem);
}

function getCurrentProductCode(context) {
  return context.find('input[name="productCode"]').val();
}

export function charityActionForAdd(value) {
  if (!Wick.checkLocalEnv) {
    const payload = {
      url: ROUTES.CHARITY_ADD,
      queries: {
        productCodePost: value,
        qty: getDefaultCharityAmount(),
      },
      type: 'POST',
    };
    const productInfo = {
      quantity: null,
      fulfillment: 'Delivery',
    };

    callAction(payload, executeCharityAction, productInfo);
  } else {
    toggleCharityElements();
    Wick.BasketPage.updateCart(...Array(2), true);
  }
}

export function toggleCharityElements() {
  if ($(charityEl.charityItem).hasClass(charityEl.charityAdded)) {
    $(charityEl.charityInput).prop('checked', false);
  }
  $(charityEl.charityItem).toggleClass(charityEl.charityAdded);
  $(charityEl.charityItem).hasClass(charityEl.charityMultiple) && toggleMultipleCharityChoose($(charityEl.charityItem));
}

function executeCharityAction(response, productInfo) {
  if (response.addedProducts && response.addedProducts[0]) {
    productInfo.quantity = response.addedProducts[0].qty;
    $(document).trigger("EEAddToCart", productInfo);
  }

  Wick.BasketPage.updateCart(...Array(2), true);
  toggleCharityElements();
}

export function changeCharityPrice() {
  if ($(this).hasClass('btn-selected')) {
    return;
  }

  const charityValue = $(this).attr('data-value'),
    context = getCurrentCharityItem(this),
    productCode = getCurrentProductCode(context);

  repaintCharityButton(this);
  $(charityEl.donationCartValue).text(charityValue);
  charityActionForChangePrice(this);
}

function charityActionForChangePrice(context) {
  const component = $(context).closest(Wick.BasketPage.el.charityItem);
  const entryNumber = $(component).find('input[name="entryNumber"]').val();
  const value = $(context).attr('data-value');

  showLoader('.basket-loader');
  Wick.BasketPage.updateQuantityAction(entryNumber, value, context, component);
}

function repaintCharityButton(context) {
  const donationButtons = $(context).closest(charityEl.donationCartHolder).find(charityEl.donationCartBtn);
  const btnSelected = 'btn-selected';
  const btnSecondary = 'btn-secondary';

  donationButtons.each((i, btn) => {
    $(btn).hasClass(btnSelected) && $(btn).removeClass(btnSelected).addClass(btnSecondary);
  })

  $(context).removeClass(btnSecondary).addClass(btnSelected);
}

function resetCharityButton(context) {
  const donationButtons = $(context).find(charityEl.donationCartBtn);
  const btnSelected = 'btn-selected';
  const btnSecondary = 'btn-secondary';
  const defaultAmount = getDefaultCharityAmount();

  donationButtons.each((i, btn) => {
    $(btn).attr('data-value') === defaultAmount
      ? $(btn).removeClass(btnSecondary).addClass(btnSelected)
      : $(btn).removeClass(btnSelected).addClass(btnSecondary);
  })
}

function getDefaultCharityAmount() {
  const donationButtons = $(charityEl.charityItem).find(charityEl.donationCartBtn);
  return donationButtons.first().attr('data-value');
}

function resetCharityValue() {
  const defaultValue = getDefaultCharityAmount();
  $(charityEl.donationCartValue).text(defaultValue);
}

export function toggleMultipleCharityChoose(context) {
  context.find(charityEl.donationCart).toggleClass(charityEl.donationCartHide);
  resetCharityButton(context);
  resetCharityValue();
}

export function setDefaultCharityValue() {
  $(charityEl.charityItem).hasClass(charityEl.charityMultiple) && resetCharityValue();
}
