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

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



const imgFondoDoc = getImage({
    name: "fondo_certificado_manejo_defensivo.png",
    height: 21.23,
    width: 29.8,
    horizontalOffset: 0,
    verticalOffset: -0.05,
    behindDocument: true,
});

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




// CERTIFICADO
const tCertificate = createSimpleTextP({
    xPosition: 0.17,
    yPosition: 5.9,
    width: 11.05,
    height: 1.72,
    text: "CERTIFICADO",
    size: 72,
    font: "Times New Roman",
    bold: true,
});

// Otorgado a
const tExpediteText = createSimpleText({
    xPosition: -1.08,
    yPosition: 5.4,
    width: 3,
    height: 0.7,
    text: "Otorgado a:",
    size: 22,
});

// IPERC
const tCourse = createSimpleText({
    xPosition: -0.44,
    yPosition: 8,
    width: 20.92,
    height: 1.5,
    text: "LUCHA CONTRA INCENDIOS",
    size: 44,
    bold: true,
});

// En temas de...
const tTopics = createSimpleTextP({
    xPosition: 2,
    yPosition: 11.5,
    width: 20.92,
    height: 1.5,
    text: "En temas de: Concepto Básico, Combustión, Distintas Clases de Combustión, Clases de Fuego, Propagación de Calor, Formas de Transmisión, Uso de extintores, Amago de Incendios, equivalente a 24 horas lectivas.",
    size: 22,
    font: "Times New Roman",
    alignment: AlignmentType.CENTER,
});

// Se expide certificado...
const tFinishLabel = createSimpleTextP({
    xPosition: 1.9,
    yPosition: 12.6,
    width: 20.92,
    height: 0.75,
    text: "Se expide el presente certificado para los fines que se estime conveniente",
    size: 22,
    font: "Times New Roman",
    alignment: AlignmentType.CENTER,
});

// Recuadro de foto
const photoSection = new Paragraph({
    frame: {
        position: {
            x: cmText(23.35),
            y: cmText(8.3),
        },
        width: cmText(2.81),
        height: cmText(3.57),
        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 luchaContraIncendiosCert(props: CertData<null>): Promise<Document> {
    const imgQR = await getQR({
        iid: props.certIId,
        dni: props.personDni,
        height: 2.45,
        width: 2.45,
        horizontalOffset: 26.4,
        verticalOffset: 17.65,
    });

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

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

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

    // N° XXXX-20XX-EEG
    const tCertCode = createSimpleText({
        xPosition: 23,
        yPosition: 12,
        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: [
                    tCertificate,
                    tExpediteText,
                    tName,
                    tContentPart1,
                    tCourse,
                    tTopics,
                    tFinishLabel,
                    certificateDate,
                    photoSection,
                    tCertCode,
                    // tMTCLabel,
                    new Paragraph({
                        children: [
                            imgFondoDoc,
                            imgQR,
                            imgCIP,
                        ],
                    }),
                ],
            },
        ],
    });

    // Return document as a buffer
    return doc;
}

