import { httpApp } from "@/plugins/axiosApp";
import type {
  MarkStore,
  StudentAnswer,
  OExamPaperJSON,
  ECSPaperJSON,
} from "@/types";
// import TempJson from './temp.json'

/** 获取学生答案JSON */
export async function getStudentAnswerJSON(url: string) {
  return httpApp.get<StudentAnswer[]>(url, { withCredentials: false });
}

/** 获取试卷JSON */
async function getPaperJSON(url: string) {
  return httpApp.get(url, {
    withCredentials: false,
  });
}

export async function getPaper(store: MarkStore) {
  const res = await getPaperJSON(store.setting.subject.paperUrl);
  store.setting.subject.questions = [];
  if (res.data.questions) {
    // 云平台格式
    const questions: ECSPaperJSON = res.data.questions;
    for (const q of questions) {
      const tempQuestion = {
        unionOrder: q.mainNumber + "-" + q.subNumber,
        body: q.body,
        parentBody: q.parentBody,
        answer: [q.answer],
        objective: null,
        options: null,
      };
      store.setting.subject.questions.push(tempQuestion);
    }
  } else {
    const details: OExamPaperJSON = res.data.details;
    for (const order1 of details) {
      for (const order2 of order1.questions) {
        if (order2.subQuestions) {
          if (order2.structType === 8) {
            store.setting.subject.questions.push(
              ...[order2.subQuestions[0], ...order2.subQuestions].map(
                (order3, i) => {
                  const unionOrder = `${order1.number}-${order2.number}${
                    i === 0 ? "" : "-" + order3.number
                  }`;
                  const options = i === 0 ? order3.options : null;
                  const parentBody = i === 0 ? order2.body : null;
                  const hideAnswer = i === 0
                  const tempQuestion = {
                    unionOrder: unionOrder,
                    body: order3.body,
                    parentBody,
                    answer: order3.answer,
                    objective: order3.objective,
                    options,
                    hideAnswer
                  };
                  return tempQuestion;
                }
              )
            );
          } else {
            for (const order3 of order2.subQuestions) {
              const tempQuestion = {
                unionOrder:
                  order1.number + "-" + order2.number + "-" + order3.number,
                body: order3.body,
                parentBody: order2.body,
                answer: order3.answer,
                objective: order3.objective,
                options: order3.options,
              };
              store.setting.subject.questions.push(tempQuestion);
            }
          }
        } else {
          const tempQuestion = {
            unionOrder: order1.number + "-" + order2.number,
            body: order2.body,
            parentBody: null,
            answer: order2.answer,
            objective: order2.objective,
            options: order2.options,
          };
          store.setting.subject.questions.push(tempQuestion);
        }
      }
    }
  }
}
