import type { UserInfo, UserUpdateInfo } from '/#/store';
import type { ErrorMessageMode } from '/#/axios';
import { defineStore } from 'pinia';
import { store } from '/@/store';
import { RoleEnum } from '/@/enums/roleEnum';
import { PageEnum } from '/@/enums/pageEnum';
import { JWT_TOKEN_KEY, REFRESH_TOKEN_KEY, ROLES_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum';
import { getAuthCache, setAuthCache } from '/@/utils/auth';
import {
  LoginParams,
  LoginResultModel,
  RefreshTokenParams,
  SmsLoginParams,
} from '/@/api/sys/model/userModel';
import { doRefreshToken, getMyInfo, loginApi, smsCodeLoginApi } from '/@/api/sys/user';
import { useI18n } from '/@/hooks/web/useI18n';
import { useMessage } from '/@/hooks/web/useMessage';
import { router } from '/@/router';
import { usePermissionStore } from '/@/store/modules/permission';
import { RouteRecordRaw } from 'vue-router';
import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
import { createLocalStorage } from '/@/utils/cache/index';
import { getEntitiesId } from '/@/api/dashboard/index';
import { defHttp } from '/@/utils/http/axios';
import moment from 'moment';

interface UserState {
  platInfo: any;
  enterPriseInfo: any;
  userInfo: Nullable<UserInfo>;
  userUpdateInfo?: Nullable<UserUpdateInfo>;
  token?: string;
  roleList: RoleEnum[];
  sessionTimeout?: boolean;
  lastUpdateTime: number;
  jwtToken?: string;
  refreshToken?: string;
  outTarget?: string;
}

const storage = createLocalStorage();
export const useUserStore = defineStore({
  id: 'app-user',
  state: (): UserState => ({
    //平台信息
    platInfo: storage.get('platformInfo') || null,
    enterPriseInfo: storage.get('enterPriseInfo') || null,

    // user info
    userInfo: null,
    userUpdateInfo: null,
    // token
    jwtToken: undefined,
    //refresh Token
    refreshToken: undefined,
    // roleList
    roleList: [],
    // Whether the login expired
    sessionTimeout: false,
    // Last fetch time
    lastUpdateTime: 0,
  }),

  getters: {
    getPlatInfo(): any {
      return this.platInfo;
    },
    getUserInfo(): UserInfo {
      return this.userInfo || getAuthCache<UserInfo>(USER_INFO_KEY) || {};
    },

    getUserUpdateInfo(): UserUpdateInfo {
      return this.userUpdateInfo || {};
    },
    getJwtToken(): string {
      return this.jwtToken || getAuthCache<string>(JWT_TOKEN_KEY);
    },
    getRefreshToken(): string {
      return this.refreshToken || getAuthCache<string>(REFRESH_TOKEN_KEY);
    },
    getRoleList(): RoleEnum[] {
      return this.roleList.length > 0 ? this.roleList : getAuthCache<RoleEnum[]>(ROLES_KEY);
    },
    getSessionTimeout(): boolean {
      return !!this.sessionTimeout;
    },
    getLastUpdateTime(): number {
      return this.lastUpdateTime;
    },
  },
  actions: {
    setPlatInfo(platInfo: any) {
      this.platInfo = platInfo;
    },
    setEnterPriseInfo(enterPriseInfo: any) {
      this.enterPriseInfo = enterPriseInfo;
    },

    storeToken(jwtToken: string, refreshToken: string) {
      this.jwtToken = jwtToken;
      this.refreshToken = refreshToken;
      setAuthCache(JWT_TOKEN_KEY, jwtToken);
      setAuthCache(REFRESH_TOKEN_KEY, refreshToken);
    },
    setToken(info: string | undefined) {
      this.jwtToken = info;
      setAuthCache(JWT_TOKEN_KEY, info);
    },
    setRoleList(roleList: RoleEnum[]) {
      this.roleList = roleList;
      setAuthCache(ROLES_KEY, roleList);
    },
    setUserInfo(info: UserInfo | null) {
      this.userInfo = info;
      this.lastUpdateTime = new Date().getTime();
      setAuthCache(USER_INFO_KEY, info);
    },
    setUserUpdateInfo(info: UserUpdateInfo) {
      this.userUpdateInfo = info;
    },
    setSessionTimeout(flag: boolean) {
      this.sessionTimeout = flag;
    },
    resetState() {
      this.userInfo = null;
      this.token = '';
      this.roleList = [];
      this.sessionTimeout = false;
    },
    /**
     * @description: login
     */
    async login(
      params: LoginParams & {
        goHome?: boolean;
        mode?: ErrorMessageMode;
      }
    ): Promise<UserInfo | null> {
      try {
        const { goHome = true, mode, ...loginParams } = params;
        const data = await loginApi(loginParams, mode);
        if (data.token) {
          return this.process(data, goHome);
        } else {
          return Promise.reject();
        }
      } catch (error) {
        return Promise.reject(error);
      }
    },
    async process(data: LoginResultModel, goHome?: boolean): Promise<UserInfo | null> {
      const { token, refreshToken } = data;
      this.storeToken(token, refreshToken);
      // get user info
      const userInfo = await this.getMyUserInfoAction();

      const sessionTimeout = this.sessionTimeout;
      if (sessionTimeout) {
        this.setSessionTimeout(false);
      } else if (goHome) {
        const permissionStore = usePermissionStore();
        if (!permissionStore.isDynamicAddedRoute) {
          const routes = await permissionStore.buildRoutesAction();
          routes.forEach((route) => {
            router.addRoute(route as unknown as RouteRecordRaw);
          });
          router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw);
          permissionStore.setDynamicAddedRoute(true);
        }
        await router.replace(userInfo.homePath || PageEnum.BASE_HOME);
      }
      return userInfo;
    },
    async smsCodelogin(
      params: SmsLoginParams & {
        goHome?: boolean;
        mode?: ErrorMessageMode;
      }
    ): Promise<UserInfo | null> {
      try {
        const { goHome = true, mode, ...loginParams } = params;
        const data = await smsCodeLoginApi(loginParams, mode);
        return this.process(data, goHome);
      } catch (error) {
        const { createMessage } = useMessage();
        createMessage.error(error?.message || '');
        // return Promise.reject(error);
      }
    },
    async getMyUserInfoAction(): Promise<UserInfo> {
      const userInfo = await getMyInfo();
      this.setUserInfo(userInfo);
      const { roles } = userInfo;
      const roleList = roles.map((item) => item) as RoleEnum[];
      this.setRoleList(roleList);
      try {
        if (roleList[0] !== 'SYS_ADMIN') {
          const res = await getEntitiesId();
          const entityId = res.data[0]?.entityId;
          window.localStorage.setItem('entityId', JSON.stringify(entityId));
          window.sessionStorage.setItem('entityId', JSON.stringify(entityId));
        }
      } catch { }
      return userInfo;
    },
    /**
     * @description: logout
     */
    async logout(goLogin = false) {
      // try {
      //   await doLogout();
      // } catch {
      //   console.log('注销Token失败');
      // }
      // this.resetState();
      // setAuthCache(JWT_TOKEN_KEY, undefined);
      // setAuthCache(REFRESH_TOKEN_KEY, undefined);
      // this.setSessionTimeout(false);
      // // goLogin && router.push(PageEnum.BASE_LOGIN);
      const userInfo = await getAuthCache(USER_INFO_KEY);
      
      let addRecordParam = {
        systemLog: {
          cjrq: moment().format('YYYY-MM-DD HH:mm:ss'),
          czsm: "退出操作",
          opt: "logout",
          sfcg: 1,
          userid: userInfo.info.EMPLOYEE_MIS.EMPLOYEE_ID,
          username: userInfo.info.EMPLOYEE.NAME
        }
      }
      if (goLogin) {
        //插入退出日志记录
        defHttp.post({ url: '/base-center/syslog/AddLogin', params: addRecordParam })
      }

      this.setToken(undefined);
      this.setSessionTimeout(false);
      setAuthCache(REFRESH_TOKEN_KEY, undefined);
      this.setUserInfo(null);
      goLogin && router.push(PageEnum.BASE_LOGIN);
      window.localStorage.clear();
      window.localStorage.removeItem('updateUserInfo');
    },

    async doRefresh() {
      try {
        const req = { refreshToken: this.refreshToken } as RefreshTokenParams;
        const data = await doRefreshToken(req);
        const { token, refreshToken } = data;
        this.storeToken(token, refreshToken);
      } catch (error) {
        this.logout();
      }
    },

    /**
     * @description: Confirm before logging out
     */
    confirmLoginOut() {
      const { createConfirm } = useMessage();
      const { t } = useI18n();
      createConfirm({
        iconType: 'warning',
        title: t('sys.app.logoutTip'),
        content: t('sys.app.logoutMessage'),
        onOk: async () => {
          await this.logout(true);
        },
      });
    },
  },
});

// Need to be used outside the setup
export function useUserStoreWithOut() {
  return useUserStore(store);
}
