import {
  CONFIRM_ORDER_STEP,
  CANCEL_ORDER_STEP,
  SET_COLLECTION_POINT_STATE,
  SET_COLLECTION_POINT_CITY,
  SET_EWALLET_PAYMENT,
  SET_SHIPPING_METHOD_USED,
  SET_PAYMENT_METHOD_USED,
  SET_RECIPIENT_DETAIL,
  SET_SHIPPING_ADDRESS,
  ESHOP_ORDER_SET_SHIPPING_DATA,
  PROCESSING_STARTED,
  PROCESSING_FINISHED,
  SET_PAYMENT_DOCUMENT_DETAILS,
  SET_PAYMENT_STEP_STATUS,
} from '../../../utils/types'
import { PAYMENT_DOCUMENT_DETAILS } from '../../../utils/apiUrls'
import router from '@/router'
import { post } from '../../../utils/api'
import _ from 'lodash'
import helpers from '@/helpers'

/**
 * Save Order Shipping Data
 *
 * @param commit
 * @param {Object} payload
 * @param {int} payload.sales_delivery_method_id // Master data sales delivery method id
 * @param {int} [payload.self_collection_point_id] // Required if delivery method is self collection
 * @param {string[]} [payload.recipient_addresses] // Address string
 * @param {int} [payload.recipient_mobile_country_code_id] // mobile country id
 * @param {int} [payload.recipient_mobile_phone_number] // mobile phone
 * @param {int} [payload.recipient_name] // recipient name
 * @param {int} [payload.recipient_selected_shipping_index] // shipping indes to use from the address
 */
export const saveOrderShipping = ({ commit }, payload) => {
  commit(ESHOP_ORDER_SET_SHIPPING_DATA, payload)
}

export const setOrderStep = ({ commit }, data) => {
  commit(CONFIRM_ORDER_STEP, data)
}

export const cancelOrderStep = ({ commit }, data) => {
  commit(SET_EWALLET_PAYMENT, 0)
  if (data === '') {
    router.go(-1)
  } else {
    commit(CANCEL_ORDER_STEP, data)
  }
}

export const setCollPointState = ({ commit }, data) => {
  commit(SET_COLLECTION_POINT_STATE, data)
}

export const setCollPointCity = ({ commit }, data) => {
  commit(SET_COLLECTION_POINT_CITY, data)
}

export const setEwalletAmount = ({ commit }, data) => {
  commit(SET_EWALLET_PAYMENT, data)
}

export const setShippingMethodUsed = ({ commit }, data) => {
  commit(SET_SHIPPING_METHOD_USED, data)
}

export const setPaymentMethodUsed = ({ commit }, data) => {
  commit(SET_PAYMENT_METHOD_USED, data)
}

export const addShippingAddress = ({ store, getters, commit }, data) => {
  let addresses = getters.getExistingAddress
  addresses.push(data)
  commit(SET_SHIPPING_ADDRESS, addresses)
}

export const removeShippingAddress = ({ store, getters, commit }, data) => {
  let addresses = getters.getExistingAddress
  for (let index = addresses.length - 1; index >= 0; index--) {
    if (addresses[index].addressId === data) {
      addresses.splice(index, 1)
    }
  }
  commit(SET_SHIPPING_ADDRESS, addresses)
}

export const setRecipientDetail = ({ commit }, data) => {
  commit(SET_RECIPIENT_DETAIL, data)
}
export const setPaymentStepStatus = ({ commit }, data) => {
  commit(SET_PAYMENT_STEP_STATUS, data)
}

export const fetchPaymentDocumentDetails = async ({ commit, dispatch, rootGetters }, payload) => {
  commit(PROCESSING_STARTED, 'loadingPaymentDocumentDetails', { root: true })
  let cancelToken = await dispatch('generateCancelToken', 'loadingPaymentDocumentDetails', { root: true })
  return post(
    PAYMENT_DOCUMENT_DETAILS,
    _.defaults(payload, {
      country_id: helpers.getValue(rootGetters, 'getSelectedCountry.id'),
    }),
    cancelToken.token
  )
    .then(response => {
      let details = response.data
      commit(PROCESSING_FINISHED, 'loadingPaymentDocumentDetails', { root: true })
      commit(SET_PAYMENT_DOCUMENT_DETAILS, details)
    })
    .catch(e => {
      dispatch('logError', { payment_document_details_loading: e }, { root: true })
      commit(PROCESSING_FINISHED, 'loadingPaymentDocumentDetails', { root: true })
    })
}
