import {
  ESHOP_CART_SET_DETAILS,
  ESHOP_CART_SET_PRODUCT_DUMMY,
  ESHOP_CART_SET_INST_ADDRESS_PRODUCT_DUMMY,
  ESHOP_CART_CLEAR_PRODUCT_DUMMY,
  ESHOP_CART_CLEAR_INST_ADDRESS_PRODUCT_DUMMY,
  ESHOP_CART_CLEAR_LOCAL,
  CLEAR_LOADED_DATA,
  ESHOP_SHOW_PRODUCT_DETAIL_MODAL,
  ESHOP_CART_SET_ADD_PRODUCT,
  ESHOP_CART_ORDER_FOR_DOWNLINE,
  ESHOP_SET_CART_NAME,
  SET_CART_PRESERVE_STATE,
} from '../../../utils/types'
import {
  ESHOP_CART_SELECTED_DUMMY_IDS,
  ESHOP_CART_SELECTED_INST_ADDRESS_DUMMY_IDS,
  ESHOP_CART_IS_ORDERING_FOR_DOWNLINE,
  ESHOP_CART_ORDERING_FOR,
  CART_NAME,
} from '../../../utils/localStorageKeys'
import defaultStore from './index'

export default {
  [ESHOP_CART_SET_DETAILS](state, details) {
    let items = details.data
    if (items !== undefined) {
      items.forEach(item => {
        if (item.image === null || item.image === '') {
          item.image = '/static/products/img-placeholder.svg'
        }
      })
      state.products = items
      state.totalQuantity = details.total_quantity
      state.totalCv = details.total_cv
      state.totalPrice = details.total_price
      state.allTotalPrices = details.total_prices
      state.total_admin_fee = details.total_admin_fee
      state.total_admin_fee_formatted = details.total_admin_fee_formatted
      state.total_delivery_fee = details.total_delivery_fee
      state.total_delivery_fee_formatted = details.total_delivery_fee_formatted
      state.total_npm = details.total_npm
      state.total_npm_formatted = details.total_npm_formatted
      state.total_price = details.total_price
      state.total_price_formatted = details.total_price_formatted
      state.total_price_rounding_adjustment = parseInt(details.total_price_rounding_adjustment)
      state.total_price_rounding_adjustment_formatted = details.total_price_rounding_adjustment_formatted
      state.total_tax = details.total_tax
      state.total_tax_formatted = details.total_tax_formatted
      state.loaded = true
      state.promotions = details.promotions
      state.has_epp = details.has_epp
      state.has_rental = details.has_rental
      state.has_ha = details.has_ha
      state.need_address_selection = details.need_address_selection
      state.need_size_selection = details.need_size_selection
      state.cartPayCycle = details.sv_month || ''
      state.cartCampaignDetails = details.campaign
      if (details.checkout.errors === undefined) {
        state.checkout.errors = {}
      }
      state.checkout = { ...state.checkout, ...details.checkout }
    }
  },
  [ESHOP_CART_SET_PRODUCT_DUMMY](state, details) {
    if (!details.name) {
      details.name = state.cartName
    }
    state.selectedDummies = { ...state.selectedDummies, ...details }
    let localStorageData = JSON.parse(localStorage.getItem(ESHOP_CART_SELECTED_DUMMY_IDS))
    if (localStorageData === null) {
      localStorage.setItem(ESHOP_CART_SELECTED_DUMMY_IDS, JSON.stringify([state.selectedDummies]))
    } else {
      let selectedIndex = localStorageData.findIndex(x => x.name === state.selectedDummies.name)
      if (selectedIndex !== -1) {
        localStorageData.splice(selectedIndex, 1)
      }
      localStorageData.push(state.selectedDummies)
      localStorage.setItem(ESHOP_CART_SELECTED_DUMMY_IDS, JSON.stringify(localStorageData))
    }
  },
  [ESHOP_CART_SET_INST_ADDRESS_PRODUCT_DUMMY](state, details) {
    if (!details.name) {
      details.name = state.cartName
    }
    state.selectedDummiesInstDetails = { ...state.selectedDummiesInstDetails, ...details }
    let localStorageData = JSON.parse(localStorage.getItem(ESHOP_CART_SELECTED_INST_ADDRESS_DUMMY_IDS))
    if (localStorageData === null) {
      localStorage.setItem(
        ESHOP_CART_SELECTED_INST_ADDRESS_DUMMY_IDS,
        JSON.stringify([state.selectedDummiesInstDetails])
      )
    } else {
      let selectedIndex = localStorageData.findIndex(x => x.name === state.selectedDummiesInstDetails.name)
      if (selectedIndex !== -1) {
        localStorageData.splice(selectedIndex, 1)
      }
      localStorageData.push(state.selectedDummiesInstDetails)
      localStorage.setItem(ESHOP_CART_SELECTED_INST_ADDRESS_DUMMY_IDS, JSON.stringify(localStorageData))
    }
  },
  [ESHOP_CART_CLEAR_PRODUCT_DUMMY](state) {
    let selectedIndex = false
    let localStorageData = JSON.parse(localStorage.getItem(ESHOP_CART_SELECTED_DUMMY_IDS))
    if (localStorageData) {
      selectedIndex = localStorageData.findIndex(x => x.name === state.cartName)
    }
    if (selectedIndex !== false && selectedIndex < 0) {
      localStorageData.splice(selectedIndex, 1)
      state.selectedDummies = localStorageData
    } else {
      state.selectedDummies = defaultStore.state.selectedDummies
    }
  },
  [ESHOP_CART_CLEAR_INST_ADDRESS_PRODUCT_DUMMY](state) {
    let selectedIndex = false
    let localStorageData = JSON.parse(localStorage.getItem(ESHOP_CART_SELECTED_INST_ADDRESS_DUMMY_IDS))
    if (localStorageData) {
      selectedIndex = localStorageData.findIndex(x => x.name === state.cartName)
    }
    if (selectedIndex !== false && selectedIndex < 0) {
      localStorageData.splice(selectedIndex, 1)
      state.selectedDummiesInstDetails = localStorageData
    } else {
      state.selectedDummiesInstDetails = defaultStore.state.selectedDummiesInstDetails
    }
  },
  [ESHOP_CART_CLEAR_LOCAL](state) {
    state.products = []
    state.loaded = false
  },
  [ESHOP_CART_ORDER_FOR_DOWNLINE](state, user) {
    state.isOrderingForDownLine = !!user
    state.orderingFor = user
    if (state.orderingFor !== null) {
      if (state.orderingFor.address !== null) {
        state.orderingFor.address.address_data = JSON.parse(state.orderingFor.address.address_data)
      } else {
        state.orderingFor.address = {
          user_id: state.orderingFor.details.user_id,
          address_data: [],
        }
      }
    }
    localStorage.setItem(ESHOP_CART_IS_ORDERING_FOR_DOWNLINE, JSON.stringify(state.isOrderingForDownLine))
    localStorage.setItem(ESHOP_CART_ORDERING_FOR, JSON.stringify(state.orderingFor))
  },
  [CLEAR_LOADED_DATA](state, type) {
    if (type === 'cart' || !type) {
      state = defaultStore.state
      localStorage.removeItem(ESHOP_CART_SELECTED_DUMMY_IDS)
    }
  },
  [ESHOP_SHOW_PRODUCT_DETAIL_MODAL](state, payload) {
    state.showProductDetailModal = payload
  },
  [ESHOP_CART_SET_ADD_PRODUCT](state) {},
  [ESHOP_SET_CART_NAME](state, payload) {
    state.cartName = payload
    localStorage.setItem(CART_NAME, payload)
  },
  [SET_CART_PRESERVE_STATE](state, payload) {
    state.preserverCartItem = payload
  },
}
