import getlogicStatus from './logical';

// 更新code
function updateCode(action, logic, pages, page) {
  const { skip_question_index: skipQuestionIndex, question_index: questionIndex } = logic;
  const startIndex = pages.findIndex(
    (page) => page.findIndex((index) => index === questionIndex) !== -1
  );
  if (startIndex < page) {
    if (skipQuestionIndex === -1) {
      action.code = 20011;
      action.msg = '成功结束页';
    } else if (skipQuestionIndex === -2) {
      action.code = 20004;
      action.msg = '甄别结束页';
    } else if (skipQuestionIndex === -3) {
      action.code = 20016;
      action.msg = '配额超限页';
    }
  }
}
// 更新分页pages(题后跳转逻辑)
function updatePagesAfter(pages, logic, jumpTo, page) {
  const { question_index: questionIndex, skip_question_index: skipQuestionIndex } = logic;
  const startIndex = pages.findIndex(
    (page) => page.findIndex((index) => index === questionIndex) !== -1
  );
  const endIndex = pages.findIndex(
    (page) => page.findIndex((questionIndex) => questionIndex === skipQuestionIndex) !== -1
  );
  if (endIndex !== -1) {
    const endQuestionIndex = pages[endIndex].findIndex(
      (questionIndex) => questionIndex === skipQuestionIndex
    );
    pages[endIndex].splice(0, endQuestionIndex);
    // 跳转到某页
    if (startIndex > endIndex && startIndex < page) {
      jumpTo.question_index = questionIndex;
      return (jumpTo.question_page = endIndex + 1);
    }
    pages.splice(startIndex + 1, endIndex - startIndex - 1);
  }
}

// 更新分页pages(题前设置逻辑)
function updatePagesBefore(pages, hideQuestionIndex) {
  const pagesIndex = pages.findIndex(
    (page) => page.findIndex((questionIndex) => questionIndex === hideQuestionIndex) !== -1
  );
  if (pagesIndex === -1) return;
  if (pages[pagesIndex].length === 1) {
    pages.splice(pagesIndex, 1);
  } else {
    const pageIndex = pages[pagesIndex].findIndex(
      (questionIndex) => questionIndex === hideQuestionIndex
    );
    pages[pagesIndex].splice(pageIndex, 1);
  }
}

// 自动填写
function autoFill(answerAutoFill, logic) {
  answerAutoFill.push({
    answer: logic.autofill.answer_insert,
    question_index: logic.question_index,
    question_type: logic.autofill.question_type
  });
}

// 选项隐藏逻辑
function updateOptionHidden(hideOptions, logic) {
  const { question_index: questionIndex, hide_option_index: hideOptionIndex } = logic;
  hideOptions.question_index = questionIndex;
  if (logic.logicStatus) {
    hideOptions.option_key.push(...hideOptionIndex.map((opt) => opt.option_key));
  }
}

// 模拟答题接口
export default function answerMock(questionsData, page) {
  const data = {
    action: { code: 20010, msg: '答案已记录' },
    jump_to: {},
    hide_options: {
      option_key: []
    },
    answer_info_autofill: [],
    pages: JSON.parse(JSON.stringify(questionsData.answer.pages_init))
  };
  const logics = getlogicStatus(questionsData);
  logics.forEach((logic) => {
    if (logic.logicStatus && logic.skip_type === 0) {
      // 20240731
      // fix: Q1,Q2,Q3,Q4,Q5. Q1 A 跳转至 Q3; Q1 B 跳转至 Q2; Q2 always 跳转至 Q5. 当 Q1 选择 A, 页面空白/作答结束
      if (!data.pages[page - 1].includes(logic.question_index)) {
        return;
      }
      // 题后跳转逻辑
      if (logic.skip_question_index < 0) {
        return updateCode(data.action, logic, data.pages, page);
      }
      updatePagesAfter(data.pages, logic, data.jump_to, page);
    } else if (logic.logicStatus && logic.skip_type === 1) {
      // 题前设置逻辑
      updatePagesBefore(data.pages, logic.question_index);
    } else if (logic.logicStatus && logic.skip_type === 3) {
      // 自动填写逻辑
      autoFill(data.answer_info_autofill, logic);
    } else if (logic.skip_type === 4) {
      // 只计算跳转后所在页面的隐藏逻辑，否则会出现只返回最后一道隐藏选项题目的情况，导致失效
      const toPage = page + 1;
      const hasHiddenLogicQuizPage =
        data.pages.findIndex((page) => page.includes(logic.question_index)) + 1;
      if (hasHiddenLogicQuizPage === toPage) {
        // 选项隐藏逻辑
        updateOptionHidden(data.hide_options, logic);
      }
    }
  });
  // 更新问卷状态
  if (page === data.pages.length) {
    data.action.code = 20011;
    data.action.msg = '成功结束页';
  }
  // 拒绝知情同意书
  // const refuseIndex = questionsData.questions.findIndex(
  //   (question) => question.question_type === 23 && question.answer?.value === "2"
  // );
  // if (refuseIndex !== -1) {
  //   data.action.code = 20013;
  //   data.action.msg = "不同意继续参与，已结束作答";
  // }
  // 返回数据
  return data;
}
