import _ from 'lodash'

export const namespaced = true
export const state = () => ({
  selected: [],
  purchases: [],
  setPurchases: []
})

export const mutations = {
  SET_SELECTED (state, payload) {
    state.selected = payload
  },
  ADD_SELECTED (state, payload) {
    const { id, __type, stockId, subdomain } = payload
    const quantity = Number(payload.quantity)
    const index = state.selected.findIndex((item) => item.id === id
      && item.stockId === stockId
      && item.subdomain === subdomain
      && item.__type === __type)
    if (index > -1) {
      state.selected[index].quantity += quantity
    } else {
      state.selected.push(payload)
    }
  },
  UPDATE_SELECTED (state, payload) {
    const { id, __type, stockId, subdomain } = payload
    const quantity = Number(payload.quantity)
    const selectedItems = _.cloneDeep(state.selected)
    const selected = selectedItems.find((item) => item.id === id
      && item.stockId === stockId
      && item.subdomain === subdomain
      && item.__type === __type)
    if (selected) {
      selected.quantity = quantity
      state.selected = selectedItems
    }
  },
  DELETE_SELECTED (state, payload) {
    const { id, __type, stockId, subdomain } = payload
    const index = state.selected.findIndex((item) => item.id === id
      && item.stockId === stockId
      && item.subdomain === subdomain
      && item.__type === __type)
    if (index !== -1) {
      state.selected.splice(index, 1)
    }
  },
  ADD_PURCHASES (state, payload) {
    const { id, stockId, subdomain } = payload
    const quantity = Number(payload.quantity)
    const index = state.purchases.findIndex((item) => item.id === id
      && item.stockId === stockId
      && item.subdomain === subdomain)
    if (index > -1) {
      state.purchases[index].quantity += quantity
    } else {
      state.purchases.push(payload)
    }
  },
  ADD_SETPURCHASES (state, payload) {
    const index = state.setPurchases.findIndex((item) => item.id === payload.id
      && item.stockId === payload.stockId
      && item.subdomain === payload.subdomain)
    if (index !== -1) {
      state.setPurchases[index].quantity += Number(payload.quantity)
    } else {
      state.setPurchases.push(payload)
    }
  },
  UPDATE_PURCHASES (state, payload) {
    const { id, stockId, subdomain } = payload
    const quantity = Number(payload.quantity)
    const purchases = _.cloneDeep(state.purchases)
    const purchase = purchases.find((item) => item.id === id
      && item.stockId === stockId
      && item.subdomain === subdomain)
    if (purchase) {
      if (quantity < purchase.available) {
        purchase.quantity = Number(quantity || 1)
      } else {
        purchase.quantity = Number(purchase.available)
      }
      state.purchases = purchases
    }
  },
  UPDATE_SETPURCHASES (state, payload) {
    const { id, stockId, subdomain } = payload
    const quantity = Number(payload.quantity)
    const setPurchases = _.cloneDeep(state.purchases)
    const setPurchase = setPurchases.find((item) => item.id === id
      && item.stockId === stockId
      && item.subdomain === subdomain)
    if (setPurchase) {
      setPurchase.quantity = Number(quantity || 1)
      state.setPurchases = setPurchases
    }
  },
  DELETE_PURCHASES (state, payload) {
    const { id, stockId, subdomain } = payload
    const index = state.purchases.findIndex((item) => item.id === id
      && item.stockId === stockId
      && item.subdomain === subdomain)
    if (index !== -1) {
      state.purchases.splice(index, 1)
    }
  },
  DELETE_SETPURCHASES (state, payload) {
    const { id, stockId, subdomain } = payload
    const index = state.setPurchases.findIndex((item) => item.id === id
      && item.stockId === stockId
      && item.subdomain === subdomain)
    if (index !== -1) {
      state.setPurchases.splice(index, 1)
    }
  },
  CLEAR_CART (state) {
    state.selected = []
    state.purchases = []
    state.setPurchases = []
  }
}

