var gAppData = {
    folders: [1], // List of folders to loop on, you can add some more...
    contacts: [false, false, false, false, false, false, false, false]
};

function onPageLoaded() {
    IDAL.getControlInterfaces()
        .then(onControlInterfaceAvailable);
}

function onControlInterfaceAvailable(iface) {
    console.log("Control interface available");

    // Disable default input contact management.
    // Input contacts will be managed by this app.
    // It is possible to do without it, but all input contact actions
    // defined by the engine will also be triggered.
    var config = {
        controller: "browser",
        lifetime: "url"
    };

    iface.contact.setInputConfiguration(config)
        .then(function(e) {
            console.log("Contact.setInputConfiguration(" + JSON.stringify(config) + ") => " + JSON.stringify(e));
        })
        .catch(function(e) {
            console.error("Error: Contact.setInputConfiguration(" + JSON.stringify(config) + ") => " + JSON.stringify(e));
        });

    var currentFolder = 0;


    // Get events to retrieve current directory
    iface.player.addListener(function(event) {
        if (event.detail.type === 'folder-enter') {
            currentFolder = event.detail.folder.number;
        }
    });

    iface.player.getFolderNumber().then(function(number) {
        currentFolder = number;
    });


    var fader = document.getElementById("fader");
    var animating = false;
    var chosenFolder = undefined;

    // Add listener for transition end events
    fader.addEventListener('transitionend', () => {
        animating = false;

        // Now that the screen is black, change the video.
        // Use stop() then play() to make safely remove the black layer without
        // displaying the old video...
        iface.player.stop()
            .then(function() {
                fader.classList.remove("fade-out");
                return iface.player.playFolder(chosenFolder);
            });
    });

    // Process input contact events
    window.addEventListener('idal-contact-event', function(e) {
        var detail = e.detail;

        // Already in transition, do nothing
        if (animating) {
            return;
        }

        chosenFolder = undefined;

        switch (detail.name) {
            case "combined":
                // Find first modified contact 
                for (var i = 0 ; (chosenFolder === undefined) && (i < detail.values.length) ; i++) {
                    var val = detail.values[i];
                    if ((val != gAppData.contacts[i]) && (val))  {
                        chosenFolder = gAppData.folders[i];
                    }
                }

                if ((chosenFolder !== undefined) && isWaitingFolder(currentFolder)) {
                    animating = true;
                    fader.classList.add("fade-out");
                }

                for (var i = 0 ; i < detail.values.length ; i++) {
                    gAppData.contacts[i] = detail.values[i];
                }

                break;

            case "standalone":
                break;
        }
    });
}

function isWaitingFolder(folder) {
    return folder === 900;
}

window.addEventListener("load", onPageLoaded);
