import { APP_ENROLMENT_FORM, SKIP_ENROLMENT_PRIVILEGE, SKIP_ENROLMENT_STATUS } from '@@/store/localStorageKeys'
import { SET_ENROLMENT_FORM, SET_SKIP_ENROLMENT_PRIVILEGE, SET_SKIP_ENROLMENT_STATUS } from '@@/store/types'
import _ from 'lodash'
import { getValue } from '@/helpers/variable-handling'
import * as T from '@/store/utils/types'
import { post } from '@/store/utils/api'
import { MEMBER_SPONSOR_SEARCH_WITH_REFERENCE } from '@/store/utils/apiUrls'

let defaultEnrolmentForm = {
  membershipType: 'Standard',
  temp_save: 1,
  global: 1,
  region: null,
  language: null,
  enrolment_type: '',
  sponsor_member_id: '',
  sponsor_name: '',
  sponsor_user_id: '',
  without_sponsor: 0,
  enable_resume_sms_code: false,
  on_behalf_enrolment: 0,
  agreement: '',
  agreement_1: '',
  verification: {
    code: '',
    get: {},
    post: {},
  },
  sales_data: {},
  member_data: {
    details: {
      nationality_id: '',
      name: '',
      translated_name: '',
      ic_pass_type_id: '',
      ic_passport_number: '',
      date_of_birth: '',
      tin_no_taiwan: '',
      tin_no_philippines: '',
      is_cross_sponsor_error: 0,
      occupation: '',
      salary_range: '',
      country_id: 1,
    },
    information: {
      gender_id: '',
      language_id: '',
      ethnic_group_id: '',
      martial_status_id: '',
    },
    verification: [],
    contact_info: {
      preferred_contact_id: '',
      email: '',
      mobile_1_country_code_id: null,
      mobile_1_num: '',
      tel_office_1_country_code_id: null,
      tel_office_1_num: '',
      tel_home_1_country_code_id: null,
      tel_home_1_num: '',
    },
    address: {
      address_data: [],
    },
  },
  recurring_info: {
    card: {
      card_type_id: '',
      card_company_id: '',
      expiry_date: '',
      card_number: '',
      issued_bank: '',
      name_on_card: '',
      card_type: '',
      cardDocuments: [],
      otherDocuments: [],
    },
  },
  payee_info: {
    mpay: {
      card_country__name: '',
      card_holder_name: '',
      card_number: '',
      expiry_date: '',
      expiry_year: '',
    },
    bank: {
      bank_country_name: '',
      bank_id: '',
      bank_account_ic: '',
      payout_comapny: '',
      swift_code: '',
      account_number: '',
      account_type: '',
      account_holder_name: '',
    },
  },
  placement: {
    placement_position: 0,
    placement_member_user_id: '',
    placement_member_id: '',
    placement_member_name: '',
    placement_same_as_sponsor: 0,
  },
  supportingDocs: [],
  created_by: null,
  status: false,
}

export default {
  namespaced: true,
  state: {
    [APP_ENROLMENT_FORM]: JSON.parse(sessionStorage.getItem(APP_ENROLMENT_FORM)) || defaultEnrolmentForm,
    skipEnrolmentPrivilege: JSON.parse(sessionStorage.getItem(SKIP_ENROLMENT_PRIVILEGE)) || false,
    skipEnrolmentStatus: JSON.parse(sessionStorage.getItem(SKIP_ENROLMENT_STATUS)) || false,
  },
  getters: {
    getEnrolmentFormData: state => _.cloneDeep(state[APP_ENROLMENT_FORM]),
    getEnrolmentInfo: (state, getters, rootState, rootGetters) => {
      let membershipSelection = rootGetters['3rdPartyCheckoutMembership/getMembershipInfo']
      let result = _.cloneDeep(state[APP_ENROLMENT_FORM])

      result.region = getValue(membershipSelection, 'country[0]', null)
      result.enrolment_type = getValue(membershipSelection, 'enrolment_type_id', null)
      result.sponsor_name = getValue(membershipSelection, 'sponsor.sponsor_name', null)
      result.sponsor_user_id = getValue(membershipSelection, 'sponsor.sponsor_user_id', null)
      result.sponsor_member_id = getValue(membershipSelection, 'sponsor.sponsor_member_id', null)
      result.member_data.details.country_id = getValue(membershipSelection, 'country_id', null)

      return result
    },
    getSkipEnrolmentPrivilege: state => state.skipEnrolmentPrivilege,
    getSkipEnrolmentStatus: state => state.skipEnrolmentStatus,
  },
  actions: {
    [APP_ENROLMENT_FORM]({ commit }, payload) {
      commit(SET_ENROLMENT_FORM, payload)
    },
    setSkipEnrolmentPrivilege({ commit }, payload) {
      // note: Changes on this value will affect Retail Price display status
      commit(SET_SKIP_ENROLMENT_PRIVILEGE, payload)
    },
    setSkipEnrolmentStatus({ commit }, payload) {
      commit(SET_SKIP_ENROLMENT_STATUS, payload)
    },
    async setExternalSponsorDetails({ commit }, payload) {
      return post(MEMBER_SPONSOR_SEARCH_WITH_REFERENCE, payload).then(response => {
        if (response && response.data) {
          return {
            sponsorName: response.data.name,
            sponsorMemberId: response.data.old_member_id,
            sponsorId: response.data.user_id,
            withoutSponsor: 0,
          }
        } else {
          return { withoutSponsor: 1 }
        }
      })
    },
  },
  mutations: {
    [SET_ENROLMENT_FORM](state, data) {
      state[APP_ENROLMENT_FORM] = _.cloneDeep(data)
    },
    [SET_SKIP_ENROLMENT_PRIVILEGE](state, data) {
      state.skipEnrolmentPrivilege = data
      sessionStorage.setItem(SKIP_ENROLMENT_PRIVILEGE, JSON.stringify(data))
    },
    [SET_SKIP_ENROLMENT_STATUS](state, data) {
      state.skipEnrolmentStatus = data
      sessionStorage.setItem(SKIP_ENROLMENT_STATUS, JSON.stringify(data))
    },
    [T.CLEAR_LOADED_DATA](state, type) {
      if (type === 'enrollment' || !type) {
        state[APP_ENROLMENT_FORM] = defaultEnrolmentForm
        state.skipEnrolmentPrivilege = false
        state.skipEnrolmentStatus = false
        sessionStorage.removeItem(APP_ENROLMENT_FORM)
        sessionStorage.setItem(SKIP_ENROLMENT_PRIVILEGE, JSON.stringify(false))
      }
    },
  },
}
