import * as T from '../../utils/types'
import {
  IS_LOGGED_IN,
  LOGGED_IN_USER,
  IS_LOGGED_IN_GUEST,
  LOGGED_GUEST_USER,
  LOGGED_IN_USER_NAME,
  LOGGED_IN_USER_IBO,
  CHANGE_PREFERRED_CONTACT_STATUS,
} from '../../utils/localStorageKeys'
import Raven from 'raven-js'

export default {
  [T.SET_TOKEN](state, data) {
    let accessToken = data.access_token
    let expiresIn = data.expires_in
    let refreshToken = data.refresh_token
    state.auth = {
      accessToken: accessToken,
      expiresIn: expiresIn,
      refreshToken: refreshToken,
    }
  },
  [T.SET_LOGIN_USER](state, data) {
    return new Promise(resolve => {
      state.loggedInUser = data
      localStorage.setItem(LOGGED_IN_USER_NAME, data !== null ? data.name : '')
      localStorage.setItem(LOGGED_IN_USER_IBO, data !== null ? data.old_member_id : '')
      localStorage.setItem(LOGGED_IN_USER, JSON.stringify(data))
      if (process.env.VUE_APP_LOG_SENTRY === 'true') {
        Raven.setUserContext({
          name: data.name,
          id: data.old_member_id,
        })
      }
      resolve()
    })
  },
  [T.SET_LOGIN_GUEST_USER](state, data) {
    return new Promise(resolve => {
      state.loggedInguestUser = data
      localStorage.setItem(LOGGED_GUEST_USER, JSON.stringify(data))
      localStorage.setItem(LOGGED_IN_USER_NAME, data.name)
      localStorage.setItem(LOGGED_IN_USER_IBO, data.old_member_id)
      if (process.env.VUE_APP_LOG_SENTRY === 'true') {
        Raven.setUserContext({
          name: data.name,
          id: data.old_member_id,
        })
      }
      resolve()
    })
  },
  [T.LOGIN_USER](state) {
    state.isLoggedIn = true
    localStorage.setItem(IS_LOGGED_IN, true)
  },
  [T.LOGOUT_USER](state) {
    state.isLoggedIn = false
    state.auth = {}
    localStorage.removeItem(IS_LOGGED_IN)
  },
  [T.SET_RESET_STATUS](state, data) {
    state.resetStatus = data
  },
  [T.ACTIVATE_RESET](state, data) {
    state.resetPasswordStatus = data
  },
  [T.SHOW_RESET](state, data) {
    state.showReset = data
  },
  [T.SET_RESET_ALERT](state, data) {
    state.resetAlert = data
  },
  [T.SET_EMAIL_RESET](state, data) {
    state.emailReset = data
  },
  [T.SET_RESET_ERROR_STATUS](state, data) {
    state.resetErrorStatus = data
  },
  [T.SET_RESET_ERROR_MESSAGE](state, data) {
    state.resetErrorMessage = data
  },
  [T.SET_RESET_RESPONSE_DATA](state, data) {
    state.resetResponseData = data
  },
  [T.SET_GUEST](state, payload) {
    state.guest = payload
  },
  [T.SET_LOGIN_GUEST_USER_LOGIN_STATUS](state, status) {
    state.isLoggedInGuest = status
    localStorage.setItem(IS_LOGGED_IN_GUEST, status)
  },
  [T.SET_ON_BEHLAF](state, status) {
    state.isLoggedInOnBehalf = status
  },
  [T.AUTH_SET_RULES](state, rules) {
    state.rules = rules
  },
  [T.SET_PREFERRED_CONTACT](state, data) {
    state.preferredContact = data
  },
  [T.DISABLE_ROUTE_CHECK](state, data) {
    state.disableRouteCheck = data
  },
  [T.SET_CONTACT_VERIFICATION_STATUS](state, data = {}) {
    let status = {
      requires: Boolean(data.requires),
      skipVerification: Boolean(data.skipVerification),
    }
    state.verifyContactStatus = status
    localStorage.setItem(CHANGE_PREFERRED_CONTACT_STATUS, JSON.stringify(status))
  },
  [T.SET_CHANGE_PASSWORD_STATUS](state, data) {
    state.changePasswordStatus = Boolean(data)
  },
}
