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

const CARD_KEY = "cardList";
const APPLY_CARD_KEY = "applyList";
const CURRENCY_LIST_KEY = "currencyList"; // 新增币种列表的本地存储键
const ORDER_DETAIL_KEY = "orderDetailEncrypted"; // 订单详情加密缓存键

const useCardStore = defineStore("cardStore", () => {
  // 用户卡和申请卡列表
  const userCard = ref([]);
  const applyCard = ref([]);

  // 新增币种列表
  const currencyList = ref([]);

  // 订单详情（使用加密存储）
  const orderDetail = ref<any | null>(null);

  // 初始化用户卡
  const initUserCard = () => {
    const encryptedCard = ls.get(CARD_KEY);
    if (encryptedCard) {
      const decryptedCard = crypt.decrypt(encryptedCard);
      if (decryptedCard) {
        userCard.value = JSON.parse(decryptedCard);
      }
    }
  };

  // 初始化申请卡
  const initApplyCard = () => {
    const encryptedCard = ls.get(APPLY_CARD_KEY);
    if (encryptedCard) {
      const decryptedCard = crypt.decrypt(encryptedCard);
      if (decryptedCard) {
        applyCard.value = JSON.parse(decryptedCard);
      }
    }
  };

  // 初始化币种列表
  const initCurrencyList = () => {
    const encryptedCurrencies = ls.get(CURRENCY_LIST_KEY);
    if (encryptedCurrencies) {
      const decryptedCurrencies = crypt.decrypt(encryptedCurrencies);
      if (decryptedCurrencies) {
        currencyList.value = JSON.parse(decryptedCurrencies);
      }
    }
  };

  // 初始化订单详情（解密）
  const initOrderDetail = () => {
    const encryptedDetail = ls.get(ORDER_DETAIL_KEY);
    if (encryptedDetail) {
      const decryptedDetail = crypt.decrypt(encryptedDetail);
      if (decryptedDetail) {
        try {
          orderDetail.value = JSON.parse(decryptedDetail);
        } catch (e) {
          orderDetail.value = null;
        }
      }
    }
  };

  // 保存用户卡
  const saveUserCard = (info: any) => {
    userCard.value = info;
    const encryptedCard = crypt.encrypt(JSON.stringify(info));
    ls.set(CARD_KEY, encryptedCard);
  };

  // 保存申请卡
  const saveApplyCard = (info: any) => {
    applyCard.value = info;
    const encryptedCard = crypt.encrypt(JSON.stringify(info));
    ls.set(APPLY_CARD_KEY, encryptedCard);
  };

  // 保存币种列表
  const saveCurrencyList = (list: any) => {
    currencyList.value = list;
    const encryptedCurrencies = crypt.encrypt(JSON.stringify(list));
    ls.set(CURRENCY_LIST_KEY, encryptedCurrencies);
  };

  // 保存订单详情（加密）
  const saveOrderDetail = (detail: any) => {
    orderDetail.value = detail;
    const encryptedDetail = crypt.encrypt(JSON.stringify(detail));
    ls.set(ORDER_DETAIL_KEY, encryptedDetail);
  };

  // 清空订单详情（缓存 + 内存）
  const clearOrderDetail = () => {
    orderDetail.value = null;
    // @ts-ignore: store2 兼容 remove/removeItem
    if (typeof (ls as any).remove === "function") {
      (ls as any).remove(ORDER_DETAIL_KEY);
    } else if (typeof (ls as any).removeItem === "function") {
      (ls as any).removeItem(ORDER_DETAIL_KEY);
    } else {
      ls.set(ORDER_DETAIL_KEY, null);
    }
  };

  // 初始化所有数据
  initUserCard();
  initApplyCard();
  initCurrencyList();
  initOrderDetail();

  return {
    userCard,
    applyCard,
    currencyList, // 新增币种列表
    orderDetail, // 订单详情（已解密）
    saveUserCard,
    saveApplyCard,
    saveCurrencyList, // 新增保存币种列表的操作
    saveOrderDetail, // 保存订单详情（加密）
    clearOrderDetail, // 清空订单详情
  };
});

export default useCardStore;
