import {
  waitExamTaskCount,
  stmmsTaskCount,
  analysisTaskCount,
  savePreviewLog,
} from "./api";
import { calcSum } from "@/plugins/utils";

const state = {
  waitTaskCount: 0,
  waitTask: {
    flow: 0,
    stmms: 0,
    analysis: 0,
  },
  waitTypes: [],
  // createExamAndPrintTask
  infoExamTask: {},
  infoExamTaskDetail: {},
  infoExamPrintPlan: {},
  infoPrintTask: {},
  // apply-task
  editType: "",
  examTask: {},
  curTaskApply: {},
  taskStatus: {},
  auditLogCache: { paper: {}, card: {} },
};

const mutations = {
  setWaitTaskCount(state, waitTaskCount) {
    state.waitTaskCount = waitTaskCount;
  },
  setWaitTask(state, waitTask) {
    state.waitTask = waitTask;
  },
  setWaitTypes(state, waitTypes) {
    state.waitTypes = waitTypes;
  },
  updateWaitTask(state, data) {
    const waitTask = Object.assign({}, state.waitTask, data);
    state.waitTask = waitTask;
    const count = calcSum(Object.values(state.waitTask));
    state.waitTaskCount = count;
  },
  // createExamAndPrintTask
  updateTaskInfo(state, data) {
    Object.entries(data).forEach(([key, val]) => {
      state[key] = Object.assign({}, state[key], val);
    });
  },
  resetTaskInfo(state) {
    state.infoExamTask = {};
    state.infoExamTaskDetail = {};
    state.infoExamPrintPlan = {};
    state.infoPrintTask = {};
  },
  // apply-task
  setTaskStatus(state, taskStatus) {
    state.taskStatus = taskStatus;
  },
  setEditType(state, editType) {
    state.editType = editType;
  },
  setExamTask(state, examTaskPartial) {
    state.examTask = Object.assign({}, state.examTask, examTaskPartial);
  },
  setCurTaskApply(state, curTaskApplyPartial) {
    state.curTaskApply = Object.assign(
      {},
      state.curTaskApply,
      curTaskApplyPartial
    );
  },
  setAuditLogCache(state, auditLogCache) {
    state.auditLogCache = auditLogCache;
  },
};

const actions = {
  async updateWaitTaskCount({ state, commit }) {
    const task = [
      {
        type: "flow",
        func: waitExamTaskCount,
      },
      {
        type: "stmms",
        func: stmmsTaskCount,
      },
      {
        type: "analysis",
        func: analysisTaskCount,
      },
    ];
    let countAll = task
      .filter((item) => state.waitTypes.includes(item.type))
      .map((item) => item.func());
    const counts = await Promise.all(countAll).catch(() => {});

    if (!counts) return;

    let waitTask = {};
    counts.forEach((count, index) => {
      waitTask[task[index].type] = count;
    });
    commit("setWaitTask", waitTask);

    const count = calcSum(counts);
    commit("setWaitTaskCount", count || 0);
  },
  async addPreviewLog({ state, commit }, { attachment, type }) {
    if (!state.taskStatus.IS_AUDIT) return;
    const id = type === "paper" ? attachment.attachmentId : attachment.cardId;
    if (!Object.prototype.hasOwnProperty.call(state.auditLogCache[type], id))
      return;

    state.auditLogCache[type][id] = true;
    await savePreviewLog({
      examTaskId: state.curTaskApply.examTaskId,
      paperType: attachment.name,
      type,
    });
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
