'use strict';

var jsUtil_esm = require('../../../node_modules/@mj-studio/js-util/dist/js-util.esm.js');
var dimension = require('../../../design/dimension.js');
var palette = require('../../../design/palette.js');
var Doc = require('../../util/Doc.js');

const SolutionItem = ({ imageSegments, index, offsetX, offsetY: _offsetY, segmentStartAt, answer, colorSet: [defaultColor, bordColor], }) => async (doc) => {
    const includeHeader = segmentStartAt === 0;
    let offsetY = _offsetY;
    doc.x = offsetX;
    doc.y = offsetY;
    if (includeHeader) {
        // problem number
        {
            Doc.Doc.font('bold')
                .get()
                .fillColor(defaultColor)
                .fontSize(16)
                .text(jsUtil_esm.padZero(index + 1, 2), offsetX, offsetY + 2);
        }
        // difficulty
        {
            const answerText = `${answer}`;
            const answerWidth = Doc.Doc.font('bold')
                .get()
                .fontSize(8)
                .fillColor(palette.default.gray900)
                .widthOfString(answerText);
            doc.text(answerText, offsetX + dimension.ProblemDimension.width - 12 - answerWidth, offsetY + 7);
            doc
                .roundedRect(offsetX + dimension.ProblemDimension.width - 12 - answerWidth - 2 - 12, offsetY + 6, 12, 12, 2)
                .fill(bordColor);
            const difficultyLabelWidth = Doc.Doc.font('bold').get().widthOfString('답');
            doc
                .fillColor(palette.default.white)
                .text('답', offsetX + dimension.ProblemDimension.width - 12 - answerWidth - 2 - difficultyLabelWidth - 2.5, offsetY + 7);
        }
        offsetY += dimension.ProblemDimension.solutionHeaderHeight + dimension.ProblemDimension.headerMarginBottom;
    }
    for (let i = 0; i < imageSegments.length; i++) {
        const { buffer, height } = imageSegments[i];
        doc.image(buffer, offsetX, offsetY, { width: dimension.ProblemDimension.width });
        offsetY += height;
    }
};

exports.SolutionItem = SolutionItem;
