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

const FILL_LINE_PROP = {
  type: "FILL_LINE",
  sign: "subjective",
  topicName: "",
  topicNo: null,
  fillCountPerLine: 4,
  fillCount: null,
  lineSpacing: 40,
  questionLineType: "custom",
  paperStruct: {},
};

const MODEL = {
  type: "FILL_LINE",
  x: 0,
  y: 0,
  w: 0,
  h: 40,
  minHeight: 40,
  sign: "subjective",
  topicName: "",
  topicNo: null,
  fillCountPerLine: 4,
  lineSpacing: 40,
  questionNo: "1",
  fillCount: 1,
  isCovered: false,
  isLast: false,
  isFirst: false,
};

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

// questions:[{questionNo,fillCount}]
const getFullModel = (modelProp) => {
  // console.log(modelProp);
  const parent = deepCopy(modelProp);
  const childModel = objAssign(MODEL, parent);

  const numPerLine = childModel.fillCountPerLine;

  let elements = [];
  const questions = parent.paperStruct.questions || [];
  questions.forEach((question, index) => {
    const questionFillCount = parent.fillCount || question.fillCount;
    const maxLineNumberPerQuestion = Math.ceil(questionFillCount / numPerLine);

    const questionHeight = childModel.lineSpacing * maxLineNumberPerQuestion;

    let child = Object.assign({}, childModel, {
      id: getElementId(),
      h: index ? questionHeight : questionHeight + 34,
      isLast: false,
      isFirst: !index,
      questionNo: question.questionNo,
      fillCount: questionFillCount,
      parent,
    });
    child.minHeight = child.h;
    elements[index] = child;
  });
  elements[elements.length - 1].isLast = true;

  return elements;
};

export { MODEL, getModel, getFullModel };
