import { pinia } from "@/pinia"
import { LoginData } from "@/common/apis/ab98/type"

// 本地存储键名常量
const STORAGE_KEYS = {
    FACE: 'ab98_face',
    SEX: 'ab98_sex',
    NAME: 'ab98_name',
    USERID: 'ab98_userid',
    REGISTERED: 'ab98_registered',
    TEL: 'ab98_tel',
    TOKEN: 'ab98_token',
    LOGIN_CODE: 'ab98_login_code'
}

/**
 * AB98用户信息存储
 * @description 管理AB98系统用户相关状态信息
 */
export const useAb98UserStore = defineStore("ab98User", () => {
  // 用户面部图像URL
  const storedFace = localStorage.getItem(STORAGE_KEYS.FACE)
  const face_img = ref<string>(storedFace ? decodeURIComponent(storedFace) : '')
  // 用户性别（男/女）
  const storedSex = localStorage.getItem(STORAGE_KEYS.SEX)
  const sex = ref<string>(storedSex ? decodeURIComponent(storedSex) : '')
  // 用户真实姓名
  const storedName = localStorage.getItem(STORAGE_KEYS.NAME)
  const name = ref<string>(storedName ? decodeURIComponent(storedName) : '')
  // AB98系统用户唯一标识
  const storedUserId = localStorage.getItem(STORAGE_KEYS.USERID)
  const userid = ref<string>(storedUserId ? decodeURIComponent(storedUserId) : "")
  // 是否已完成注册流程
  const registered = ref<boolean>(JSON.parse(localStorage.getItem(STORAGE_KEYS.REGISTERED) || "false"))
  // 用户绑定手机号
  const storedTel = localStorage.getItem(STORAGE_KEYS.TEL)
  const tel = ref<string>(storedTel ? decodeURIComponent(storedTel) : "")
  // 用户认证令牌
  const storedToken = localStorage.getItem(STORAGE_KEYS.TOKEN)
  const token = ref<string>(storedToken ? decodeURIComponent(storedToken) : "");

  const tokenLogin = ref<string>("");
  // 登录验证码
  const loginCode = '1'; // 默认验证码
  // 用户登录状态
  const isLogin = ref<boolean>(false);
  const storedLoginCode = localStorage.getItem(STORAGE_KEYS.LOGIN_CODE);
  isLogin.value = tel.value && storedLoginCode === String(loginCode) ? true : false;

  /**
   * 更新用户基本信息
   * @param data - 登录接口返回的用户数据
   */
  const setUserInfo = (data: LoginData) => {
    face_img.value = data.face_img
    localStorage.setItem(STORAGE_KEYS.FACE, encodeURIComponent(data.face_img))
    sex.value = data.sex
    localStorage.setItem(STORAGE_KEYS.SEX, encodeURIComponent(data.sex))
    name.value = data.name
    localStorage.setItem(STORAGE_KEYS.NAME, encodeURIComponent(data.name))
    userid.value = data.userid
    localStorage.setItem(STORAGE_KEYS.USERID, encodeURIComponent(data.userid))
    registered.value = data.registered
    localStorage.setItem(STORAGE_KEYS.REGISTERED, JSON.stringify(data.registered))
    tel.value = data.tel
    localStorage.setItem(STORAGE_KEYS.TEL, encodeURIComponent(data.tel))
    localStorage.setItem(STORAGE_KEYS.LOGIN_CODE, encodeURIComponent(loginCode))
  }

  /**
   * 清空用户敏感信息
   * @description 用于用户登出或会话过期时
   */
  const clearUserInfo = () => {
    face_img.value = ""
    localStorage.removeItem(STORAGE_KEYS.FACE)
    sex.value = ""
    localStorage.removeItem(STORAGE_KEYS.SEX)
    name.value = ""
    localStorage.removeItem(STORAGE_KEYS.NAME)
    userid.value = ""
    localStorage.removeItem(STORAGE_KEYS.USERID)
    registered.value = false
    localStorage.removeItem(STORAGE_KEYS.REGISTERED)
    tel.value = ""
    localStorage.removeItem(STORAGE_KEYS.TEL)
    localStorage.removeItem(STORAGE_KEYS.TOKEN)
    localStorage.removeItem(STORAGE_KEYS.LOGIN_CODE)
  }

  /**
   * 设置认证令牌
   * @param value - JWT格式的认证令牌
   */
  const setToken = (value: string) => {
    localStorage.setItem(STORAGE_KEYS.TOKEN, encodeURIComponent(value))
    token.value = value
  }

  const setTel = (value: string) => {
    localStorage.setItem(STORAGE_KEYS.TEL, btoa(value))
    tel.value = value
  }

  const setIsLogin = (value: boolean) => {
    isLogin.value = value;
  }

  const setTokenLogin = (value: string) => {
    tokenLogin.value = value;
  }

  return { 
    face_img,
    sex,
    name,
    userid,
    registered,
    tel,
    token,
    isLogin,
    tokenLogin,
    setUserInfo,
    setToken,
    setTel,
    setIsLogin,
    clearUserInfo,
    setTokenLogin
  }
})

/**
 * @description 在非setup上下文或SSR场景中使用store
 */
export function useAb98UserStoreOutside() {
  return useAb98UserStore(pinia)
}