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_certificado_mecanica_basica.jpg",
    height: 21.23,
    width: 29.8,
    horizontalOffset: 0,
    verticalOffset: 0,
    behindDocument: true,
});

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

const imgMichigan = getImage({
    name: "michigan_logo.png",
    height: 1.96,
    width: 2.22,
    horizontalOffset: 0.31,
    verticalOffset: 18.86,
});

const tCertificate = createSimpleText({
    xPosition: 7.75,
    yPosition: 1.64,
    width: 9,
    height: 1.56,
    text: "CERTIFICADO",
    size: 60,
    bold: true,
});

// Se expide el presente certificado a:
const tExpediteText = createSimpleText({
    xPosition: 0.76,
    yPosition: 3.45,
    width: 7.88,
    height: 1,
    text: "Se expide el presente certificado a:",
    size: 22,
});

// MECANICA BASICA
const tCourse = createSimpleText({
    xPosition: 1.7,
    yPosition: 7,
    width: 20.08,
    height: 1.5,
    text: "MECÁNICA BÁSICA",
    size: 72,
    bold: true,
});

// En los temas de...
const tTopics = createSimpleText({
    xPosition: 1.7,
    yPosition: 8.9,
    width: 19.31,
    height: 1.5,
    text: "En los temas de: Lineamientos del Ministerio de Transportes y Comunicaciones (MTC), Operatividad efectiva del equipo. Procedimiento de operación correcta. Equipamiento y mantenimiento preventivo, Medidas de Control y Tipos de IPERC, Cuidados de la maquina durante la operación, Uso de pedales y controles, Funcionamiento del motor;",
    size: 22,
    alignment: AlignmentType.JUSTIFIED,
});

// Con una duracion de...
const tDuration = createSimpleText({
    xPosition: 1.7,
    yPosition: 11,
    width: 19.31,
    height: 1,
    text: "Con una duración de 12 horas lectivas.",
    size: 22,
    alignment: AlignmentType.JUSTIFIED,
});

// Se expide certificado...
const tFinishLabel = createSimpleText({
    xPosition: 1.52,
    yPosition: 11.65,
    width: 20.1,
    height: 0.8,
    text: "Se expide el presente certificado para los fines que se estime conveniente",
    size: 22,
    alignment: AlignmentType.CENTER,
    italics: true,
});

// Recuadro de foto
const photoSection = new Paragraph({
    frame: {
        position: {
            x: cmText(-1.5),
            y: cmText(-1.9),
        },
        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 mecanicaBasicaCert(props: CertData<null>): Promise<Document> {
    const imgQR = await getQR({
        iid: props.certIId,
        dni: props.personDni,
        height: 2.2,
        width: 2.2,
        horizontalOffset: 24.63,
        verticalOffset: 17.01,
    });

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

    // FERNANDO ARAOZ
    const tName = createSimpleText({
        xPosition: 0.38,
        yPosition: 4.35,
        width: 24.08,
        height: 1.51,
        text: props.personFullName,
        size: 52,
        bold: true,
        underline: {},
    });

    // Identificado con DNI...
    const tContentPart1 = new Paragraph({
        frame: {
            position: {
                x: cmText(1.7),
                y: cmText(6.16),
            },
            width: cmText(20.08),
            height: cmText(1),
            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: 22,
                bold: true,
            }),
            new TextRun({
                text: ", al haber aprobado el curso de capacitación en:",
                font: "Arial",
                size: 22,
            }),
        ],
        alignment: AlignmentType.LEFT,
    });

    // Fecha de Emision: ...
    const certificateDate = new Paragraph({
        frame: {
            position: {
                x: cmText(17.25),
                y: cmText(17),
            },
            width: cmText(7.5),
            height: cmText(0.9),
            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: 18,
            }),
            new TextRun({
                text: `Fecha de Expiración:\t\t${props.certDay} / ${props.certMonth} / ${parseInt(props.certYear, 10) + 1}`,
                font: "Arial",
                size: 18,
                break: 1,
            }),
        ],
        alignment: AlignmentType.LEFT,
    });


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

    // Return document as a buffer
    return doc;
}
