let s3_browser = {
    reloadList: 0,
    init: function (path) {
        let elem = document.getElementById('filemanager');
        let initpath = [
            [ Admin.prontus_id, Admin.prontus_id, { canmodify: false } ]
        ];

        if (path) {
            let parts = path.split("/");

            parts.forEach(function (item) {
                initpath.push([item, item]);
            });
        }

        let options = {
            initpath: initpath,
            onrefresh: function(folder, required) {
                let path = folder.GetPathIDs().join("/");
                let $this = this;
                let xhr = new this.PrepareXHR({
                    url: '/cgi-cpn/prontus_s3_list.cgi',
                    params: {
                        _prontus_id: Admin.prontus_id,
                        path: path,
                        reload: s3_browser.reloadList
                    },
                    onsuccess: function(e) {
                        var data = JSON.parse(e.target.response);
                        s3_browser.reloadList = 0;

                        if (data.status == 1) {
                            folder.SetEntries(data.data);
                        } else {
                            folder.SetEntries([]);
                            $this.SetNamedStatusBarText('folder', 'No se pudo cargar la carpeta seleccionada.');
                        }

                    },
                    onerror: function(e) {
                    }
                });

                xhr.Send();
            },
            onopenfile: function (_folder, entry) {
                // console.log("onopenfile", folder.GetPath(), entry);

                if (entry.ext == "pdf" || entry.ext == "jpg" || entry.ext == "jpeg" || entry.ext == "png" || entry.ext == "svg") {
                    $("#previewIframe").attr("src", entry.path);
                    $("#modalPreview").modal("show");
                } else if (entry.ext == "mp4") {
                    $("#modalPreviewVideo").modal("show");

                    $("#modalPreviewVideo").off("shown.bs.modal").on("shown.bs.modal", function (e) {
                        let html = $("#pplpayer").html();

                        $("#modalPreviewVideoContenido").html(html);

                        prontusPlayer5.install({
                            videoTagId: "video-pp",
                            video: {
                                autoplay: true,
                                muted: true,
                                src: entry.path,
                            }
                        });
                    });

                    $("#modalPreviewVideo").off("hide.bs.modal").on("hide.bs.modal", function (e) {
                        prontusPlayer5.destroy("video-pp");
                    });
                }
            },
            langmap: {
                "Download": "Descargar",
                "Delete": "Borrar",
                "Loading...": "Cargando...",
                "{0} items": "{0} elemento(s)",
                "{0} items selected {1}": "{0} elementos seleccionados",
                "1 item selected {0}": "1 elemento seleccionado {0}",
                "{0} items selected": "{0} elementos seleccionados",
                "1 item selected": "1 elemento seleccionado",
                "This folder is empty.": "La carpeta está vacía.",
                "Back (Alt + Left Arrow)": "Retroceder",
                "Back to \"{0}\" (Alt + Left Arrow)": 'Retroceder a "{0}"',
                "Forward (Alt + Right Arrow)": "Avanzar",
                "Forward to \"{0}\" (Alt + Right Arrow)": "Avanzar a \"{0}\"",
                "Up (Alt + Up Arrow)": "Arriba",
                "Up to \"{0}\" (Alt + Up Arrow)": "Ir hasta \"{0}\"",
                "1 item": "1 elemento",
                "Recent locations": "Ubicaciones recientes"
            }
        };

        let fe = new window.FileExplorer(elem, options);

        $("#feRefresh").off("click").on("click", function (e) {
            e.preventDefault();

            s3_browser.reloadList = 1;
            fe.RefreshFolders(true);
        });
    }
};
