import {
  SET_COUNTRIES_LIST,
  SET_BANK_LIST,
  SET_MY_ACCOUNT_MEMBER_DETAILS,
  SET_COUNTRY_DINAMIC_CONTENT,
  SET_MY_ACCOUNT_ETHNIC_GROUP,
  SET_MY_ACCOUNT_MARITAL_STATUS,
  SET_MY_ACCOUNT_GENDER_LIST,
  UPDATE_MY_ACCOUNT_MEMBER_DETAILS,
  UPDATE_MY_ACCOUNT_MEMBER_DETAILS_ADDRESS,
  SET_MY_ACCOUNT_CITIES,
  SET_MY_ACCOUNT_AVATAR,
  SET_MY_ACCOUNT_PREFERRED_CONTACT,
  SET_MY_ACCOUNT_PLACEMENT_PREFERENCE,
  SET_MEMBER_AMP_ACCUMULATIONS,
} from '../../utils/types'
import lodash from 'lodash'
import {
  MY_ACCOUNT_ADDRESS_DINAMIC_CONTENT,
  MY_ACCOUNT_BANK_INFO_DINAMIC_CONTENT,
  MY_ACCOUNT_TAX_PAYER_INFO,
} from '../../utils/localStorageKeys'
import { getValue } from '@/helpers/variable-handling'

