import { isObject } from '@/utils/verify';
import { STORAGE_COMMANDS, tauriSessionStorage } from '@/utils/tauri/tauri-session';
import type { BriUserInfo } from '@/api/login';
import { tauri_store_page_curr_course, tauri_store_page_curr_tutorship, tauri_store_page_user_info } from '@/utils/constant';
import { getCurrentWindow } from '@tauri-apps/api/window';
import { ref } from 'vue';

/**
 * 课程信息（悬浮列表窗口只关心必要字段）
 */
export type BriCourse = {
  /** 课程 ID */
  id: number;
  /** 班级 ID */
  classId: number;
  /** 其他字段 */
  [key: string]: unknown;
};

/**
 * 评讲信息（悬浮列表窗口只关心必要字段）
 */
export type BriTutorship = {
  /** 评讲 ID */
  id: number;
  /** 其他字段 */
  [key: string]: unknown;
};

/**
 * useTauriSessionStorage
 */
export function useTauriSessionStorage() {
  const userInfo = ref<BriUserInfo | null>(null);
  const currTutorship = ref<BriTutorship | null>(null);
  const currCourse = ref<BriCourse | null>(null);
  const currentWindow = getCurrentWindow();
  updateBaseDate();
  // 监听后端存储数据变更
  currentWindow.listen<[string, string]>(STORAGE_COMMANDS.CHANGE, ({ payload: [key, value] }) => {
    if (key === tauri_store_page_curr_tutorship) {
      const _currTutorship = isObject(value) ? value : null;
      if (_currTutorship && 'id' in _currTutorship) {
        currTutorship.value = _currTutorship as unknown as BriTutorship;
      }
    } else if (key === tauri_store_page_curr_course) {
      const _currCourse = isObject(value) ? value : null;
      if (_currCourse && 'id' in _currCourse && 'classId' in _currCourse) {
        currCourse.value = _currCourse as unknown as BriCourse;
      }
    } else if (key === tauri_store_page_user_info) {
      const _userInfo = isObject(value) ? value : null;
      if (_userInfo && 'id' in _userInfo && 'schoolId' in _userInfo) {
        userInfo.value = _userInfo as unknown as BriUserInfo;
      }
    }
  });

  // 监听后端数据删除事件
  currentWindow.listen<string>(STORAGE_COMMANDS.REMOVE, (event) => {
    const key = event.payload;
    if (key === tauri_store_page_curr_tutorship) {
      currTutorship.value = null;
    } else if (key === tauri_store_page_curr_course) {
      currCourse.value = null;
    } else if (key === tauri_store_page_user_info) {
      userInfo.value = null;
    }
  });

  // 监听后端清空事件
  currentWindow.listen(STORAGE_COMMANDS.CLEAR, () => {
    currTutorship.value = null;
    currCourse.value = null;
    userInfo.value = null;
  });

  /**
   * 跟新基础数据
   */
  function updateBaseDate() {
    tauriSessionStorage.get(tauri_store_page_user_info).then((value) => {
      const obj = isObject(value) ? value : null;
      if (obj && 'id' in obj && 'schoolId' in obj) {
        userInfo.value = obj as unknown as BriUserInfo;
      }
    });
    tauriSessionStorage.get(tauri_store_page_curr_tutorship).then((value) => {
      const obj = isObject(value) ? value : null;
      if (obj && 'id' in obj) {
        currTutorship.value = obj as unknown as BriTutorship;
      }
    });
    tauriSessionStorage.get(tauri_store_page_curr_course).then((value) => {
      const obj = isObject(value) ? value : null;
      if (obj && 'id' in obj && 'classId' in obj) {
        currCourse.value = obj as unknown as BriCourse;
      }
    });
  }

  return {
    /** 更新基础数据 */
    updateBaseDate,
    userInfo,
    currTutorship,
    currCourse,
  };
}
