import {
  getElementId,
  randomCode,
  objAssign,
  deepCopy,
} from "../../plugins/utils";
import { BOOLEAN_TYPE, ALPHABET } from "../../enumerate";

const FILL_QUESTION_PROP = {
  type: "FILL_QUESTION",
  sign: "objective",
  topicName: "",
  topicNo: null,
  questionCountPerGroup: 5,
  questionGap: 8,
  groupGap: 30,
  optionGap: 12,
  optionCount: null,
  isBoolean: false, // 是否是判断题
  booleanType: BOOLEAN_TYPE[0],
  isMultiply: false, // 是否是多选题
  paperStruct: {},
};

const MODEL = {
  type: "FILL_QUESTION",
  x: 0,
  y: 0,
  w: 0,
  h: 138,
  minHeight: 138,
  sign: "objective",
  topicName: "",
  topicNo: null,
  questionCountPerGroup: 5,
  questionGap: 8,
  groupGap: 30,
  optionGap: 12,
  questionGroup: [],
  isBoolean: false, // 是否是判断题
  booleanType: BOOLEAN_TYPE[0],
  isMultiply: false, // 是否是多选题
  isCovered: false,
  isLast: false,
  isFirst: false,
};

function getChoiceList(data) {
  if (data.isBoolean) {
    return data.booleanType.split(",");
  } else {
    return ALPHABET.slice(0, data.optionCount).split("");
  }
}

const getModel = (presetData) => {
  const model = objAssign(deepCopy(FILL_QUESTION_PROP), presetData);
  model.id = getElementId();
  model.key = randomCode();
  return model;
};

const getFullModel = (modelProp, { pageSize, columnNumber }) => {
  // console.log(modelProp);
  const parent = deepCopy(modelProp);
  const childModel = objAssign(MODEL, parent);

  // 不同栏数，不同选项个数，每一行对应的组数
  // 以一行4题，每题4选项为标准展示效果：选项序号占一个选项位置，即一行最多展示20个选项位置。
  // 可扩展其他分栏的最佳展示效果
  const lineMaxOptionCountConfig = {
    A3: {
      2: 20,
    },
    "8K": {
      2: 18,
    },
    A4: {
      1: 20,
    },
  };
  const lineMaxOptionCount = lineMaxOptionCountConfig[pageSize][columnNumber];
  const questions = parent.paperStruct.questions || [];
  let questionList = questions.map((item) => {
    let question = {
      ...item,
    };
    if (parent.optionCount) question.optionCount = parent.optionCount;
    question.choiceList = getChoiceList({
      ...question,
      isBoolean: parent.isBoolean,
      booleanType: parent.booleanType,
    });
    return question;
  });

  const getNextGroupQuestions = () => {
    let isEnough = false;
    let groupOptionCount = 0;
    let groups = [];
    do {
      const curGroup = questionList.splice(0, parent.questionCountPerGroup);
      if (curGroup.length) {
        const curGroupMaxOptionCount = Math.max.apply(
          null,
          curGroup.map((item) => item.optionCount)
        );
        if (
          curGroupMaxOptionCount + 1 + groupOptionCount <= lineMaxOptionCount ||
          !groups.length
        ) {
          groupOptionCount += curGroupMaxOptionCount + 1;
          groups.push(curGroup);
        } else {
          questionList = [...curGroup, ...questionList];
          isEnough = true;
        }
      } else {
        isEnough = true;
      }
    } while (!isEnough);

    return groups;
  };

  let elements = [];

  while (questionList.length) {
    const questionGroup = getNextGroupQuestions();
    let child = Object.assign({}, childModel, {
      id: getElementId(),
      key: randomCode(),
      isFirst: !elements.length,
      isLast: false,
      questionGroup,
      parent,
    });
    // console.log(questionGroup);
    const maxOptionCountPerGroup =
      questionGroup.length > 1
        ? childModel.questionCountPerGroup
        : questionGroup[0].length;
    const optionsHeight =
      14 * maxOptionCountPerGroup +
      (maxOptionCountPerGroup - 1) * childModel.questionGap +
      36;

    child.h = elements.length ? optionsHeight : optionsHeight + 34;
    child.minHeight = child.h;
    elements.push(child);
  }

  elements[elements.length - 1].isLast = true;

  return elements;
};

export { MODEL, getModel, getFullModel };
