import { login, loginUp } from '@/api/newLogin'
import { getToken, setToken, removeToken, setUserId, TokenKey } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    isLogin: false,
    roles: [],
    UserType: sessionStorage.getItem('UserType') ? sessionStorage.getItem('UserType') : '',
    UserId: sessionStorage.getItem('User_Id') ? sessionStorage.getItem('User_Id') : '',
    userPoewrList: sessionStorage.getItem('userAuthList') ? JSON.parse(sessionStorage.getItem('userAuthList')) : []
    // userPoewrList: sessionStorage.setItem('userAuthList') ? JSON.parse(sessionStorage.getItem('userAuthList')) : []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_LOGIN: (state, login) => {
    state.isLogin = login
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_UserId: (state, UserId) => {
    state.UserId = UserId
  },
  SET_UserType: (state, UserType) => {
    state.UserType = UserType
    sessionStorage.setItem('UserType', UserType)
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_UserPoewrList: (state, arr) => {
    state.userPoewrList = arr
  }
}

const actions = {
  // user login
  login ({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      login(userInfo).then(response => {
        const { date, token, userid } = response.data
        commit('SET_TOKEN', token)
        commit('SET_UserType', 1)
        commit('SET_UserId', userid)
        sessionStorage.setItem("userName", userInfo.username);
        setToken(TokenKey, token)
        setUserId(userid)
        // let nowDate = new Date();
        // let oldDate = new Date(date);
        // let diffTime = oldDate - nowDate;
        // setTimeout(function () {
        //   resetToken()
        // }, diffTime)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo ({ commit, state }) {
    return new Promise((resolve, reject) => {

      let data = {
        roles: state.userPoewrList,
        introduction: '',
        avatar: '',
        name: sessionStorage.getItem("userName"),
        token: state.token
      }

      const { name, avatar, roles } = data

      commit('SET_NAME', name)
      commit('SET_AVATAR', avatar)
      commit('SET_ROLES', roles ? roles : ['admin'])
      resolve(data)
    })
  },

  // user logout
  logout ({ commit, state }) {
    return new Promise((resolve, reject) => {
      loginUp().then(result => {
        // sessionStorage.removeItem("userName");
        // sessionStorage.removeItem("UserType");
        // sessionStorage.removeItem("User_Id");
        sessionStorage.clear();
        removeToken(); // must remove  token  first
        removeToken('codeToken');
        removeToken('systemSet');
        resetRouter();
        commit('RESET_STATE');
        commit('SET_ROLES', [])
        resolve();
      })

      // 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')
      commit('SET_ROLES', [])
      resolve()
    })
  },

  setPowerList ({ commit }, arr) {
    commit('SET_UserPoewrList', arr);
  },

  setPowerToken ({ commit }, token) {
    commit('SET_TOKEN', token);
  },

  setPowerName ({ commit }, name) {
    commit('SET_NAME', name);
  },

  setIsLogin ({ commit }, login) {
    commit('SET_LOGIN', login);
  }
}

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

