import { cmText, createSimpleText, getImage, getQR } from "./utils";
import { CertData } from "./CertData";

const {
    Document, Paragraph, PageOrientation,
    FrameAnchorType,
    TextRun,
    AlignmentType,
    BorderStyle,
} = window.docx;

const imgFondoDoc = getImage({
    name: "fondo_seg_op_maq_pes.png",
    height: 21.02,
    width: 29.63,
    horizontalOffset: 0.03,
    verticalOffset: 0.04,
    behindDocument: true,
});

const imgCIP = getImage({
    name: "colegio_ingenieros_logo.png",
    height: 2.02,
    width: 2.02,
    horizontalOffset: 25.53,
    verticalOffset: 14.17,
});

const imgMundomaq = getImage({
    name: "mundo_maq.png",
    height: 4.82,
    width: 6.73,
    horizontalOffset: 20.87,
    verticalOffset: 0.9,
});

const imgHazescorp = getImage({
    name: "logo_hazescorp.png",
    height: 2.95,
    width: 3.33,
    horizontalOffset: 0.4,
    verticalOffset: 17.73,
});

const imgEEG = getImage({
    name: "eeg_logo.png",
    height: 2.38,
    width: 4.94,
    horizontalOffset: 1.66,
    verticalOffset: 1.6,
});



// SEGURIDAD EN LA OPERACIÓN DE MAQUINARIA PESADA
const tCourse = createSimpleText({
    xPosition: 5.5,
    yPosition: 6.65,
    width: 12.83,
    height: 1.5,
    text: "SEGURIDAD EN LA OPERACIÓN DE MAQUINARIA PESADA",
    size: 44,
    bold: true,
});

// En temas de...
const tTopics = createSimpleText({
    xPosition: 1.65,
    yPosition: 8.9,
    width: 20.42,
    height: 1.5,
    text: "En los temas de: Seguridad en maquinaria pesada, IPERC, Prevención de riesgos con Maquinas, Medidas de prevención con máquinas en movimientos de tierra, Seguridad en la operación, Procedimiento de mantenimiento, Correcto llenado de IPERC, Correcto llenado de Check List, Mantenimiento Preventivo, predictivo, correctivo.",
    size: 22,
    font: "Arial",
    alignment: AlignmentType.JUSTIFIED,
});

// Con una duracion...
const tHours = createSimpleText({
    xPosition: 1.65,
    yPosition: 10.7,
    width: 13.15,
    height: 0.5,
    text: "Con una duración de 36 horas lectivas",
    size: 22,
    font: "Arial",
    alignment: AlignmentType.LEFT,
});

// Se expide certificado...
const tFinishLabel = createSimpleText({
    xPosition: 5.3,
    yPosition: 11.5,
    width: 13.15,
    height: 0.5,
    text: "Se expide el presente certificado para los fines que se estime conveniente",
    size: 22,
    font: "Arial",
    italics: true,
    alignment: AlignmentType.CENTER,
});


// Recuadro de foto
const photoSection = new Paragraph({
    frame: {
        position: {
            x: cmText(22.62),
            y: cmText(7),
        },
        height: cmText(3.57),
        width: cmText(2.81),
        anchor: {
            horizontal: FrameAnchorType.MARGIN,
            vertical: FrameAnchorType.MARGIN,
        },
    },
    children: [],
    border: {
        top: {
            style: BorderStyle.DASHED,
        },
        bottom: {
            style: BorderStyle.DASHED,
        },
        left: {
            style: BorderStyle.DASHED,
        },
        right: {
            style: BorderStyle.DASHED,
        },
    },
    alignment: AlignmentType.LEFT,
});


export async function segOpMaqPesCert(props: CertData<null>): Promise<Document> {
    const imgQR = await getQR({
        iid: props.certIId,
        dni: props.personDni,
        height: 2.01,
        width: 2.01,
        horizontalOffset: 25.53,
        verticalOffset: 16.43,
    });

    // FERNANDO ARAOZ
    const tName = createSimpleText({
        xPosition: 0,
        yPosition: 4.15,
        width: 23.56,
        height: 1.5,
        text: props.personFullName,
        size: 52,
        bold: true,
        underline: {},
    });

    // Identificado con DNI...
    const tContentPart1 = new Paragraph({
        frame: {
            position: {
                x: cmText(1.4),
                y: cmText(5.9),
            },
            width: cmText(20.92),
            height: cmText(0.6),
            anchor: {
                horizontal: FrameAnchorType.MARGIN,
                vertical: FrameAnchorType.MARGIN,
            },
        },
        children: [
            new TextRun({
                text: "Identificado con DNI N° ",
                font: "Arial",
                size: 22,
            }),
            new TextRun({
                text: props.personDni,
                font: "Arial",
                size: 24,
                bold: true,
            }),
            new TextRun({
                text: ", al haber aprobado el curso de capacitación sobre:",
                font: "Arial",
                size: 22,
            }),
        ],
        alignment: AlignmentType.CENTER,
    });

    // Fecha de Emision: ...
    const certificateDate = createSimpleText({
        xPosition: 19.25,
        yPosition: 16.2,
        width: 5.87,
        height: 0.5,
        text: `Fecha de Emisión:\t${props.certDay} / ${props.certMonth} / ${props.certYear}`,
        font: "Times New Roman",
        size: 20,
        alignment: AlignmentType.LEFT,
    });

    // N° XXXX-20XX-EEG
    const tCertCode = createSimpleText({
        xPosition: 22.15,
        yPosition: 10.85,
        width: 3.67,
        height: 0.8,
        text: `N° ${props.certCode}-${props.certYear}-EEG`,
        size: 20,
        alignment: AlignmentType.CENTER,
    });

    const doc = new Document({
        sections: [
            {
                properties: {
                    page: {
                        size: {
                            orientation: PageOrientation.LANDSCAPE,
                        },
                    },
                },
                children: [
                    tName,
                    tContentPart1,
                    tCourse,
                    tTopics,
                    tHours,
                    tFinishLabel,
                    certificateDate,
                    photoSection,
                    tCertCode,
                    new Paragraph({
                        children: [
                            imgFondoDoc,
                            imgQR,
                            imgCIP,
                            imgMundomaq,
                            imgHazescorp,
                            imgEEG,
                        ],
                    }),
                ],
            },
        ],
    });

    // Return document as a buffer
    return doc;
}
