import { defineStore } from 'pinia';
import { store } from '@/store';
import { storage } from '@/utils/Storage';
import {ACCESS_TOKEN, CURRENT_USER, LOCATION_MAP, OPEN_ID, WX_CONFIG} from "@/store/mutation-types";
import {getUserInfo, login} from "@/api/login";
import {ResultEnum} from "@/enums/httpEnum";

export interface IUserState {
  showCoupon: boolean;
  token: string;
  openId: string;
  username: string;
  welcome: string;
  avatar: string;
  permissions: any[];
  info: any;
  wxConfig: any;
  locationMap: any;
}

export const useUserStore = defineStore({
  id: 'app-user',
  state: (): IUserState => ({
    showCoupon: true,
    token: storage.get(ACCESS_TOKEN, ''),
    openId: storage.get(OPEN_ID, ''),
    // openId: storage.get(OPEN_ID, 'omWdJ62bH_6HXLQVOIefzN9J1oi4'),
    username: '',
    welcome: '',
    avatar: '',
    permissions: [],
    info: storage.get(CURRENT_USER, {}),
    wxConfig: storage.get(WX_CONFIG, null),
    locationMap: storage.get(LOCATION_MAP, {}),
  }),
  getters: {
    getShowCoupon(): boolean {
      return this.showCoupon;
    },
    getToken(): string {
      return this.token;
    },
    getOpenId(): string {
      return this.openId;
    },
    getAvatar(): string {
      return this.avatar;
    },
    getNickname(): string {
      return this.username;
    },
    getPermissions(): [any][] {
      return this.permissions;
    },
    getUserInfo(): object {
      return this.info;
    },
    getWxConfig(): object {
      return this.wxConfig;
    },
    getLocationMap(): object {
      return this.locationMap;
    },
  },
  actions: {
    setShowCoupon(showCoupon: boolean) {
      this.showCoupon = showCoupon;
    },
    setToken(token: string) {
      const ex = 1 * 4 * 60 * 60;
      storage.set(ACCESS_TOKEN, token, ex);
      this.token = token;
    },
    setOpenId(openId: string) {
      const ex = 1000 * 24 * 60 * 60;
      storage.set(OPEN_ID, openId, ex);
      this.openId = openId;
    },
    setAvatar(avatar: string) {
      this.avatar = avatar;
    },
    setPermissions(permissions) {
      this.permissions = permissions;
    },
    setUserInfo(info) {
      const ex = 1 * 4 * 60 * 60;
      storage.set(CURRENT_USER, info, ex);
      this.info = info;
    },
    setWxConfig(wxConfig) {
      const ex = 7000;
      storage.set(WX_CONFIG, wxConfig, ex);
      this.wxConfig = wxConfig;
    },
    setLocationMap(locationMap) {
      const ex = 7000;
      storage.set(LOCATION_MAP, locationMap, ex);
      this.locationMap = locationMap;
    },
    // 登录
    async login(loginData) {
      try {
        const {data, code, msg} = await login(loginData)
        if (code === ResultEnum.SUCCESS) {
          this.setToken(data.token);
          const userInfo = await getUserInfo()
          this.setUserInfo(userInfo)
          return Promise.resolve(data.token);
        } else {
          return Promise.reject(msg);
        }
      } catch (e) {
        return Promise.reject(e);
      }
    },
    // 登出
    async logout() {
      this.setToken('');
      this.setUserInfo(null);
      this.setWxConfig(null);
      // this.setLocationMap(null);
      this.setUserInfo(null);
      this.setOpenId('');
      storage.remove(ACCESS_TOKEN);
      storage.remove(CURRENT_USER);
      storage.remove(WX_CONFIG);
      // storage.remove(LOCATION_MAP);
      storage.remove(OPEN_ID);
      return Promise.resolve('');
    },
  },
});

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