import {
  login,
  logout,
  getInfo
} from '@/api/user'
import {
  getToken,
  setToken,
  removeToken,
  setUserInfo,
  getUserInfo,
  removeUserInfo
} from '@/utils/auth'
import {
  resetRouter
} from '@/router'
import $router from '@/router'
const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    checked: '',
    userInfo: getUserInfo() || {}
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USER_INFO: (state, userInfo) => {
    state.userInfo = userInfo
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  getAdmin: (state, data) => {
    state.checked = data
  }
}

const actions = {
  getAdmin: (state, data) => {
    state.checked = data
  },
  // 登录
  ActionLogin({ commit }, userInfo) {
    const { username: account } = userInfo
    login({ account, ...userInfo }).then(res => {
      if (res?.data) {
        const { token, userInfo } = res?.data
        commit('SET_TOKEN', token)
        commit('SET_USER_INFO', userInfo)
        setToken(token)
        setUserInfo(userInfo)
        $router.push({
          path: '/'
        })
      }
    })
  },
  // 获取用户信息
  ActionUserInfo({ commit, dispatch }) {
    return new Promise((resolve, reject) => {
      if (Object.keys(state.userInfo).length > 0) {
        resolve(state.userInfo)
      } else if (getUserInfo()) {
        resolve(getUserInfo)
      } else {
        reject()
      }
    })
  },
  // 退出登录
  ActionLogout({ commit, dispatch }) {
    removeToken() // must remove  token  first
    removeUserInfo()
    resetRouter()
    commit('SET_TOKEN', '')
    commit('SET_USER_INFO', {})
    $router.push({ path: '/login' })
  },
  // user login
  login({
    commit
  }, userInfo) {
    const {
      username,
      password,
      loginType
    } = userInfo
    new Promise((resolve, reject) => {
      login({
        account: username.trim(),
        password: password,
        loginType: loginType
      }).then(response => {
        const {
          data
        } = response
        console.log('2', data)
        commit('SET_TOKEN', data)
        setToken(data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  // getInfo({ commit, state }) {
  //   return new Promise((resolve, reject) => {
  //     getInfo(state.token).then(response => {
  //       const { data } = response

  //       if (!data) {
  //         return reject('Verification failed, please Login again.')
  //       }

  //       const { name, avatar } = data

  //       commit('SET_NAME', name)
  //       commit('SET_AVATAR', avatar)
  //       resolve(data)
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // },

  // user logout
  logout({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({
    commit
  }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
