import { getFillBackClozeCount } from "./paperCard";

const COMMON_QUESTION_TYPES = [
  "SINGLE_ANSWER_QUESTION",
  "MULTIPLE_ANSWER_QUESTION",
  "BOOL_ANSWER_QUESTION",
  "FILL_BLANK_QUESTION",
  "TEXT_ANSWER_QUESTION",
];
// paper json
export function getPaperJsonSimpleStructInfo(paperJson) {
  let struct = [];
  paperJson.paperDetails.forEach((detail) => {
    struct.push(`detail:${detail.number}-${detail.name}`);
    detail.paperDetailUnits.forEach((question) => {
      if (COMMON_QUESTION_TYPES.includes(question.questionType)) {
        question.question.number = question.number;
        const info = parseCommonTypeQuestion(question.question);
        struct.push(`${detail.number}-${info}`);
      } else {
        let infos = parseNestedTypeQuestion(question);
        infos.forEach((info) => {
          struct.push(`${detail.number}-${info}`);
        });
      }
    });
  });
  return struct.join("#");
}

function parseCommonTypeQuestion(question) {
  const structType = question.questionType;
  let info = `${question.number}:${structType}`;
  const choiceQs = ["SINGLE_ANSWER_QUESTION", "MULTIPLE_ANSWER_QUESTION"];
  if (choiceQs.includes(structType)) {
    info += `:${question.quesOptions.length}`;
  } else if (structType === "FILL_BLANK_QUESTION") {
    info += `:${getFillBackClozeCount(question.quesOptions)}`;
  } else {
    info += ":";
  }

  return info;
}

function parseNestedTypeQuestion(question) {
  let struct = [];
  let qinfo = `${question.number}`;
  question.question.subQuestions.forEach((subq) => {
    subq.number = subq.subNumber;
    const info = parseCommonTypeQuestion(subq);
    struct.push(`${qinfo}-${info}`);
  });
  return struct;
}

// paper struct
export function getPaperStructSimpleStructInfo(paperStruct) {
  let struct = [];
  paperStruct.details.forEach((detail) => {
    struct.push(`detail:${detail.number}-${detail.name}`);
    detail.units.forEach((question) => {
      if (COMMON_QUESTION_TYPES.includes(question.type)) {
        const info = parseStructCommonTypeQuestion(question);
        struct.push(`${detail.number}-${info}`);
      }
    });
  });
  return struct.join("#");
}

function parseStructCommonTypeQuestion(question) {
  const structType = question.type;
  let info = `${question.number}:${structType}`;
  const choiceQs = ["SINGLE_ANSWER_QUESTION", "MULTIPLE_ANSWER_QUESTION"];
  if (choiceQs.includes(structType)) {
    info += `:4`;
  } else if (structType === "FILL_BLANK_QUESTION") {
    info += `:1`;
  } else {
    info += ":";
  }

  return info;
}
