import { ENROL_ADDRESS, LOGGED_IN_USER } from '@/store/utils/localStorageKeys'
import Vue from 'vue'
import Helpers from '@/helpers'
import appSettings from '@/config/app-settings'
import _ from 'lodash'

export default {
  getSponsorListing: state => state.sponsorMemberDetails || [],
  getSponsorDetails: (state, getters, rootState, rootGetters) => {
    let member = {
      name: '',
      member_id: '',
      user_id: '',
      ic_passport_number: '',
      country_id: null,
      is_empty: state.sponsorMemberDetails !== false,
    }
    if (state.sponsorMemberDetails !== false && state.sponsorMemberDetails.length > 0) {
      member = {
        name: state.sponsorMemberDetails[0].name,
        member_id: state.sponsorMemberDetails[0].old_member_id,
        user_id: state.sponsorMemberDetails[0].user_id,
        ic_passport_number: state.sponsorMemberDetails[0].ic_passport_number,
        country_id: state.sponsorMemberDetails[0].country_id,
        country: rootGetters.getCountries.filter(x => x.value === state.sponsorMemberDetails[0].country_id),
      }
    }
    return member
  },
  getBankCardDetails: state => {
    let bankCardDetails = _.cloneDeep(state.bankCardDetails)
    return bankCardDetails
  },
  getmPayDetails: state => {
    let mPayDetails = _.cloneDeep(state.mPayDetails)
    return mPayDetails
  },
  getRentalCardDetails: (state, getters, rootState, rootGetters) => {
    let stateLocation = 'rentalCardDetails'
    if (rootGetters.getAppStatus('eshop') === true) {
      stateLocation = 'rentalCardDetailsEshop'
    }
    let rentalCardData = _.cloneDeep(state[stateLocation])
    return rentalCardData
  },
  getSupportingDocDetails: state => {
    let supportingDoc = _.cloneDeep(state.supportingDocDetails)
    return supportingDoc
  },
  getNRICDocDetails: state => {
    let nricDoc = _.cloneDeep(state.nricDocsDetails)
    return nricDoc
  },
  getSelectedRecurringInfoType: state => {
    let value = state.selectedRecurringInfoType
    value = value.replace(/["']/g, '')

    return value
  },
  getRentalCardData: (state, getters, rootState, rootGetters) => {
    let rentalState = 'rentalCardDetails'
    if (rootGetters.getAppStatus('eshop') === true) {
      rentalState = 'rentalCardDetailsEshop'
    }
    let data = _.cloneDeep(state[rentalState])
    let rentalCardData = {
      card_type_id: data.card_type_id,
      card_company_id: data.card_company_id,
      country_bank_id: data.country_bank_id,
      card_holder_name: data.card_holder_name,
      card_number: data.card_number,
      credit_card_photocopy: data.credit_card_photocopy,
      income_proof: data.income_proof,
      card_expiry_date: data.card_expiry_date,
    }
    return rentalCardData
  },
  getRentalCardDataPayload: (state, getters, rootState, rootGetters) => {
    let rentalState = 'rentalCardDetails'
    if (rootGetters.getAppStatus('eshop') === true) {
      rentalState = 'rentalCardDetailsEshop'
    }

    let data = _.cloneDeep(state[rentalState])

    let payload = _.cloneDeep(getters.getCardRecurringInfoFields)

    if (payload !== undefined && payload !== null && payload.recurring_info !== undefined) {
      if (getters.getSelectedRecurringInfoType === 'GIRO') {
        payload.selected = payload.recurring_info[1].id
      } else {
        let rentalCardData = {
          'Card Type': data.card_type_id,
          'Card Network': data.card_company_id,
          'Issuing Bank': data.country_bank_id,
          'Name on Card': data.card_holder_name,
          'Card No': data.card_number,
          'Credit/ Debit Card': data.credit_card_photocopy,
          'Expiry Date': data.card_expiry_date,
        }
        payload.selected = payload.recurring_info[0].id
        payload.recurring_info[0].fields = payload.recurring_info[0].fields.map(field => {
          for (let key in rentalCardData) {
            let regex = new RegExp(key, 'gi')
            if (regex.test(field.label) === true) {
              field.value = rentalCardData[key]
            }
          }

          return field
        })
      }
    }

    return payload
  },
  getCreditCardPhotoCopy: (state, getters) => {
    let data = getters.getRentalCardDetails
    return data.credit_card_photocopy
  },
  getIncomeProof: (state, getters) => {
    let data = getters.getRentalCardDetails
    return data.income_proof
  },
  getApplicationForm: state => {
    let applicationForm = []
    if (typeof state.enrolForm.supportingDocs !== 'undefined') {
      applicationForm = state.enrolForm.supportingDocs
    }
    return applicationForm
  },
  getUpgradeDetails: state => {
    let member = {}
    if (state.upgradeMemberDetails !== null && state.upgradeMemberDetails.length > 0) {
      member = {
        name: state.upgradeMemberDetails[0].name,
        member_id: state.upgradeMemberDetails[0].old_member_id,
        user_id: state.upgradeMemberDetails[0].user_id,
      }
    }
    return member
  },
  getPlacementMemberDetails: state => {
    let member = {}
    if (state.placementMemberDetails !== null && state.placementMemberDetails.length > 0) {
      member = {
        name: state.placementMemberDetails[0].name,
        member_id: state.placementMemberDetails[0].old_member_id,
        user_id: state.placementMemberDetails[0].user_id,
      }
    }
    return member
  },
  // Sage9
  getBankList: state => {
    return state.bankList
  },
  // Sage 9
  getBankSwiftCode: state => bankId => {
    if (state.bankList !== null) {
      let res = ''
      state.bankList.forEach(item => {
        if (parseInt(item.bankId) === parseInt(bankId)) {
          res = item.bankSwiftCode
        }
      })
      return res
    }
  },
  getEnrolForm: state => _.cloneDeep(state.enrolForm),
  getEnrolAddress: () => {
    return JSON.parse(localStorage.getItem(ENROL_ADDRESS))
  },
  getVerificationResponse: state => {
    return state.verification.get
  },
  postVerificationResponse: state => {
    return state.verification.post
  },
  getEnrolmentTypes: state => {
    return state.enrolmentTypes
  },
  getIsExpressEnrollment: state => id => {
    // Disable express enrolment
    if (appSettings.enrolment.disableExpressEnrolment) {
      return false
    }
    // retail customer enrolment type the id always null in front-end
    if (id === null) {
      return true
    } else {
      let type = state.enrolmentTypes.find(type => type.id === id)
      return type && type.is_express_checkout
    }
  },
  getLoggedInSponsor: state => {
    if (localStorage.getItem(LOGGED_IN_USER) !== null) {
      return JSON.parse(localStorage.getItem(LOGGED_IN_USER)).id
    }
  },
  getPlacementVerification: state => {
    return state.memberPlacementVerify
  },
  getEnrolGuideStatus: state => {
    return state.guide
  },
  getEnrolCountryId: state => {
    return state.country_id
  },
  getEnrolProductMainCategories: state => {
    let mainCategories = state.categories
      .filter(data => data.parent_id === 1)
      .map(category => ({
        value: category.id,
        text: Helpers.getValue(category, 'translated_name.content', category.name),
      }))

    return [{ value: null, text: Vue.prototype.$t('Common.AllCategories.Title') }, ...mainCategories]
  },
  getTempEnrolForm: state => {
    let temp = {}
    if (
      state.tempEnrolmentData !== null &&
      state.tempEnrolmentData.temp_data !== undefined &&
      state.tempEnrolmentData.temp_data !== ''
    ) {
      temp = JSON.parse(state.tempEnrolmentData.temp_data)
    }
    return temp
  },
  getTempSmsCode: state => {
    let code = ''
    if (
      state.tempEnrolmentData !== null &&
      state.tempEnrolmentData.sms_code !== undefined &&
      state.tempEnrolmentData.sms_code !== ''
    ) {
      code = state.tempEnrolmentData.sms_code
    }
    return code
  },
  getUserName: state => Helpers.getValue(state, 'enrolForm.member_data.details.name'),
  getEnrolTaiwanFiles: state => state.enrolmentTaiwanFiles,
  getEnrolmentLanguage: state => state.selectedEnrolLanguage,
  getIsTabActive: state => {
    return state.enrolmentTabStatus.disableChecker
      ? true
      : state.enrolmentTabStatus.activeTabId !== false &&
          state.enrolmentTabStatus.activeTabId === parseInt(sessionStorage.tabId)
  },
  getMembershipType: state => state.membership_type,
  getMembershipTypes: state => state.membershipTypes,
  getBankFormSelect: state => {
    let result = []
    if (state.bankList !== null) {
      state.bankList.forEach(item => {
        result.push({
          value: item.bankId,
          text: item.bankName,
        })
      })
    }
    return result
  },
  getSalaryRangeSelect: state => {
    return (state.salaryRanges || []).map(range => ({
      value: range.salaryRangeId,
      text: range.salaryRangeName,
    }))
  },
  getCardRecurringInfoFields: state => state.cardRecurringInfoFields,
  getPayeeInfoFields: state => state.payeeInfoFields,
  getEmploymentDetailsFields: state => {
    return state.employmentDetailsFields
  },
  getEmploymentFormStatus: (state, getters, rootState, rootGetters) => {
    let platform =
      (rootGetters.getAppStatus('eshop') && 'eshop') || (rootGetters.getAppStatus('enrolment') && 'enrolment')
    if (state.employmentDetailsFields !== null && platform) {
      return state.employmentFormStatus[platform]
    } else {
      return true
    }
  },
  getEnrolProductSearchFilter: state => state.productSearchFilter,
}
