import {
  login,
  logout,
  getInfo
} from '@/api/user';
import { login as h5login, logout as h5logout ,me } from "@/api/h5/login";
import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'
import {
  resetRouter,
} from '@/router'
import router from "@/router";
const getDefaultState = () => {
  return {
    token_h5: getToken(true),
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    info_h5: {}
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_INFO_H5: (state, info) => {
    state.info_h5 = info
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_TOKEN_H5: (state, token) => {
    state.token_h5 = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  }
}

const actions = {
  // user login
  login({
    commit
  }, userInfo) {
    const {
      username,
      password
    } = userInfo
    const path = router.app._route.path
    if (/\/h5/g.test(path)) {
      return new Promise((resolve, reject) => {
        h5login({
          username: username.trim(),
          password: password
        }).then(res => {
          commit('SET_TOKEN_H5', res.access_token)
          setToken(res.access_token)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    } else {
      return new Promise((resolve, reject) => {
        login({
          username: username.trim(),
          password: password
        }).then(response => {
          console.log(response)
          const {
            data
          } = response;
          console.log(response.access_token)
          commit('SET_TOKEN', response.access_token)
          setToken(response.access_token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  },

  // get user info
  getInfo({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      const path = router.app._route?.path || window.location.href
      if (/\/h5/g.test(path)) {
        me(state.token_h5).then(res => {
          if (!res) {
            reject('身份验证失败请重新登录')
          }
          res["roles"] = ["admin"];
          commit('SET_INFO_H5',res)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      } else {
        getInfo(state.token).then(response => {

          if (!response) {
            reject('身份验证失败请重新登录')
          }
          response["roles"] = ["admin"];
          const {
            roles,
            name,
            avatar
          } = response



          commit('SET_ROLES', roles)
          commit('SET_NAME', name)
          commit('SET_AVATAR', avatar)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      }
    })
  },

  // user logout
  logout({
    commit,
    state
  }) {
    const path = router.app._route.path
    if (/\/h5/g.test(path)) {
      return new Promise((resolve,reject) => {
        h5logout(state.token_h5).then(_ => {
          removeToken()
          commit('RESET_STATE')
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    } else {
      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
}
