'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 ProblemItem = ({ imageSegments, index, offsetX, offsetY: _offsetY, segmentStartAt, difficulty, subtext, color, }) => async (doc) => {
    const includeProblemHeader = segmentStartAt === 0;
    let offsetY = _offsetY;
    doc.x = offsetX;
    doc.y = offsetY;
    if (includeProblemHeader) {
        // problem number
        {
            Doc.Doc.font('bold')
                .get()
                .fillColor(color)
                .fontSize(16)
                .text(jsUtil_esm.padZero(index + 1, 2), offsetX, offsetY + 2);
        }
        // difficulty
        if (difficulty) {
            {
                const difficultyWidth = Doc.Doc.font('bold')
                    .get()
                    .fontSize(8)
                    .fillColor(palette.default.gray700)
                    .widthOfString(difficulty);
                doc.text(difficulty, offsetX + dimension.ProblemDimension.width - 12 - difficultyWidth, offsetY + 7);
                const difficultyLabelWidth = Doc.Doc.font('medium').get().widthOfString('난이도');
                doc.text('난이도', offsetX + dimension.ProblemDimension.width - 12 - difficultyWidth - 2 - difficultyLabelWidth, offsetY + 7);
            }
        }
        // description
        if (subtext) {
            {
                Doc.Doc.font('medium')
                    .get()
                    .fontSize(8)
                    .fillColor(palette.default.gray400)
                    .text(subtext, offsetX, offsetY + 28);
            }
        }
        offsetY += dimension.ProblemDimension.problemHeaderHeight + 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.ProblemItem = ProblemItem;
