import * as T from '@/store/utils/types'
import * as U from '@/store/utils/apiUrls'
import { post, patch, get, del } from '../../utils/api'
import querystring from 'querystring'
import _ from 'lodash'
import { getValue } from '@/helpers/variable-handling'

export const addCard = async ({ commit }, payload) => {
  commit(T.PROCESSING_STARTED, 'addingNewCard')
  return await post(U.AUTOSHIP_CREATE_CARD, payload)
    .then(response => {
      return response
    })
    .catch(error => {
      return false
    })
    .finally(() => {
      commit(T.PROCESSING_FINISHED, 'addingNewCard')
    })
}

export const loadCardList = async ({ commit }) => {
  commit(T.PROCESSING_STARTED, 'loadingCardList')
  await get(U.AUTOSHIP_GET_CARD_LIST + '?paginate=0')
    .then(response => {
      commit(T.UPDATE_CARD_LIST, response.data.data)
    })
    .catch(e => {})
    .finally(() => {
      commit(T.PROCESSING_FINISHED, 'loadingCardList')
    })
}

export const setPaymentInfoFormData = ({ commit }, payload) => {
  commit(T.PROCESSING_STARTED, 'savingPaymentInfo')
  commit(T.SET_SHIP_CYCLE, payload.shipCycle)
  commit(T.SET_PAYMENT_INFO, payload.paymentInfo)
  commit(T.PROCESSING_FINISHED, 'savingPaymentInfo')
}

export const setShippingInfo = ({ commit }, payload) => {
  commit(T.PROCESSING_STARTED, 'savingShippingInfo')
  commit(T.SET_SHIPPING_INFO, payload)
  commit(T.PROCESSING_FINISHED, 'savingShippingInfo')
}

export const setBillingInfo = ({ commit }, payload) => {
  commit(T.PROCESSING_STARTED, 'savingBillingInfo')
  commit(T.SET_BILLING_INFO, payload)
  commit(T.PROCESSING_FINISHED, 'savingBillingInfo')
}

export const createAutoshipSales = ({ commit, dispatch, getters }, payload) => {
  commit(T.PROCESSING_STARTED, 'createAutoshipSales')
  let query = {
    selected: {
      subscription: {
        recurring_frequency:
          getValue(payload, 'selected.subscription.recurring_frequency') ||
          getters.getShippingCycleDetail.billingCycle.value,
        subscription_length: -1,
        start_date: getValue(payload, 'selected.subscription.start_date') || getters.getShippingCycleDetail.billOn,
        card: getValue(payload, 'selected.subscription.card') || getters.getPaymentInfo,
      },
      shipping: getValue(payload, 'selected.shipping') || getters.getAutoshipShippingInfo,
      billing: getValue(payload, 'selected.shipping') || getters.getAutoshipBillingInfo,
      promotions: [],
      additional_requirements: getters['eshop/getCartIsAllDummiesSelectedForSales'],
    },
    country_id: payload.countryId,
    location_id: payload.locationId,
    sale_id: getValue(payload, 'sale_id', null),
  }
  return post(U.AUTOSHIP_CREATE_SALES, query)
    .then(response => {
      let data = response.data
      commit(T.CLEAR_LOADED_DATA, 'autoship')
      commit(T.AUTOSHIP_DATA, data)
      return data
    })
    .catch(e => {
      dispatch('logError', { doSalesCartCreateSale: e.data }, { root: true })
      return false
    })
    .finally(() => {
      commit(T.PROCESSING_FINISHED, 'createAutoshipSales')
    })
}

export const updateAutoshipSubscription = async ({ commit }, payload) => {
  commit(T.PROCESSING_STARTED, 'updatingAutoshipSubscription')
  return await patch(U.AUTOSHIP_UPDATE_SUBSCRIPTION + payload.subscription_id, payload)
    .then(response => {
      commit(T.SET_AUTOSHIP_SUBSCRIPTION_DETAIL, response.data)
      commit(T.CLEAR_LOADED_DATA, 'autoship')
      return response
    })
    .catch(e => {
      return false
    })
    .finally(() => {
      commit(T.PROCESSING_FINISHED, 'updatingAutoshipSubscription')
    })
}

export const loadAutoshipSubscriptionList = async ({ commit }, payload = { page: 1 }) => {
  commit(T.PROCESSING_STARTED, 'loadingSubscriptionList')
  // Reset Data
  commit(T.SET_AUTOSHIP_SUBSCRIPTION_LIST, [])
  commit(T.SET_SUBSCRIPTION_LIST_PAGINATION_META, { pagination: { total: 0, per_page: 20 } })

  let query = '?' + querystring.stringify(_.pickBy(payload, _.identity))
  await get(U.AUTOSHIP_GET_SUBSCRIPTION + query)
    .then(response => {
      commit(T.SET_AUTOSHIP_SUBSCRIPTION_LIST, response.data.data)
      commit(T.SET_SUBSCRIPTION_LIST_PAGINATION_META, response.data.meta)
    })
    .catch(e => {})
    .finally(() => {
      commit(T.PROCESSING_FINISHED, 'loadingSubscriptionList')
    })
}

export const loadAutoshipSubscriptionDetail = async ({ commit }, payload) => {
  commit(T.PROCESSING_STARTED, 'loadingSubscriptionDetail')
  await get(U.AUTOSHIP_GET_SUBSCRIPTION + '/' + payload)
    .then(response => {
      commit(T.SET_AUTOSHIP_SUBSCRIPTION_DETAIL, response.data)
    })
    .catch(e => {})
    .finally(() => {
      commit(T.PROCESSING_FINISHED, 'loadingSubscriptionDetail')
    })
}

export const unsubscribeAutoship = async ({ commit }, payload) => {
  commit(T.PROCESSING_STARTED, 'unsubscribingAutoship')
  return await del(U.AUTOSHIP_TERMINATE_SUBSCRIPTION + '/' + payload)
    .then(response => {
      return true
    })
    .catch(e => {
      return false
    })
    .finally(() => {
      commit(T.PROCESSING_FINISHED, 'unsubscribingAutoship')
    })
}

export const setAutoshipSearchFilter = ({ commit }, data) => {
  commit(T.SET_AUTOSHIP_SEARCH_FILTER, data)
}
