import { getStore, setStore } from '@utils/store';
import { resetRouter } from '@/router';
import { loginByPwd } from '@api/api-login';
import { logout, refreshToken } from '@api/upms/api-user';

const user = {
  state: {
    accessToken: getStore('accessToken') || '',
    userInfo: {},
    curTheme: getStore('curTheme') || 'dark',
    expireTime: getStore('expireTime') || 0,
    urlPrefix: getStore('urlPrefix') || 'undefined'
  },
  mutations: {
    SET_ACCESS_TOKEN: (state, accessToken) => {
      state.accessToken = accessToken;
      setStore('accessToken', accessToken);
    },
    SET_USER_INFO: (state, userInfo) => {
      state.userInfo = userInfo;
      setStore('userInfo', userInfo);
    },
    SET_CUR_THEME: (state, curTheme) => {
      state.curTheme = curTheme;
      setStore('curTheme', curTheme);
    },
    SET_TOKEN_EXPIRE_TIME: (state, expireTime) => {
      state.expireTime = expireTime;
      setStore('expireTime', expireTime);
    },
    SET_URL_PREFIX: (state, urlPrefix) => {
      state.urlPrefix = urlPrefix;
      setStore('urlPrefix', urlPrefix);
    }
  },
  actions: {
    // 设置当前主题
    setCurTheme({ commit }, curTheme) {
      commit('SET_CUR_THEME', curTheme);
    },
    // 设置用户信息
    setUserInfo({ commit }, userInfo) {
      commit('SET_USER_INFO', userInfo);
    },
    switchRole({ commit }, data) {
      commit('SET_ACCESS_TOKEN', data.token);
      commit('SET_AUTH_BTN_CODES', data.permissionCodes);
      commit('SET_TOKEN_EXPIRE_TIME', new Date().getTime() + data.ttl * 1000);
    },
    // 用户名登录
    loginByUsername({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        loginByPwd(userInfo)
          .then((res) => {
            commit('SET_ACCESS_TOKEN', res.data.token);
            commit('SET_URL_PREFIX', res.data.urlPrefix);
            commit('SET_AUTH_BTN_CODES', res.data.permissionCodes);
            commit('SET_TOKEN_EXPIRE_TIME', new Date().getTime() + res.data.ttl * 1000);
            resolve(res);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 登出
    logout({ commit }) {
      return new Promise((resolve, reject) => {
        logout()
          .then((res) => {
            if (res.code === 200) {
              commit('SET_ACCESS_TOKEN', '');
              commit('SET_MULTIPLE_TAG', []);
              commit('SET_AUTH_BTN_CODES', []);
              resetRouter();
              resolve(res);
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    refreshToken({ commit }) {
      refreshToken().then((res) => {
        commit('SET_ACCESS_TOKEN', res.data.token);
        commit('SET_URL_PREFIX', res.data.urlPrefix);
        commit('SET_TOKEN_EXPIRE_TIME', new Date().getTime() + res.data.ttl * 1000);
      });
    },
    removeStorage({ commit }) {
      return new Promise((resolve) => {
        commit('SET_ACCESS_TOKEN', '');
        commit('SET_URL_PREFIX', 'undefined');
        commit('SET_MULTIPLE_TAG', []);
        commit('SET_AUTH_BTN_CODES', []);
        resolve();
      });
    }
  }
};

export default user;
