var cycle
var initizialized
var instancePlaylistPrio

/**
 * Éxécute le callback si type est différent de browser
 *
 * @param {function} method
 */
function executeIfNotBrowser(method) {
    if (type !== "browser") {
        method()
    }
}

$(function () {
    var Log

    try {
        initizialized = new Initizialized()
    } catch (e) {
        console.log("ERREUR ATTRAPEE : " + e)

        if (type === "webos") {
            manageErrorInInitizialized(e)
        }
    }

    //debug background widget météo
    setInterval(function () {
        const iframe = document.querySelectorAll("iframe")[1]
        let weatherImg
        if (iframe) {
            try {
                weatherImg = iframe.contentWindow.document.getElementsByClassName("weather-background")[0]
            } catch (e) {
                console.log("ERREUR ATTRAPEE : " + e)
            }
        }
        if (iframe && iframe.style.backgroundImage && weatherImg) {
            weatherImg.style.display = "none"
        }
    }, 2000)
    //fin debug background widget météo

    function manageErrorInInitizialized(errorMessage) {
        const pathFolder = "file://internal/initializedError"
        removeFolderWebos(pathFolder)
        createFolderWebos(pathFolder)
        writeErrorFile(pathFolder + "/error.txt", errorMessage.toString())
    }

    function createFolderWebos(path) {
        const options = {}
        options.path = path

        const successCb = function (cbObject) {
                console.log("DOSSIER ERREUR IN INITIALIZED")
            },
            failureCb = function (cbObject) {
                const errorCode = cbObject.errorCode,
                    errorText = cbObject.errorText

                console.log(" Error Code [" + errorCode + "]: " + errorText)
            },
            storage = new Storage()
        storage.mkdir(successCb, failureCb, options)
    }

    function removeFolderWebos(path) {
        const successCb = function () {
                console.log("REMOVE DOSSIER ERREUR IN INITIALIZED")
            },
            failureCb = function (cbObject) {
                const errorCode = cbObject.errorCode,
                    errorText = cbObject.errorText
                console.log(" Error Code [" + errorCode + "]: " + errorText)
            },
            options = {
                file: path,
                recursive: true
            },
            storage = new Storage()
        storage.removeFile(successCb, failureCb, options)
    }

    function writeErrorFile(path, errorMessage) {
        const successCb = function (cbObject) {
                console.log("Successfully writen " + cbObject.written + " bytes")
            },
            failureCb = function (cbObject) {
                const errorCode = cbObject.errorCode,
                    errorText = cbObject.errorText
                console.log(" Error Code [" + errorCode + "]: " + errorText)
            },
            options = {
                data: errorMessage,
                path: path,
                position: 0,
                mode: "append",
                offset: 0,
                length: errorMessage.length,
                encoding: "utf8"
            },
            storage = new Storage()
        storage.writeFile(successCb, failureCb, options)
    }
})
