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

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

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

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

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

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

/** 更新评卷UI */
export async function updateUISetting(
  mode?: Setting["mode"],
  uiSetting?: UISetting
) {
  const form = new FormData();
  uiSetting && form.append("uiSetting", JSON.stringify(uiSetting));
  mode && form.append("mode", mode);
  return httpApp.post<void>("/mark/updateSetting", form);
}

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

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

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

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

/** 获取用户信息 */
export async function changeUserInfo(name: string, password?: string) {
  const form = new FormData();
  form.append("name", name);
  password && form.append("password", password);
  return httpApp.post<void>("/mark/changeName", form);
}

/** 评卷用户退出 */
export function doLogout() {
  window.location.href = "/mark/logout";
}

/** 评卷用户选择分组 */
export async function doSwitchGroup(markerId: number) {
  const form = new FormData();
  form.append("markerId", "" + markerId);
  return httpApp.post<CommonResponse>("/mark/subjectSelect", form);
}

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

  let markResult = store.currentTask?.markResult;
  markResult.problem = true;
  markResult.unselective = false;
  markResult.problemTypeId = problemId;
  markResult.markerScore = null;
  markResult.scoreList = [];
  markResult.specialTagList = [];
  markResult.trackList = [];

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

  return httpApp.post<CommonResponse>("/mark/saveTask", markResult);
}

/** 评卷用户选择试卷的为未选做 */
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>("/mark/saveTask", markResult);
}
