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

const MODEL = {
  type: "FILL_QUESTION",
  x: 0,
  y: 0,
  w: 0,
  h: 114,
  minHeight: 114,
  sign: "objective",
  topicName: "",
  topicNo: null,
  nameFontSize: "14px",
  nameFontWeight: 400,
  startNumber: 1,
  questionsCount: 10,
  optionCount: 4,
  questionCountPerGroup: 5,
  groupPerLine: 4, // 小题纵向排列时，表示每行组数。小题横向排列时，表示每行小题数。
  optionDirection: "horizontal",
  questionDirection: "vertical",
  questionGap: 8,
  groupGap: 30,
  optionGap: 6,
  isBoolean: false, // 是否是判断题
  booleanType: BOOLEAN_TYPE[0],
  isMultiply: false, // 是否是多选题
  isCovered: false,
  fontSize: "14px",
};

const getModel = (preSetData = {}) => {
  const model = Object.assign({}, MODEL, preSetData);
  return {
    id: getElementId(),
    key: randomCode(),
    ...model,
  };
};

const getOptionStructInfo = (pageSize, columnNumber, model) => {
  // 选项纵向排列
  if (model.optionDirection === "vertical") {
    return { groupGap: 23, groupPerLine: 5 };
  }

  // 选项横向排列
  // 不同栏数，不同选项个数，每一行对应的组数
  // 以一行4题，每题5选项为标准展示效果
  const numberPerChildren = {
    "8K": {
      1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      2: [0, 0, 6, 5, 4, 3, 3, 2, 2, 2, 2, 2, 1],
      3: [0, 0, 4, 3, 3, 2, 2, 1],
      4: [0, 0, 3, 3, 2, 1],
    },
    A3: {
      2: [0, 0, 7, 6, 5, 4, 3, 3, 3, 2, 2, 2, 2, 1],
      3: [0, 0, 5, 4, 3, 2, 2, 2, 1],
      4: [0, 0, 3, 3, 2, 2, 1],
    },
    A4: {
      1: [0, 0, 7, 6, 5, 4, 3, 3, 3, 2, 2, 2, 2, 1],
      2: [0, 0, 3, 3, 2, 2, 1],
    },
  };
  const groupGapSet = {
    "8K": {
      2: [0, 0, 35, 30, 38, 70, 45, 90, 70, 60, 50, 20],
      3: [0, 0, 32, 35, 16, 60, 40, 30],
      4: [0, 0, 33, 27, 40, 30],
    },
    A3: {
      2: [0, 0, 33, 28, 27, 40, 40, 40, 27, 40, 40],
      3: [0, 0, 27, 27, 40, 40, 40, 40, 30],
      4: [0, 0, 33, 27, 40, 30],
    },
    A4: {
      1: [0, 0, 33, 28, 27, 40, 40, 40, 27, 40, 40],
      2: [0, 0, 33, 27, 40, 30],
    },
  };
  const groupGapList = groupGapSet[pageSize][columnNumber];
  const groupGap =
    model.optionCount >= groupGapList.length
      ? groupGapList.pop()
      : groupGapList[model.optionCount];

  const numList = numberPerChildren[pageSize][columnNumber];
  const groupPerLine =
    model.optionCount >= numList.length
      ? numList.pop()
      : numList[model.optionCount];

  return { groupGap, groupPerLine };
};

const getFullModel = (model, { pageSize, columnNumber }) => {
  const parent = { ...model };
  const { groupGap, groupPerLine } = getOptionStructInfo(
    pageSize,
    columnNumber,
    model
  );
  const numPerLine = groupPerLine * model.questionCountPerGroup;
  const total = Math.ceil(model.questionsCount / numPerLine);
  let elements = [];
  for (let i = 0; i < total; i++) {
    let child = Object.assign({}, parent, {
      id: getElementId(),
      key: randomCode(),
      groupPerLine,
      groupGap,
      startNumber: model.startNumber + i * numPerLine,
      questionsCount:
        i === total - 1 ? model.questionsCount - numPerLine * i : numPerLine,
      parent,
      isLast: i === total - 1,
    });

    if (model.optionDirection === "vertical") {
      const optionCount = model.optionCount + 1;
      const optionsHeight =
        10 * optionCount + (optionCount - 1) * model.optionGap + 20;
      child.h = i ? optionsHeight : optionsHeight + 34;
    } else {
      const optionCount =
        model.questionDirection === "vertical"
          ? Math.min(child.questionsCount, model.questionCountPerGroup)
          : Math.ceil(child.questionsCount / groupPerLine);
      const optionsHeight =
        10 * optionCount + (optionCount - 1) * model.questionGap + 20;
      child.h = i ? optionsHeight : optionsHeight + 34;
    }
    child.minHeight = child.h;

    elements[i] = child;
  }
  return elements;
};

export { MODEL, getModel, getFullModel };
