import {ButtonType} from "./ButtonType";

export class Modal {
    get title(): HTMLHeadingElement {
        return this._title;
    }

    private static currentModalId: string | undefined = "";

    private _title: HTMLHeadingElement;
    private _id: string = "";
    private _loader: boolean = false;
    private _buttons: { type: string, content: string, onclick: Function }[] = [];
    private _leftCentered: boolean = false;
    private _elements: HTMLElement[] = [];

    set leftCentered(value: boolean) {
        this._leftCentered = value;
    }

    private static modalInClose = false;

    constructor(id, title: string) {
        this._id = id;

        this._title = document.createElement("h1") as HTMLHeadingElement;
        this._title.innerText = title;
        this._elements.push(this._title);
    }

    public static isModalOpen(): boolean {
        return !(!this.currentModalId);
    }


    /**
     * Adds an html element to the modal
     * @param element
     */
    public addContent(element: HTMLElement) {
        this._elements.push(element);
    }


    /**
     * Adds a spacer line to the modal
     */
    public addBR() {
        this.addContent(document.createElement("br"));
    }


    set id(value: string) {
        this._id = value;
    }

    set loader(value: boolean) {
        this._loader = value;
    }

    public addButton(type: ButtonType, content: string, onclick: Function = () => {
    }): HTMLButtonElement {
        let btn = document.createElement("button");
        btn.classList.add("btn", "btn-" + type);
        btn.onclick = () => onclick(btn);
        btn.innerText = content;
        btn.value = content;

        this._elements.push(btn);
        return btn;
    }


    public open(): Promise<void> {
        return new Promise(async (resolve) => {
            /* if (this._leftCentered) {
                 this._content = "<div style='text-align: left; padding-left: 2%;'>" + this._content;
             }*/ //todo

            if (this._loader) {
                let div = document.createElement("div");
                div.innerHTML = "<br><div class=\"lds-ellipsis\">\n" +
                    "  <div></div><div></div><div></div><div></div>\n" +
                    "</div>";
                this._elements.push(div);
            }

            /*if (this._leftCentered) {
                this._content += "</div>";
            }*/

            let elements = this._elements;
            let id = this._id;

            function tryOpen() {
                if (Modal.modalInClose) {
                    setTimeout(tryOpen, 50);
                    return;
                }
                resolve();
                Modal.open(elements, id);
            }

            tryOpen();
        });
    }

    public setTitle(title) {

    }

    /**
     * @param elements
     * @param id
     */
    private static open(elements: HTMLElement[], id?: string): Promise<void> {
        return new Promise((resolve, reject) => {
            const modal = document.getElementById("modal");
            const modalContent = document.getElementById("modalInnerContent");

            if (!modal || !modalContent) {
                reject();
                return;
            }

            modalContent.classList.add("modalBlendIn");
            modal.classList.add("modalBlendIn");

            setTimeout(() => {
                modalContent.classList.remove("modalBlendIn");
                modal.classList.remove("modalBlendIn");
                resolve();
            }, 800);

            modalContent.innerHTML = "";
            elements.forEach((val) => modalContent.append(val));
            //modalContent.innerHTML = `<h1 id="modalTitle">${title}</h1>${content}`;
            modal.style.display = "block";

            this.currentModalId = id ? id : "null";
        });

    }


    /**
     * Closes the modal
     */
    public close(): void {
        Modal.close(this._id);
    }


    /**
     * Closes the modal with id
     * @param id    The id of the modal
     */
    public static close(id?: string): void {
        if (id && this.currentModalId != id)
            return;

        Modal.modalInClose = true;

        const modal = document.getElementById("modal");
        const modalContent = document.getElementById("modal-content");
        const modalInnerContent = document.getElementById("modalInnerContent");

        if (!modal || !modalContent || !modalInnerContent)
            return;

        modalContent.classList.add("modalBlendOut");
        modal.classList.add("modalBlendOut");

        setTimeout(() => {
            modal.style.display = "none";
            modalInnerContent.innerHTML = "";
            modalContent.classList.remove("modalBlendOut");
            modal.classList.remove("modalBlendOut");
            this.modalInClose = false;
        }, 402);

        this.currentModalId = undefined;
    }
}