var DownloadManager = function () {};

/*
 * Chargement de l'application
 */
DownloadManager.Load = function (parameter) {
    this.LoadEvent();
};

/*
 * Chargement des événements (panneau admin)
 */
DownloadManager.LoadEvent = function () {
    Dashboard.AddEventAppMenu(DownloadManager.Execute, "", "DownloadManager");
    Dashboard.AddEventWindowsTool("DownloadManager");

    DownloadManager.InitTabDocument();
    DownloadManager.InitTabCategory();
};

// ─── Tab Documents ────────────────────────────────────────────────────────────

DownloadManager.InitTabDocument = function () {
    Event.AddById("btnAddDocument", "click", () => {
        DownloadManager.ShowAddDocument("");
    });
    EntityGrid.Initialise("gdDocument");
};

DownloadManager.ShowAddDocument = function (documentId) {
    Dialog.open("", {
        title:  Dashboard.GetCode("DownloadManager.AddDocument"),
        app:    "DownloadManager",
        class:  "DialogAdminDownloadManager",
        method: "AddDocument",
        params: documentId,
    });
};

DownloadManager.EditDocument = function (documentId) {
    DownloadManager.ShowAddDocument(documentId);
};

DownloadManager.DeleteDocument = function (documentId) {
    Animation.Confirm(Language.GetCode("DownloadManager.ConfirmDeleteDocument"), () => {

        var data = "Class=DownloadManager&Methode=DeleteDocument&App=DownloadManager";
        data += "&documentId=" + documentId;

        Request.Post("Ajax.php", data).then(data => {
            data = JSON.parse(data);
            DownloadManager.ReloadDocument(data);
        });
    });
};

DownloadManager.ReloadDocument = function (data) {
    let gdDocument = Dom.GetById("gdDocument");
    gdDocument.parentNode.innerHTML = data.data;
    DownloadManager.InitTabDocument();
};

// ─── Tab Catégories ───────────────────────────────────────────────────────────

DownloadManager.InitTabCategory = function () {
    Event.AddById("btnAddCategory", "click", () => {
        DownloadManager.ShowAddCategory("");
    });
    EntityGrid.Initialise("gdCategory");
};

DownloadManager.ShowAddCategory = function (categoryId) {
    Dialog.open("", {
        title:  Dashboard.GetCode("DownloadManager.AddCategory"),
        app:    "DownloadManager",
        class:  "DialogAdminDownloadManager",
        method: "AddCategory",
        params: categoryId,
    });
};

DownloadManager.EditCategory = function (categoryId) {
    DownloadManager.ShowAddCategory(categoryId);
};

DownloadManager.DeleteCategory = function (categoryId) {
    Animation.Confirm(Language.GetCode("DownloadManager.ConfirmDeleteDocument"), () => {

        var data = "Class=DownloadManager&Methode=DeleteCategory&App=DownloadManager";
        data += "&categoryId=" + categoryId;

        Request.Post("Ajax.php", data).then(data => {
            data = JSON.parse(data);
            DownloadManager.ReloadCategory(data);
        });
    });
};

DownloadManager.ReloadCategory = function (data) {
    let gdCategory = Dom.GetById("gdCategory");
    gdCategory.parentNode.innerHTML = data.data;
    DownloadManager.InitTabCategory();
};

// ─── Init page Front ─────────────────────────────────────────────────────────

DownloadManager.Init = function (app, method) {
    if (app == "DownloadManager") {
        switch (method) {
            case "Detail":
                DownloadManager.Detail();
                break;
            default:
                break;
        }
    }
    
    if(app == "downloads" && method != ""){
                DownloadManager.Detail();
    }
};

DownloadManager.Detail = function () {
   DownloadManagerEmailDownloadForm.Init();
};

// ─── Fonctions standard PuzzleApp ────────────────────────────────────────────

DownloadManager.Execute = function (e) {
    Dashboard.Execute(this, e, "DownloadManager");
    return false;
};

DownloadManager.RefreshPlugin = function () {
    Dashboard.StartApp("", "DownloadManager", "");
};

DownloadManager.About = function () {
    Dashboard.About("DownloadManager");
};

DownloadManager.Help = function () {
    Dashboard.OpenBrowser("DownloadManager", "{$BaseUrl}/Help-App-DownloadManager.html");
};

DownloadManager.ReportBug = function () {
    Dashboard.ReportBug("DownloadManager");
};

DownloadManager.Quit = function () {
    Dashboard.CloseApp("", "DownloadManager");
};
