import type { Module } from 'vuex'
import { getPublicKey, login, loginOut } from '@/api/login'
import { passwordEncryption } from '@/utils/encrypt'
import { reject } from 'lodash'
import { removeToken, setStorage, setToken } from '@/utils/jsCookie'

const getDefaultState = () => {
  return {
    username: '',
    avatar: '',
    token: '',
    userId: '',
    userInfo: {},
    menu: [],
    admin: true
  }
}

const userModule: Module<any, any> = {
  namespaced: true,
  state: getDefaultState(),
  mutations: {
    RESET_STATE: state => {
      Object.assign(state, getDefaultState())
    },
    SET_NAME: (state, name) => {
      state.username = name
    },
    SET_USERID: (state, userId) => {
      state.userId = userId
    },
    SET_ADMIN: (state, admin) => {
      state.admin = admin
    },
    SET_TOKEN: (state, admin) => {
      state.token = admin
    }
  },
  actions: {
    loginInside({ commit }, userInfo) {
      let pwd
      const { loginname, password } = userInfo
      return new Promise((resolve, reject) => {
        getPublicKey().then((result: any) => {
          const { data: res } = result
          if (res.code === 'success') {
            const { publicKey } = res.data
            pwd = passwordEncryption(password, publicKey)
            login({
              username: loginname,
              password: pwd
            })
              .then((response: any) => {
                if (response.data.code === 'success') {
                  resolve(response)
                  commit('SET_NAME', response.data.data.username)
                  commit('SET_TOKEN', response.data.data.accessToken)
                  commit('SET_USERID', response.data.data.id)
                  setToken(response.data.data.accessToken)
                  setStorage('userId', response.data.data.id)
                }
              })
              .catch(error => {
                resolve(error)
              })
          }
        })
      }).catch((err: any) => {
        reject(err)
      })
    },
    /* getUserInfo({ commit }) {
      return new Promise((resolve, reject) => {
        getMember()
          .then(response => {
            setStorage('admin', response.data.data.admin)
            commit('SET_ADMIN', response.data.data.admin)
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    }, */
    loginOut({ commit }) {
      return new Promise((resolve, reject) => {
        loginOut()
          .then(response => {
            resolve(response)
            removeToken()
            commit('RESET_STATE')
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // remove token
    resetToken({ commit }) {
      return new Promise(resolve => {
        removeToken() // must remove  token  first
        commit('RESET_STATE')
        resolve('')
      })
    }
  },
  getters: {
    userId: state => state.userId,
    token: state => state.token
  }
}
export default userModule
