import { saveAs } from "file-saver";
import { _4X4Cert } from "./certs/4X4";
import { CertData } from "./certs/CertData";
import { matpelCert } from "./certs/MATPEL";
import { Matpel } from "./certs/utils";

import { manejoDefensivoCert } from "./certs/MANEJO_DEFENSIVO";
import { mecanicaBasicaCert } from "./certs/MECANICA_BASICA";
import { segOpMaqPesCert } from "./certs/SEG_OP_MAQ_PES";
import { supervisorEscolta } from "./certs/SUPERVISOR_ESCOLTA";
import { ipercCert } from "./certs/IPERC";
import { excavadoraHidraulica } from "./certs/EXCAVADORA_HIDRAULICA";
import { cargadorFrontal } from "./certs/CARGADOR_FRONTAL";
import { retroexcavadora } from "./certs/RETROEXCAVADORA";
import { volquete } from "./certs/VOLQUETE";
import { primerosAuxiliosCert } from "./certs/PRIMEROS_AUXILIOS";
import { luchaContraIncendiosCert } from "./certs/LUCHA_CONTRA_INCENDIOS";

declare global {
    interface Window {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        docx: any,
        certs: CertGenObj,
    }
}

async function download(document: Document, filename: string = "filename.docx") {
    const blob = await window.docx.Packer.toBlob(document);
    saveAs(blob, filename);
}

const certGeneratorGenerator = <T>(generatorFn: (options: CertData<T>) => Promise<Document>) => (filename: string, options: CertData<T>) => {
    generatorFn(options)
        .then((doc) => {
            download(doc, filename);
        });
};

type CertGenObj = {
    [key: string]: (filename: string, options: CertData<null>) => void
}

export const certGenerator: CertGenObj = Object.freeze({
    "Matpel 1": (filename: string, options: CertData<null>) => {
        const options_f: CertData<Matpel> = {
            ...options,
            matpel: Matpel._1,
        };
        matpelCert(options_f)
            .then((doc) => {
                download(doc, filename);
            });
    },
    "Matpel 2": (filename: string, options: CertData<null>) => {
        const options_f: CertData<Matpel> = {
            ...options,
            matpel: Matpel._2,
        };
        matpelCert(options_f)
            .then((doc) => {
                download(doc, filename);
            });
    },
    "Matpel 3": (filename: string, options: CertData<null>) => {
        const options_f: CertData<Matpel> = {
            ...options,
            matpel: Matpel._3,
        };
        matpelCert(options_f)
            .then((doc) => {
                download(doc, filename);
            });
    },
    "Manejo Defensivo": certGeneratorGenerator(manejoDefensivoCert),
    "Mecanica Basica": certGeneratorGenerator(mecanicaBasicaCert),
    "4x4": certGeneratorGenerator(_4X4Cert),
    "Seg. Op. Maquinaria Pesada": certGeneratorGenerator(segOpMaqPesCert),
    "Sup. Escolta": certGeneratorGenerator(supervisorEscolta),
    "IPERC": certGeneratorGenerator(ipercCert),
    "Excavadora": certGeneratorGenerator(excavadoraHidraulica),
    "Cargador Frontal": certGeneratorGenerator(cargadorFrontal),
    "Retroexcavadora": certGeneratorGenerator(retroexcavadora),
    "Volquete": certGeneratorGenerator(volquete),
    "Primeros Auxilios": certGeneratorGenerator(primerosAuxiliosCert),
    "Lucha contra Incendios": certGeneratorGenerator(luchaContraIncendiosCert),
});


// Add certificate generation to the window in dev mode,
// to make it easier to develop & test new certificates.
if (import.meta.env.DEV) {
    console.log("Dev time!!");
    window.certs = certGenerator;
}