export default {
  // TODO: move to member
  [SET_MY_ACCOUNT_PREFERRED_CONTACT](state, data) {
    state.preferredContactList = data
  },
  // TODO: move to member
  [SET_MY_ACCOUNT_AVATAR](state, data) {
    state.memberDetails.details.avatar_image_link = data
  },
  // TODO: use master data key mapper mixin
  [SET_MY_ACCOUNT_CITIES](state, data) {
    state.citiesList = data
  },
  // TODO: use SET_MY_ACCOUNT_MEMBER_DETAILS mutation
  [UPDATE_MY_ACCOUNT_MEMBER_DETAILS_ADDRESS](state, data) {
    state.memberDetails = lodash.assign({}, state.memberDetails, data)
  },
  // TODO: use SET_MY_ACCOUNT_MEMBER_DETAILS mutation
  [UPDATE_MY_ACCOUNT_MEMBER_DETAILS](state, data) {
    state.memberDetails = lodash.merge(state.memberDetails, data)
    state.memberDetails.verification = data.verification
  },
  // TODO: use master data key mapper mixin
  [SET_MY_ACCOUNT_GENDER_LIST](state, data) {
    state.genderList = data
  },
  // TODO: use master data key mapper mixin
  [SET_MY_ACCOUNT_MARITAL_STATUS](state, data) {
    state.maritalStatusList = data
  },
  // TODO: use master data key mapper mixin
  [SET_MY_ACCOUNT_ETHNIC_GROUP](state, data) {
    state.ethnicGroupList = data
  },
  // TODO: move this to location
  [SET_COUNTRY_DINAMIC_CONTENT](state, data) {
    if (data.address) {
      state.countryDinamicContent.address = JSON.parse(data.address)
      localStorage.setItem(MY_ACCOUNT_ADDRESS_DINAMIC_CONTENT, JSON.stringify(data.address))
    }
    if (data.taxpayer_info) {
      state.countryDinamicContent.taxpayerInfo = JSON.parse(data.taxpayer_info)
    }
    if (data.member_payout_options) {
      state.countryDinamicContent.memberPayoutOptions = JSON.parse(data.member_payout_options)
      localStorage.setItem(MY_ACCOUNT_BANK_INFO_DINAMIC_CONTENT, JSON.stringify(data.member_payout_options))
      localStorage.setItem(MY_ACCOUNT_TAX_PAYER_INFO, JSON.stringify(data.member_payout_options))
    }
  },
  // TODO: Move this to member
  [SET_MY_ACCOUNT_MEMBER_DETAILS](state, data) {
    state.memberDetails = data
    // TODO: state member->addresses
    if (state.memberDetails.address !== null) {
      state.memberDetails.address.address_data = JSON.parse(state.memberDetails.address.address_data)
    } else {
      state.memberDetails.address = {
        user_id: state.memberDetails.details.user_id,
        address_data: [],
      }
    }
    // TODO: state member->sponser
    if (state.memberDetails.details.sponsor === null) {
      state.memberDetails.details.sponsor = {
        id: null,
        user_id: null,
        sponsor_parent_user_id: null,
        sponsor_node_key: null,
        sponsor_depth_level: null,
        sponsor_node_left: null,
        sponsor_node_right: null,
        placement_parent_user_id: null,
        placement_node_key: null,
        placement_depth_level: null,
        placement_node_left: null,
        placement_node_right: null,
        placement_position: null,
        sponsor_position: null,
        created_at: null,
        updated_at: null,
        member: {
          id: null,
          user_id: null,
          country_id: null,
          avatar_image_link: null,
          avatar_image_path: null,
          name: null,
          translated_name: null,
          nationality_id: null,
          ic_pass_type_id: null,
          active_until_cw_id: null,
          ic_passport_number: null,
          date_of_birth: null,
          cw: null,
          join_date: null,
          expiry_date: null,
          personal_sales_cv: null,
          personal_sales_cv_percentage: null,
          effective_rank_id: null,
          highest_rank_id: null,
          enrollment_rank_id: null,
          tin_no_taiwan: null,
          tin_no_philippines: null,
          enroll_from_received: 0,
          ic_pass_verified: 0,
          is_enrollment_terminate: 0,
          defer_bonus_commission: 0,
          defer_reason_id: null,
          bank_type: null,
          status_id: null,
          created_by: null,
          updated_by: null,
          created_at: null,
          updated_at: null,
          user: {
            id: null,
            old_ibs_user_id: null,
            name: null,
            unique_login_id: null,
            old_member_id: null,
            mobile: null,
            email: null,
            active: null,
            login_count: null,
            created_by: null,
            update_by: null,
            created_at: null,
            updated_at: null,
          },
        },
      }
    }

    // Assign and Verify bank info
    // TODO: state member->banking
    if (typeof state.memberDetails.banking !== 'undefined' && state.memberDetails.banking !== null) {
      if (
        typeof state.memberDetails.banking.payment_data !== 'undefined' &&
        state.memberDetails.banking.payment_data !== null
      ) {
        state.memberDetails.banking.payment_data = JSON.parse(state.memberDetails.banking.payment_data)
      }
    }
    if (state.memberDetails.banking === null) {
      state.memberDetails.banking = {
        payment_data: {
          selected: '',
          bank_data: [],
        },
        user_id: state.memberDetails.details.user_id,
      }
    }
    if (
      typeof state.memberDetails.banking.payment_data.bank_data !== 'undefined' &&
      state.memberDetails.banking.payment_data.bank_data.length < 1 &&
      state.countryDinamicContent.memberPayoutOptions
    ) {
      state.memberDetails.banking.payment_data.bank_data = state.countryDinamicContent.memberPayoutOptions.bank_data
    }
    state.memberDetails.details.enrollment_rank.sales_types = JSON.parse(
      state.memberDetails.details.enrollment_rank.sales_types
    )

    // Assign and Verify Tax Info
    // TODO: state member->tax
    if (typeof state.memberDetails.tax !== 'undefined' && state.memberDetails.tax !== null) {
      state.memberDetails.tax.tax_data = JSON.parse(state.memberDetails.tax.tax_data)
    }
    if (state.memberDetails.tax === null && state.countryDinamicContent.memberPayoutOptions) {
      state.memberDetails.tax = state.countryDinamicContent.memberPayoutOptions.taxpayerInfo
    }
    // Verifying bank info
    if (typeof state.memberDetails.banking.payment_data === 'string') {
      state.memberDetails.banking.payment_data = state.countryDinamicContent.memberPayoutOptions
      state.memberDetails.banking.payment_data.selected = ''
    }
    if (state.expiryYearRange.length < 2) {
      if (getValue(state.countryDinamicContent, 'memberPayoutOptions.bank_data[1].fields[4].options')) {
        state.countryDinamicContent.memberPayoutOptions.bank_data[1].fields[4].options.forEach(item => {
          state.expiryYearRange.push({
            value: item.value,
            text: item.label,
          })
        })
      }
    }

    // Checking contact info
    // TODO: state member->contactInfo
    if (state.memberDetails.contact_info === null) {
      state.memberDetails.contact_info = {
        user_id: state.memberDetails.details.user_id,
        tel_office_1_country_code_id: null,
        tel_office_1_num: null,
        tel_home_1_num: null,
        mobile_1_country_code_id: 60,
        mobile_1_num: null,
      }
    }
  },
  // TODO: use master data mapper mixin
  [SET_BANK_LIST](state, data) {
    state.bankList = data
  },
  // TODO: use master data mapper mixin
  [SET_COUNTRIES_LIST](state, data) {
    state.countriesList = data
  },

  [SET_MY_ACCOUNT_PLACEMENT_PREFERENCE](state, data) {
    state.placementPreference = {
      preference: data.preference,
      next_placement: data.next_placement,
    }
  },
  [SET_MEMBER_AMP_ACCUMULATIONS](state, data) {
    state.ampAccumulations = data
  },
}