export const actions = {
  setSelected ({ commit }, payload) {
    commit('SET_SELECTED', payload)
  },
  addToCart ({ commit, rootState }, payload) {
    const {
      id,
      name,
      pictureUrl,
      price,
      available,
      isCollection,
      quantity
    } = payload
    if (available < quantity) { return }
    const stockId = rootState.salePage.stockId
    const subdomain = rootState.salePage.subdomain
    const commitName = isCollection ? 'ADD_SETPURCHASES' : 'ADD_PURCHASES'
    const __typename = isCollection ? 'SetPurchase' : 'Purchase'
    commit(commitName, {
      id,
      __typename,
      name,
      stockId,
      subdomain,
      pictureUrl,
      available,
      isCollection,
      price,
      quantity
    })
    commit('ADD_SELECTED', {
      id,
      __typename,
      name,
      stockId,
      subdomain,
      pictureUrl,
      available,
      isCollection,
      price,
      quantity
    })
    commit('salePage/SET_CHECKOUT', {
      promocode: {
        code: '',
        claimable: null,
        discountType: null,
        discountValue: 0,
        minRedeemType: null,
        minRedeemValue: 0
      }
    }, { root: true })
  },
  updatePurchases ({ commit, rootState }, payload) {
    const stockId = rootState.salePage.stockId
    const subdomain = rootState.salePage.subdomain
    commit('UPDATE_PURCHASES', {
      ...payload,
      stockId,
      subdomain
    })
    commit('UPDATE_SELECTED', {
      ...payload,
      stockId,
      subdomain
    })
    commit('salePage/SET_CHECKOUT', {
      promocode: {
        code: '',
        claimable: null,
        discountType: null,
        discountValue: 0,
        minRedeemType: null,
        minRedeemValue: 0
      }
    }, { root: true })
  },
  updateSetPurchases ({ commit, rootState }, payload) {
    const stockId = rootState.salePage.stockId
    const subdomain = rootState.salePage.subdomain
    commit('UPDATE_SETPURCHASES', {
      ...payload,
      stockId,
      subdomain
    })
    commit('UPDATE_SELECTED', {
      ...payload,
      stockId,
      subdomain
    })
    commit('salePage/SET_CHECKOUT', {
      promocode: {
        code: '',
        claimable: null,
        discountType: null,
        discountValue: 0,
        minRedeemType: null,
        minRedeemValue: 0
      }
    }, { root: true })
  },
  deletePurchases ({ commit, rootState }, payload) {
    const stockId = rootState.salePage.stockId
    const subdomain = rootState.salePage.subdomain
    commit('DELETE_PURCHASES', {
      ...payload,
      stockId,
      subdomain
    })
    commit('DELETE_SELECTED', {
      ...payload,
      stockId,
      subdomain
    })
    commit('salePage/SET_CHECKOUT', {
      promocode: {
        code: '',
        claimable: null,
        discountType: null,
        discountValue: 0,
        minRedeemType: null,
        minRedeemValue: 0
      }
    }, { root: true })
  },
  deleteSetPurchases ({ commit, rootState }, payload) {
    const stockId = rootState.salePage.stockId
    const subdomain = rootState.salePage.subdomain
    commit('DELETE_SETPURCHASES', {
      ...payload,
      stockId,
      subdomain
    })
    commit('DELETE_SELECTED', {
      ...payload,
      stockId,
      subdomain
    })
    commit('salePage/SET_CHECKOUT', {
      promocode: {
        code: '',
        claimable: null,
        discountType: null,
        discountValue: 0,
        minRedeemType: null,
        minRedeemValue: 0
      }
    }, { root: true })
  },
  clearCart ({ commit }) {
    commit('CLEAR_CART')
    commit('salePage/SET_CHECKOUT', {
      promocode: {
        code: '',
        claimable: null,
        discountType: null,
        discountValue: 0,
        minRedeemType: null,
        minRedeemValue: 0
      }
    }, { root: true })
  }
}

export const getters = {
  purchases: (state, gts, rootState) => {
    const stockId = rootState.salePage.stockId
    const subdomain = rootState.salePage.subdomain
    return state.purchases.filter((item) => item.stockId === stockId && item.subdomain === subdomain)
  },
  setPurchases: (state, gts, rootState) => {
    const stockId = rootState.salePage.stockId
    const subdomain = rootState.salePage.subdomain
    return state.setPurchases.filter((item) => item.stockId === stockId && item.subdomain === subdomain)
  },
  selected: (state, gts, rootState) => {
    const stockId = rootState.salePage.stockId
    const subdomain = rootState.salePage.subdomain
    return state.selected.filter((item) => item.stockId === stockId && item.subdomain === subdomain)
  },
  cart: (state, gts) => _.concat(gts.purchases, gts.setPurchases),
  cartQuantity: (state, gts) => gts.cart.reduce((total, cartItem) => total + cartItem.quantity, 0),
  purchasesQuantity: (state, gts) => gts.cart.reduce((total, cartItem) => total + cartItem.quantity, 0)
}

export default {
  namespaced,
  state,
  actions,
  mutations,
  getters
}
