﻿/**
 * Fenêtre d'alerte (ex ACDAlert)
 * */
Components.AlertWindow = {
    timer: null,
    callback: () => Components.AlertWindow.widgets.window.close(),

    /*
     * Initialisation (bindings)
     */
    init() {
        // Déclaration de nos bindings sur les boutons
        $("#component-alert-button-ok").on("click.component", (e) => {
            if (!!this.callback && typeof this.callback == "function")
                this.callback(e);

            this.widgets.window.close();
        });

        $(window).on("resize", () => {
            if (!!Components.AlertWindow.widgets.window.selector().is(":visible")) {
                if (!!Components.AlertWindow.timer) clearTimeout(Components.AlertWindow.timer);
                Components.AlertWindow.timer = setTimeout(() => { Components.AlertWindow.widgets.window.get().center() }, 50);
            }
        });
    },
    /**
     * Contient les widgets de la fenêtre avec leurs selecteurs 
     */
    widgets: {
        window: {
            selector() { return $("#component-alert-window"); },
            get() { return this.selector().data("kendoWindow"); },
            open() { this.get().center().open(); },
            close() { this.get().close(); },
            set: {
                title(value) { $("#component-alert-window_wnd_title").html(value); },
                content(value) { $("#component-alert-content-right").html(value); },
                width(value) { this.get().setOptions({ width: value }); }
            }
        },
        btnOk: {
            selector() { return $("#component-alert-button-ok"); },
            set(value) { this.selector().html(value); }
        },
        icon: {
            selector() { return $("#component-alert-content-left"); },
            set(value) {
                let type = Components.icons.get(value);
                if (!type) { type = Components.icons.get("info"); }

                this.selector().html(type);
            }
        }
    },
    /**
     * Point d'entrée du composant
     * @param {string} message Contenu de la fenêtre
     * @param {function} callbackAlert 
     * @param {function} callbackCancel
     * @param {object} parameters
     */
    show(message, callback, parameters = null) {
        if (!message) {
            const error = "Erreur AlertWindow : Components.AlertWindow.show(<message>, <type d'alerte>, <callback>, <objet parametres>);";
            console.error(error);
            return;
        }

        // On met à jour la fenêtre
        this.set.call(Components.AlertWindow.widgets, parameters);

        // On set nos callbacks
        if (!!callback && typeof callback == "function")
            this.callback = callback;

        // On set le content
        this.widgets.window.set.content(message);

        // On ouvre
        this.widgets.window.open();
    },
    /**
     * Permet de mettre à jour la window avant ouverture (ou après) avec les paramètres passés dans le .show()
     * @param {any} parameters
     */
    set(parameters) {
        // Si on a dans les paramètres une value pour btnAlert
        this.btnOk.set.call(this.btnOk, parameters?.btnAlert ?? "OK");

        // On set l'icone
        this.icon.set.call(this.icon, parameters?.type ?? "info");

        // Si on a dans les paramètres une value pour le titre
        this.window.set.title.call(this.window, parameters?.title ?? "Informations");

        // Si on a dans les paramètres une value pour la taille
        this.window.set.width.call(this.window, parameters?.width ?? "auto");
    },
    /**
     * Permet d'activer le callback dans le cas ou l'utilisateur ferme la fenêtre par la croix
     * ou en appuyant sur la croix
     * @param {any} e evenements
     */
    onClose(e) {
        e.preventDefault();

        if (e.userTriggered) {
            Components.ConfirmWindow.callbacks.canceled(e);
        }
    }
}

// On init la window (principalement pour les bindings)
$(document).ready(() => Components.AlertWindow.init());