import {
  PROCESSING_STARTED,
  PROCESSING_FINISHED,
  ESHOP_CART_SET_FOC_PWP,
  SET_SELECTED_FOC_ITEMS,
  SET_SELECTED_PWP_ITEMS,
  SET_FOC_SELECTION_COMPLETE,
  SET_PWP_SELECTION_COMPLETE,
  SET_PWP_FOC_SELECTION_COMPLETE,
  SET_SELECTED_FOCPWP_ITEMS,
} from '../../../utils/types'
import { ESHOP_CART_ELIGIBLE_PROMO } from '../../../utils/apiUrls'
import { post } from '../../../utils/api'
import helpers from '@/helpers'

export const loadCartProductEligible = async ({ store, getters, commit, dispatch, rootGetters }, data) => {
  commit(PROCESSING_STARTED, 'loadingCartProductEligible', { root: true })
  let country = rootGetters.getSelectedCountry
  let location = rootGetters.getSelectedCountryLocation
  let cancelToken = await dispatch('generateCancelToken', 'loadingCartProductEligible', { root: true })
  let param = {
    function_type: 'promo',
    country_id: country.id,
    location_id: location.id,
  }
  if (rootGetters.getAppStatus('enrolment') === true) {
    param.is_enrollment = true
    param.order_for_user_id = null
  } else {
    param.order_for_user_id =
      helpers.getValue(rootGetters['eshop/getOrderingFor'], 'details.user_id') ||
      helpers.getValue(rootGetters['getLoggedInUser'], 'id', null)
  }
  return post(ESHOP_CART_ELIGIBLE_PROMO, param, cancelToken.token)
    .then(response => {
      commit(PROCESSING_FINISHED, 'loadingCartProductEligible', { root: true })
      commit(SET_PWP_FOC_SELECTION_COMPLETE, false)
      commit(ESHOP_CART_SET_FOC_PWP, response.data)
    })
    .catch(e => {
      dispatch('logError', { cartProductEligible_loading: e }, { root: true })
      commit(PROCESSING_FINISHED, 'loadingCartProductEligible', { root: true })
    })
}

export const loadCartProductEligibleDetails = async ({ getters, commit, dispatch, rootGetters }) => {
  let products = []
  let kittings = []
  getters.getEligiblePromotion.forEach(promotion => {
    promotion.conditions.options.forEach(option => {
      Object.keys(option).forEach(optionKey => {
        Object.keys(option[optionKey]).forEach(setKey => {
          if (setKey !== 'option_id') {
            option[optionKey][setKey].forEach(product => {
              if (typeof product.product_id !== 'undefined' && product.product_id !== null) {
                products.push(product.product_id)
              }
              if (typeof product.kitting_id !== 'undefined' && product.kitting_id !== null) {
                kittings.push(product.kitting_id)
              }
            })
          }
        })
      })
    })
  })
  for (let i in products) {
    if (products[i] !== null && !getters.getProductOrKittingDetails(products[i])) {
      await dispatch('loadProductOrKittingDetails', {
        product_id: products[i],
        country_id: rootGetters.getSelectedCountry.id,
      })
    }
  }
  for (let i in kittings) {
    if (kittings[i] !== null && !getters.getProductOrKittingDetails(null, kittings[i])) {
      await dispatch('loadProductOrKittingDetails', {
        kitting_id: kittings[i],
        country_id: rootGetters.getSelectedCountry.id,
      })
    }
  }
  commit(PROCESSING_FINISHED, 'loadingCartProductEligibleDetails', { root: true })
}

export const setSelectedFocItems = ({ store, getters, commit }, data) => {
  commit(SET_SELECTED_FOC_ITEMS, data)
  commit(SET_SELECTED_FOCPWP_ITEMS, getters.getSelectedPromotionItems)
}

export const setSelectedPwpItems = ({ store, getters, commit }, data) => {
  commit(SET_SELECTED_PWP_ITEMS, data)
  commit(SET_SELECTED_FOCPWP_ITEMS, getters.getSelectedPromotionItems)
}

export const clearSelectedFocPwpItems = ({ commit }) => {
  commit(SET_SELECTED_FOCPWP_ITEMS, [])
}

export const setFocSelectionComplete = ({ store, getters, commit }, data) => {
  commit(SET_FOC_SELECTION_COMPLETE, data)
}

export const setPwpSelectionComplete = ({ store, getters, commit }, data) => {
  commit(SET_PWP_SELECTION_COMPLETE, data)
}

export const setPwpFocSelectionComplete = ({ store, getters, commit }, data) => {
  commit(SET_PWP_FOC_SELECTION_COMPLETE, data)
}
