import { ref, watch } from "vue";
import { defineStore } from "pinia";
import api from '@/utils/api';
import { toast } from '@/utils/widget';
import { initGlobalTIM, globalTimChatManager } from "@/utils/tim-chat.js";
import cache from '@/utils/cache';
const env = __VITE_ENV__;

// 缓存键名
const CACHE_KEYS = {
  ACCOUNT: 'account',
  OPENID: 'openid',
  DOCTOR_INFO: 'doctorInfo'
};

export default defineStore("accountStore", () => {
  const appid = env.MP_WX_APP_ID;
  const corpId = env.MP_CORP_ID;

  // 从缓存中恢复数据
  const account = ref(cache.get(CACHE_KEYS.ACCOUNT, null));

  const loginPromise = ref(null);
  // IM 相关
  const openid = ref(cache.get(CACHE_KEYS.OPENID, ""));
  const isIMInitialized = ref(false);
  // 医生信息 - 不做缓冲处理，每次都重新获取
  const doctorInfo = ref(null);

  function getLoginPromise(phoneCode = '') {
    if (loginPromise.value) return loginPromise.value;
    loginPromise.value = loginByCode(phoneCode);
    return loginPromise.value;
  }

  async function login(phoneCode) {
    const res = await getLoginPromise(phoneCode);
    loginPromise.value = null;
    return res
  }

  async function loginByCode(phoneCode = '') {
    try {

      const { code } = await uni.login({
        appid,
        provider: "weixin",
        scope: "snsapi_base",
      });
      if (code) {
        const res = await api('wxAppLogin', {
          appId: appid,
          phoneCode,
          code,
          corpId,
        });
        if (res.success && res.data) {
          // if (!res.data.mobile) {
          //   const target = '/pages/login/login';
          //   uni.redirectTo({ url: target });
          //   return;
          // }
          account.value = res.data;
          openid.value = res.data.openid;
          // 持久化账户信息
          // cache.set(CACHE_KEYS.ACCOUNT, res.data);
          // cache.set(CACHE_KEYS.OPENID, res.data.openid);

          // 登录成功后初始化腾讯IM
          await getDoctorInfo({ openid: openid.value });
          await initIMAfterLogin();
          return res.data
        }
      }
      toast('登录失败，请重新登录');

    } catch (e) {
      toast('登录失败，请重新登录');
    }
    return Promise.reject()
  }

  async function getDoctorInfo(data = {}) {
    try {
      if (!account.value?.openid) {
        return
      }
      const res = await api('getCorpMemberData', {
        ...data,
        weChatOpenId: account.value.openid,
      });
      doctorInfo.value = res?.data || null;

      // 检查账号是否被禁用
      if (doctorInfo.value?.accountState === "disable") {
        uni.showModal({
          title: '账号被禁用',
          content: '您的账号已被禁用，请联系管理员',
          showCancel: false,
          confirmText: '确定',
          success: () => {
            uni.redirectTo({ url: "/pages/login/login" });
          }
        });
        return;
      }

    } catch (e) {
      console.error('获取医生信息失败:', e);
    }
  }
  async function initIMAfterLogin() {
    if (isIMInitialized.value) return true;
    if (!doctorInfo.value) {
      await getDoctorInfo();
    }
    try {
      const userID = doctorInfo.value?.userid;
      if (!userID) {
        return
      }

      const success = await initGlobalTIM(userID);
      if (!success) {
        console.error('initGlobalTIM 返回失败');
        return false;
      }

      isIMInitialized.value = true;
      return true;
    } catch (error) {
      console.error('IM初始化失败:', error);
      isIMInitialized.value = false;
      return false;
    }
  }

  // 退出登录
  async function logout() {
    try {
      // 退出腾讯IM
      if (globalTimChatManager && globalTimChatManager.tim) {
        console.log('开始退出腾讯IM');
        await globalTimChatManager.destroy();
        console.log('腾讯IM退出成功');
      }
    } catch (error) {
      console.error('退出腾讯IM失败:', error);
    }

    // 清空账户信息
    account.value = null;
    openid.value = "";
    isIMInitialized.value = false;
    doctorInfo.value = null;

    // 清空缓存
    // cache.remove(CACHE_KEYS.ACCOUNT);
    // cache.remove(CACHE_KEYS.OPENID);
  }

  watch(account, n => {
    if (n) {
      cache.set(CACHE_KEYS.ACCOUNT, n);
    } else {
      cache.remove(CACHE_KEYS.ACCOUNT);
    }
  })

  watch(openid, n => {
    if (n) {
      cache.set(CACHE_KEYS.OPENID, n);
    } else {
      cache.remove(CACHE_KEYS.OPENID);
    }
  })

  return { account, openid, isIMInitialized, doctorInfo, login, getDoctorInfo, initIMAfterLogin, logout }
})