import * as QR from "qrcode";

const { AlignmentType, FrameAnchorType, HorizontalPositionRelativeFrom,
    ImageRun, Paragraph, TextRun,
    VerticalPositionRelativeFrom, convertMillimetersToTwip} = window.docx;

// To define centimeters in a image's size
export function cm(centimeters: number) {
    return convertMillimetersToTwip((100 * centimeters) / 150);
}

// To define centimeters in a floating image's position
export function cmToEmu(cm: number) {
    return Math.round(cm * 360000);
}

// To define centimeters in a text
export function cmText(cm: number) {
    return Math.round(cm * 567);
}

type ImgConfig = {
    name: string,
    height: number,
    width: number,
    horizontalOffset: number,
    verticalOffset: number,
    behindDocument?: boolean,
}

// Fetches images from the server
async function getImageData(imageName:string): Promise<Blob> {
    const response = await fetch(`assets/img/${imageName}`);
    const blob = await response.blob();

    return blob;
}

export function getImage(data: ImgConfig): typeof ImageRun {
    return new ImageRun({
        // Magic path, based on dist folder
        data: getImageData(data.name),
        transformation: {
            height: cm(data.height),
            width: cm(data.width),
        },
        floating: {
            zIndex: 0,
            horizontalPosition: {
                relative: HorizontalPositionRelativeFrom.LEFT_MARGIN,
                offset: cmToEmu(data.horizontalOffset),
            },
            verticalPosition: {
                relative: VerticalPositionRelativeFrom.TOP_MARGIN,
                offset: cmToEmu(data.verticalOffset),
            },
            behindDocument: data.behindDocument ?? false,
        },
    });
}

export async function getQR(data : {
    /** ID of the entity in the DB */
    iid: number,
    dni: string,
    height: number,
    width: number,
    horizontalOffset: number,
    verticalOffset: number,
    behindDocument?: boolean,
}): Promise<typeof ImageRun> {
    // Old URL: https://www.eegsac.com/alumnoscertificados.php?DNI=${dni()}
    // New URL: https://eegsac.com/certificado/${dni()}
    const qr = await QR.toDataURL(`https://eegsac.com/certificado/${data.dni}?iid=${data.iid}`, {margin: 1});

    return new ImageRun({
        data: qr,
        transformation: {
            height: cm(data.height),
            width: cm(data.width),
        },
        floating: {
            zIndex: 0,
            horizontalPosition: {
                relative: HorizontalPositionRelativeFrom.LEFT_MARGIN,
                offset: cmToEmu(data.horizontalOffset),
            },
            verticalPosition: {
                relative: VerticalPositionRelativeFrom.TOP_MARGIN,
                offset: cmToEmu(data.verticalOffset),
            },
            behindDocument: data.behindDocument ?? false,
        },
    });
}

export function createSimpleText(data: {
    xPosition: number,
    yPosition: number,
    width: number,
    height: number,
    text: string,
    font?: string,
    bold?: boolean,
    size: number,
    underline?: Record<string, never>,
    alignment?: typeof AlignmentType,
    italics?: boolean,
    color?: string,
}): typeof Paragraph {

    return new Paragraph({
        frame: {
            position: {
                x: cmText(data.xPosition),
                y: cmText(data.yPosition),
            },
            width: cmText(data.width),
            height: cmText(data.height),
            anchor: {
                horizontal: FrameAnchorType.MARGIN,
                vertical: FrameAnchorType.MARGIN,
            },
        },
        children: [
            new TextRun({
                text: data.text,
                bold: data.bold,
                font: data.font ?? "Arial",
                size: data.size,
                underline: data.underline,
                italics: data.italics,
                color: data.color,
            }),
        ],
        alignment: data.alignment ?? AlignmentType.CENTER,
    });
}

export function createSimpleTextP(data: {
    xPosition: number,
    yPosition: number,
    width: number,
    height: number,
    text: string,
    font?: string,
    bold?: boolean,
    size: number,
    underline?: Record<string, never>,
    alignment?: typeof AlignmentType,
    italics?: boolean,
    color?: string,
}): typeof Paragraph {

    return new Paragraph({
        frame: {
            position: {
                x: cmText(data.xPosition),
                y: cmText(data.yPosition),
            },
            width: cmText(data.width),
            height: cmText(data.height),
            anchor: {
                horizontal: FrameAnchorType.PAGE,
                vertical: FrameAnchorType.PAGE,
            },
        },
        children: [
            new TextRun({
                text: data.text,
                bold: data.bold,
                font: data.font ?? "Arial",
                size: data.size,
                underline: data.underline,
                italics: data.italics,
                color: data.color,
            }),
        ],
        alignment: data.alignment ?? AlignmentType.CENTER,
    });
}


export enum Matpel {
    _1,
    _2,
    _3,
}

const matpel1Label = "MATPEL I - Advertencia";
const matpel2Label = "MATPEL II - Operaciones";
const matpel3Label = "MATPEL III - Técnico";

export function getMatpelLabel(matpel: Matpel) {
    switch (matpel) {
        case Matpel._1: {
            return matpel1Label;
        }
        case Matpel._2: {
            return matpel2Label;
        }
        case Matpel._3: {
            return matpel3Label;
        }
    }
}

export function getMatpelHours(matpel: Matpel): number {
    switch (matpel) {
        case Matpel._1: {
            return 12;
        }
        case Matpel._2: {
            return 12;
        }
        case Matpel._3: {
            return 40;
        }
    }
}

export function getMatpelFileName(matpel: Matpel) {
    switch (matpel) {
        case Matpel._1: {
            return "MATPEL 1";
        }
        case Matpel._2: {
            return "MATPEL 2";
        }
        case Matpel._3: {
            return "MATPEL 3";
        }
    }
}
