import { defineStore } from "pinia";
import ls from "@/utils/store2";
import { ref } from "vue";
import { userToken } from "../composables/config";
import crypt from "../composables/crypt";

export interface UserInfo {
  id?: string;
  name: string;
  email: string;
  phone: string;
  approveStatus?: number;
}
export interface AccountInfo {
  loginName: string;
  password: string;
  rememberPassword: boolean;
}
const STORAGE_KEY = "user";
const ACCOUNT_INFO_KEY = "accountInfo";
const ID_TYPE_OPTIONS_KEY = "reasonsOptions";
const APP_VERSION = 'appVersion'
const PROBLEM_DETAILS_KEY = "problemDetails";
const useUserStore = defineStore("userStore", () => {
  const userInfo = ref<UserInfo | null>(null);
  const accountInfo = ref<AccountInfo | null>(null);
  const isLoggedIn = ref(false);
  const reasonsOptions = ref<any>(null);
  const appVersion = ref<any>(null);
  const problemDetails = ref<any>(null);
  const initUserInfo = () => {
    const encryptedInfo = ls.get(STORAGE_KEY);
    if (encryptedInfo) {
      const decryptedInfo = crypt.decrypt(encryptedInfo);
      if (decryptedInfo) {
        userInfo.value = JSON.parse(decryptedInfo);
        isLoggedIn.value = true;
      }
    }
  };
  const initAppVersion = () => {
    const encryptedInfo = ls.get(APP_VERSION);
    if (encryptedInfo) {
      const decryptedInfo = crypt.decrypt(encryptedInfo);
      if (decryptedInfo) {
        userInfo.value = JSON.parse(decryptedInfo);
        isLoggedIn.value = true;
      }
    }
  };
  const initProblemDetails = () => {
    const encryptedInfo = ls.get(PROBLEM_DETAILS_KEY);
    if (encryptedInfo) {
      const decryptedInfo = crypt.decrypt(encryptedInfo);
      if (decryptedInfo) {
        problemDetails.value = JSON.parse(decryptedInfo);
        isLoggedIn.value = true;
      }
    }
  };
  const initReasonsOptions = () => {
    const encryptedInfo = ls.get(ID_TYPE_OPTIONS_KEY);
    if (encryptedInfo) {
      const decryptedInfo = crypt.decrypt(encryptedInfo);
      if (decryptedInfo) {
        reasonsOptions.value = JSON.parse(decryptedInfo);
        isLoggedIn.value = true;
      }
    }
  };
  const initAccountInfo = () => {
    const encryptedInfo = ls.get(ACCOUNT_INFO_KEY);
    if (encryptedInfo) {
      const decryptedInfo = crypt.decrypt(encryptedInfo);
      if (decryptedInfo) {
        accountInfo.value = JSON.parse(decryptedInfo);
      }
    }
  };
  const saveUserInfo = (info: UserInfo) => {
    userInfo.value = info;
    isLoggedIn.value = true;
    const encryptedInfo = crypt.encrypt(JSON.stringify(info));
    ls.set(STORAGE_KEY, encryptedInfo);
  };
  const saveProblemDetails = (info: any) => {
    problemDetails.value = info;
    const encryptedInfo = crypt.encrypt(JSON.stringify(info));
    ls.set(PROBLEM_DETAILS_KEY, encryptedInfo);
  };
  const saveAppVersion = (info: any) => {
    appVersion.value = info;
    const encryptedInfo = crypt.encrypt(JSON.stringify(info));
    ls.set(APP_VERSION, encryptedInfo);
  };
  const saveReasonsOptions = (info: any) => {
    reasonsOptions.value = info;
    const encryptedInfo = crypt.encrypt(JSON.stringify(info));
    ls.set(ID_TYPE_OPTIONS_KEY, encryptedInfo);
  };
  const saveAccountInfo = (info: AccountInfo) => {
    accountInfo.value = info;
    const encryptedInfo = crypt.encrypt(JSON.stringify(info));
    ls.set(ACCOUNT_INFO_KEY, encryptedInfo);
  };
  const clearUserInfo = () => {
    userInfo.value = null;
    isLoggedIn.value = false;
    userToken.value = "";
    reasonsOptions.value = null;
    ls.remove(STORAGE_KEY);
  };

  initReasonsOptions();
  initUserInfo();
  initAccountInfo();
  initAppVersion();
  initProblemDetails();
  return {
    userInfo,
    accountInfo,
    isLoggedIn,
    reasonsOptions,
    appVersion,
    problemDetails,
    saveProblemDetails,
    saveAppVersion,
    saveReasonsOptions,
    saveUserInfo,
    saveAccountInfo,
    clearUserInfo,
  };
});

export default useUserStore;
