import { Matpel, cmText, getImage, getMatpelHours, getMatpelLabel, getQR } from "./utils";
import { CertData } from "./CertData";

/*
import {
    Document, Packer, Paragraph, PageOrientation,
    FrameAnchorType,
    TextRun,
    AlignmentType,
} from "docx";
// */

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

const imgFondoDoc = getImage({
    name: "fondo_certificado.png",
    height: 20.97,
    width: 29.8,
    horizontalOffset: 0,
    verticalOffset: 0,
    behindDocument: true,
});

const imgMatpel = getImage({
    name: "matpel-logo.png",
    height: 2.81,
    width: 2.81,
    horizontalOffset: 0.7,
    verticalOffset: 0.7,
});

const imgCIP = getImage({
    name: "colegio_ingenieros_logo.png",
    height: 2.15,
    width: 2.15,
    horizontalOffset: 0.91,
    verticalOffset: 3.64,
});

const imgCEE = getImage({
    name: "cee_logo.png",
    height: 2.22,
    width: 3.11,
    horizontalOffset: 0.29,
    verticalOffset: 18,
});

const imgMTC = getImage({
    name: "mtc_logo.png",
    height: 1.3,
    width: 4.08,
    horizontalOffset: 5.13,
    verticalOffset: 19.33,
});

const imgEEG = getImage({
    name: "eeg_f_logo.png",
    height: 4.39,
    width: 6.4,
    horizontalOffset: 23.35,
    verticalOffset: 0,
});

const imgOSHA = getImage({
    name: "osha_logo.png",
    height: 1.55,
    width: 4.46,
    horizontalOffset: 24.34,
    verticalOffset: 4.95,
});

const imgAguila = getImage({
    name: "aguila_logo.png",
    height: 2.62,
    width: 2.68,
    horizontalOffset: 25.45,
    verticalOffset: 6.57,
});

const imgMichigan = getImage({
    name: "michigan_logo.png",
    height: 2.47,
    width: 2.6,
    horizontalOffset: 25.59,
    verticalOffset: 9.54,
});

const imgCEEM = getImage({
    name: "ceem_logo.jpg",
    height: 1,
    width: 3.99,
    horizontalOffset: 24.97,
    verticalOffset: 12.5,
});

const imgEATE = getImage({
    name: "eate_logo.jpg",
    height: 2.21,
    width: 3.28,
    horizontalOffset: 25.54,
    verticalOffset: 14.09,
});




/**
 * Generates a certificate in format DOCX and in-place.
 *
 * @param props Data to use in the certificate
 * @returns A buffer of the DOCX document
 */
