import { store } from "@/store/store";
import { unref } from "vue";

let hovering = false;
let timeoutId = -1;

export function addFocusTrack(
  groupNumber: number | undefined,
  mainNumber: number | undefined,
  subNumber: string | undefined,
  isMark?: boolean | undefined, // 是否是评卷，评卷时要考虑标记删除
  list?: any
) {
  hovering = true;

  timeoutId = setTimeout(() => {
    if (hovering) {
      _addFocusTrack(groupNumber, mainNumber, subNumber, isMark, list);
    }
  }, 200);
}

function _addFocusTrack(
  groupNumber: number | undefined,
  mainNumber: number | undefined,
  subNumber: string | undefined,
  isMark: boolean | undefined, // 是否是评卷，评卷时要考虑标记删除
  list?: any
) {
  store.focusTracks.splice(0);
  const listArr = unref(list);
  console.log("listArr:", listArr);
  if (listArr) {
    const trackList: any = list.map((q) => q.trackList).flat();

    trackList
      .filter((t) => {
        if (mainNumber) {
          return t.mainNumber === mainNumber && t.subNumber === subNumber;
        } else {
          return false;
        }
      })
      .forEach((t: any) => {
        // 回评时，如果没被删除
        const shouldAdd = isMark ? trackList.includes(t) : true;
        if (shouldAdd) {
          store.focusTracks.push(t);
        }
      });
    return;
  }

  if (groupNumber) {
    store.currentTask?.questionList
      ?.filter((q) => q.groupNumber === groupNumber)
      ?.map((q) => q.trackList)
      .flat()
      .forEach((t) => {
        // 回评时，如果没被删除
        const shouldAdd = isMark
          ? store.currentTask?.markResult.trackList.includes(t)
          : true;
        if (shouldAdd) store.focusTracks.push(t);
      });
  } else {
    store.currentTask?.questionList
      ?.map((q) => q.trackList)
      .flat()
      .filter((t) => {
        if (mainNumber) {
          return t.mainNumber === mainNumber && t.subNumber === subNumber;
        } else {
          return false;
        }
      })
      .forEach((t) => {
        // 回评时，如果没被删除
        const shouldAdd = isMark
          ? store.currentTask?.markResult.trackList.includes(t)
          : true;
        if (shouldAdd) store.focusTracks.push(t);
      });
  }
  // console.log(store.focusTracks);
}

let removeTrackTimer: number | null = null;

export const removeFocusTrack = function removeFocusTrack() {
  console.log("removeFocusTrack");
  hovering = false;
  clearTimeout(timeoutId);
  removeTrackTimer && clearTimeout(removeTrackTimer);
  removeTrackTimer = setTimeout(() => {
    if (!hovering) {
      store.focusTracks.splice(0);
    }
    // }, 3000);
  }, 200);
};
