import { store } from "@/store/store";
import { httpApp } from "@/plugins/axiosApp";
import {
  Setting,
  UISetting,
  HistoryQueryParams,
  MarkStore,
  Group,
  Task,
  CommonResponse,
} from "@/types";
import vls from "@/utils/storage";

const getMarkInfo = () => {
  return vls.get("mark", {});
};

/** 清除评卷任务（之前锁住的任务之类的） */
export async function clearMarkTask() {
  return httpApp.post<void>("/api/mark/clear", {}, { params: getMarkInfo() });
}

/** 获取评卷设置 */
export async function getSetting() {
  return httpApp.post<Setting>(
    "/api/mark/getSetting",
    {},
    { params: getMarkInfo() }
  );
}

/** 获取评卷状态 */
export async function getStatus() {
  return httpApp.post<MarkStore["status"]>(
    "/api/mark/getStatus",
    {},
    { params: getMarkInfo() }
  );
}

/** 获取评卷分组 */
export async function getGroup() {
  return httpApp.post<Group[]>(
    "/api/mark/getGroup",
    {},
    { params: getMarkInfo() }
  );
}

/** 获取评卷任务 */
export async function getTask() {
  return httpApp.post<Task>("/api/mark/getTask", {}, { params: getMarkInfo() });
}

/** 更新评卷UI */
export async function updateUISetting(
  mode?: Setting["mode"],
  uiSetting?: UISetting
) {
  return httpApp.post<void>(
    "/api/mark/updateSetting",
    {},
    {
      params: {
        mode: mode || undefined,
        uiSetting: uiSetting ? JSON.stringify(uiSetting) : undefined,
        ...getMarkInfo(),
      },
    }
  );
}

/** 获取评卷历史任务 */
export async function getHistoryTask({
  pageNumber = 1,
  pageSize = 20,
  order = "markerTime",
  sort = "DESC",
  secretNumber = null,
  markerScore = null,
}: HistoryQueryParams) {
  return httpApp.post<Task[]>(
    "/api/mark/getHistory",
    {},
    {
      params: {
        pageNumber,
        pageSize,
        order,
        sort,
        secretNumber,
        markerScore,
        ...getMarkInfo(),
      },
    }
  );
}

/** 保存评卷任务(正常保存) */
export async function saveTask() {
  if (!store.currentTask?.markResult) return;

  let markResult = store.currentTask.markResult;
  markResult.problem = false;
  markResult.problemType = undefined;
  markResult.problemRemark = undefined;
  markResult.unselective = false;
  markResult.spent = Date.now() - store.currentTask.__markStartTime;
  markResult = { ...markResult };

  return httpApp.post<CommonResponse>("/api/mark/saveTask", markResult, {
    setGlobalMask: true,
    params: getMarkInfo(),
  });
}

/** 获取用户信息 */
export async function changeUserInfo(name: string, password?: string) {
  return httpApp.post<void>(
    "/api/mark/changeName",
    {},
    {
      params: {
        name,
        password: password || undefined,
      },
    }
  );
}

/** 评卷用户退出 */
export function doLogout() {
  // window.history.go(-1);
  window.close();
}

/** 评卷用户选择分组 */
// ps:已经没用了 3.3.0
export async function doSwitchGroup(markerId: number) {
  return httpApp.post<CommonResponse>(
    "/api/mark/subjectSelect",
    {},
    {
      params: { markerId, ...getMarkInfo() },
    }
  );
}

/** 评卷用户选择试卷的问题类型 */
export async function doProblemType({ problemType, problemRemark }) {
  if (!store.currentTask?.markResult) return;

  let markResult = store.currentTask?.markResult;
  markResult.problem = true;
  markResult.unselective = false;
  markResult.problemType = problemType;
  markResult.problemRemark = problemRemark || undefined;
  markResult.markerScore = null;
  markResult.scoreList = [];
  markResult.specialTagList = [];
  markResult.trackList = [];

  markResult.spent = Date.now() - store.currentTask.__markStartTime;
  markResult = { ...markResult };

  return httpApp.post<CommonResponse>("/api/mark/saveTask", markResult, {
    params: getMarkInfo(),
  });
}

/** 评卷用户选择试卷的为未选做 */
export async function doUnselectiveType() {
  if (!store.currentTask?.markResult) return;

  let markResult = store.currentTask?.markResult;
  markResult.problem = false;
  markResult.unselective = true;
  markResult.markerScore = -1;
  markResult.scoreList = [];
  markResult.specialTagList = [];
  markResult.trackList = [];

  markResult.spent = Date.now() - store.currentTask.__markStartTime;
  markResult = { ...markResult };

  return httpApp.post<CommonResponse>("/api/mark/saveTask", markResult, {
    params: getMarkInfo(),
  });
}