export async function matpelCert(props: CertData<Matpel>): Promise<Document> {
    const certificateName = new Paragraph({
        frame: {
            position: {
                x: cmText(3.13),
                y: cmText(1.9),
            },
            width: cmText(14.28),
            height: cmText(3.19),
            anchor: {
                horizontal: FrameAnchorType.MARGIN,
                vertical: FrameAnchorType.MARGIN,
            },
        },
        children: [
            new TextRun({
                text: `CAPACITACIÓN EN MANEJO; MANIPULACIÓN Y TRANSPORTE DE MATERIALES Y RESIDUOS PELIGROSOS - ${getMatpelLabel(props.matpel)}`,
                bold: true,
                font: "Arial",
                size: 32,
            }),
        ],
        alignment: AlignmentType.CENTER,
    });

    const certificatePersonName = new Paragraph({
        frame: {
            position: {
                x: cmText(0),
                y: cmText(6.5),
            },
            width: cmText(23),
            height: cmText(1.5),
            anchor: {
                horizontal: FrameAnchorType.MARGIN,
                vertical: FrameAnchorType.MARGIN,
            },
        },
        children: [
            new TextRun({
                text: props.personFullName.toUpperCase(),
                bold: true,
                font: "Arial",
                allCaps: true,
                size: 48,
                underline: {},
            }),
        ],
        alignment: AlignmentType.CENTER,
    });

    // "Se expide el presente certificado a:"
    const certificateExpedite = new Paragraph({
        frame: {
            position: {
                x: cmText(2.85),
                y: cmText(5.15),
            },
            width: cmText(14.28),
            height: cmText(1),
            anchor: {
                horizontal: FrameAnchorType.MARGIN,
                vertical: FrameAnchorType.MARGIN,
            },
        },
        children: [
            new TextRun({
                text: "Se expide el presente certificado a:",
                font: "Arial",
                size: 22,
            }),
        ],
        alignment: AlignmentType.CENTER,
    });

    // N° XXXX-202X-EEG
    const certificateCode = new Paragraph({
        frame: {
            position: {
                x: cmText(17.1),
                y: cmText(5.35),
            },
            width: cmText(3.68),
            height: cmText(0.7),
            anchor: {
                horizontal: FrameAnchorType.MARGIN,
                vertical: FrameAnchorType.MARGIN,
            },
        },
        children: [
            new TextRun({
                text: `N° ${props.certCode}-${props.certYear}-EEG`,
                font: "Arial",
                size: 20,
            }),
        ],
        alignment: AlignmentType.CENTER,
    });

    // Identificado con dni ...
    const certificateBody = new Paragraph({
        frame: {
            position: {
                x: cmText(1.51),
                y: cmText(8.35),
            },
            width: cmText(20.08),
            height: cmText(3.7),
            anchor: {
                horizontal: FrameAnchorType.MARGIN,
                vertical: FrameAnchorType.MARGIN,
            },
        },
        children: [
            new TextRun({
                text: "Identificado con DNI N° ",
                font: "Arial",
                size: 24,
            }),
            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: 24,
            }),
            new TextRun({
                text: `MANEJO DE MATERIALES Y RESIDUOS PELIGROSOS - ${getMatpelLabel(props.matpel)}`,
                font: "Arial",
                size: 24,
                bold: true,
            }),
            new TextRun({
                text: `, según lo estipulado en la Ley N°28256 (ley que regula el Transporte de Materiales y Residuos Peligrosos) y Decreto Supremo N° 021-2008-MTC (Reglamento Nacional de Transporte Terrestre de Materiales y Residuos Peligrosos) con una duración de ${getMatpelHours(props.matpel)} horas lectivas.`,
                font: "Arial",
                size: 24,
            }),
        ],
        alignment: AlignmentType.JUSTIFIED,
    });

    // Se expide certificado...
    const certificateFinishLabel = new Paragraph({
        frame: {
            position: {
                x: cmText(1.52),
                y: cmText(11.25),
            },
            width: cmText(20.1),
            height: cmText(0.8),
            anchor: {
                horizontal: FrameAnchorType.MARGIN,
                vertical: FrameAnchorType.MARGIN,
            },
        },
        children: [
            new TextRun({
                text: "Se expide certificado para los fines que se estime conveniente.",
                font: "Arial",
                size: 24,
            }),
        ],
        alignment: AlignmentType.CENTER,
    });

    // Fecha de Emision: ...
    const certificateDate = new Paragraph({
        frame: {
            position: {
                x: cmText(16),
                y: cmText(16.5),
            },
            width: cmText(9),
            height: cmText(1.4),
            anchor: {
                horizontal: FrameAnchorType.MARGIN,
                vertical: FrameAnchorType.MARGIN,
            },
        },
        children: [
            new TextRun({
                text: `Fecha de Emisión:\t\t${props.certDay} / ${props.certMonth} / ${props.certYear}`,
                font: "Arial",
                size: 20,
                break: 1,
            }),
            new TextRun({
                text: `Fecha de Expiración:\t\t${props.certDay} / ${props.certMonth} / ${parseInt(props.certYear, 10) + 1}`,
                font: "Arial",
                size: 20,
                break: 1,
            }),
        ],
        alignment: AlignmentType.LEFT,
    });

    const imgQR = await getQR({
        iid: props.certIId,
        dni: props.personDni,
        height: 2.47,
        width: 2.47,
        horizontalOffset: 26.3,
        verticalOffset: 16.48,
    });

    const doc = new Document({
        sections: [
            {
                properties: {
                    page: {
                        size: {
                            orientation: PageOrientation.LANDSCAPE,
                        },
                    },
                },
                children: [
                    certificateName,
                    certificatePersonName,
                    certificateExpedite,
                    certificateCode,
                    certificateBody,
                    certificateFinishLabel,
                    certificateDate,
                    new Paragraph({
                        children: [
                            imgFondoDoc,
                            imgMatpel,
                            imgCIP,
                            imgCEE,
                            imgMTC,
                            imgEEG,
                            imgOSHA,
                            imgAguila,
                            imgMichigan,
                            imgCEEM,
                            imgEATE,
                            imgQR,
                        ],
                    }),
                ],
            },
        ],
    });

    // Return document
    return doc;
}

