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

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

const imgFondoDoc = getImage({
    name: "fondo_super_escolta.png",
    height: 20.99,
    width: 29.69,
    horizontalOffset: 0,
    verticalOffset: 0,
    behindDocument: true,
});

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

const imgEATE = getImage({
    name: "eate_logo.jpg",
    height: 2.28,
    width: 3.39,
    horizontalOffset: 0.94,
    verticalOffset: 15.67,
});

const imgMTC = getImage({
    name: "mtc_logo.png",
    height: 1.6,
    width: 5,
    horizontalOffset: 0.9,
    verticalOffset: 18.3,
});

const imgOSHA = getImage({
    name: "osha_logo.png",
    height: 1.99,
    width: 5.72,
    horizontalOffset: 22.58,
    verticalOffset: 1.29,
});

const imgMatpelTransparent = getImage({
    name: "logo_matpel_transparente.png",
    height: 18.63,
    width: 18.63,
    horizontalOffset: 5.38,
    verticalOffset: 1.38,
});



const tCertificate = createSimpleTextP({
    xPosition: 9.18,
    yPosition: 2.91,
    width: 11.3,
    height: 1.46,
    text: "CERTIFICADO",
    size: 72,
    font: "Times New Roman",
    bold: true,
});

// Se expide el presente
const tLabel3 = createSimpleTextP({
    xPosition: 11.08,
    yPosition: 5.43,
    width: 7.74,
    height: 0.5,
    text: "Se expide el presente a:",
    size: 22,
    font: "Arial",
});

// SUPERVISOR ESCOLTA MATPEL
const tCourse = createSimpleTextP({
    xPosition: 7.28,
    yPosition: 8.75,
    width: 15.42,
    height: 1.5,
    text: "SUPERVISOR ESCOLTA MATPEL",
    size: 52,
    bold: true,
});

// Por haber aprobado la dormacion...
const tLabel2 = createSimpleTextP({
    xPosition: 10.93,
    yPosition: 7.95,
    width: 7.74,
    height: 0.6,
    text: "Por haber aprobado la formación en el curso",
    size: 22,
    font: "Arial",
});

// Temas tratados...
const tTopics = createSimpleTextP({
    xPosition: 5.04,
    yPosition: 10.3,
    width: 19.34,
    height: 1.5,
    text: "Temas tratados: Inspección de Unidades y Llenado de Herramientas de Gestión Check List, IPERC, Inspección básica de Kit de Emergencias, Parada de Controles e Inspección, Delimitación y Evaluación del Evento, Sistemas de Comando de Incidentes, Practicas Reales en caso de Derrames de MATPEL con Trajes, Evacuación de Pacientes Afectados con Camioneta, Primeros Auxilios básico, Con una duración de 36 horas lectivas.",
    size: 22,
    font: "Arial",
    alignment: AlignmentType.JUSTIFIED,
});

// Respaldado por:
const tHours = createSimpleTextP({
    xPosition: 1.15,
    yPosition: 15,
    width: 3.07,
    height: 0.5,
    text: "Respaldado por:",
    size: 22,
    font: "Arial",
    italics: true,
    alignment: AlignmentType.LEFT,
});

// Chile
const tChile = createSimpleTextP({
    xPosition: 3.07,
    yPosition: 17.83,
    width: 1.43,
    height: 0.5,
    text: "CHILE",
    size: 22,
    font: "Times New Roman",
    bold: true,
    alignment: AlignmentType.LEFT,
});

// Se expide certificado...
const tFinishLabel = createSimpleTextP({
    xPosition: 8.22,
    yPosition: 12.68,
    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(25),
            y: cmText(5.2),
        },
        height: cmText(3.57),
        width: cmText(2.81),
        anchor: {
            horizontal: FrameAnchorType.PAGE,
            vertical: FrameAnchorType.PAGE,
        },
    },
    children: [],
    border: {
        top: {
            style: BorderStyle.DASHED,
            size: 2,
        },
        bottom: {
            style: BorderStyle.DASHED,
            size: 2,
        },
        left: {
            style: BorderStyle.DASHED,
            size: 2,
        },
        right: {
            style: BorderStyle.DASHED,
            size: 2,
        },
    },
    alignment: AlignmentType.LEFT,
});


export async function supervisorEscolta(props: CertData<null>): Promise<Document> {
    const imgQR = await getQR({
        iid: props.certIId,
        dni: props.personDni,
        height: 2.5,
        width: 2.5,
        horizontalOffset: 25.85,
        verticalOffset: 17.49,
    });

    // FERNANDO ARAOZ
    const tName = createSimpleTextP({
        xPosition: 3.78,
        yPosition: 6.02,
        width: 22.07,
        height: 1.5,
        text: props.personFullName,
        size: 52,
        bold: true,
        underline: {},
    });

    // Identificado con DNI...
    const tContentPart1 = new Paragraph({
        frame: {
            position: {
                x: cmText(11.84),
                y: cmText(7.35),
            },
            width: cmText(6.02),
            height: cmText(0.6),
            anchor: {
                horizontal: FrameAnchorType.PAGE,
                vertical: FrameAnchorType.PAGE,
            },
        },
        children: [
            new TextRun({
                text: "Identificado con DNI N° ",
                font: "Arial",
                size: 20,
            }),
            new TextRun({
                text: props.personDni,
                font: "Arial",
                size: 20,
                bold: true,
            }),
        ],
        alignment: AlignmentType.CENTER,
    });

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

    // N° XXXX-20XX-EEG
    const tCertCode = createSimpleTextP({
        xPosition: 24.59,
        yPosition: 9,
        width: 3.67,
        height: 0.5,
        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,
                    tLabel3,
                    tName,
                    tLabel2,
                    tContentPart1,
                    tCourse,
                    tTopics,
                    tHours,
                    tFinishLabel,
                    certificateDate,
                    photoSection,
                    tCertCode,
                    tChile,
                    new Paragraph({
                        children: [
                            imgFondoDoc,
                            imgQR,
                            imgEATE,
                            imgMTC,
                            imgOSHA,
                            imgEEG,
                            imgMatpelTransparent,
                        ],
                    }),
                ],
            },
        ],
    });

    // Return document as a buffer
    return doc;
}
