/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */


$('.item-content[data-setting=darktheme] .toggle input').on("change", function () {
    var checked = $(this).prop('checked');
    console.log(checked);
    localStorage.setItem("darktheme", checked);

    if (localStorage.getItem("darktheme") == "true") {
        $("#app").addClass("theme-dark");
    } else {
        $("#app").removeClass("theme-dark");
    }
});

$('.item-content[data-setting=units] .toggle input').on("change", function () {
    var checked = $(this).prop('checked');
    console.log(checked);
    localStorage.setItem("units", (checked ? "imperial" : "metric"));
});

$('.item-content[data-setting=wakelock] .toggle input').on("change", function () {
    var checked = $(this).prop('checked');
    console.log(checked);
    localStorage.setItem("wakelock", checked);

    if (platform_type == "cordova") {
        if (localStorage.getItem("wakelock") == "true") {
            window.powerManagement.acquire(function () {
                console.log('Wakelock acquired');
            }, function () {
                console.log('Failed to acquire wakelock');
            });
        } else {
            window.powerManagement.release(function () {
                console.log('Wakelock released');
            }, function () {
                console.log('Failed to release wakelock');
            });
        }
    } else {
        app.toast.show({
            text: "This setting won't do anything on your device.",
            position: "bottom",
            destroyOnClose: true,
            closeTimeout: 1000 * 10
        });
    }
});

$('.item-content[data-setting=alertvolume] .range-slider').on('range:changed', function (e, range) {
    var val = app.range.get(".item-content[data-setting=alertvolume] .range-slider").getValue();
    localStorage.setItem("alertvolume", val);
    setVolume("alert", val);
    playSound("alert");
});

$('.item-content[data-setting=alertinterval] .range-slider').on('range:changed', function (e, range) {
    var val = app.range.get(".item-content[data-setting=alertinterval] .range-slider").getValue();
    localStorage.setItem("alertinterval", val);
});

function pickAlertSound() {
    var currentalertsound = localStorage.getItem("alertsound");
    app.dialog.create({
        title: 'Alert Sound',
        buttons: [
            {
                text: 'Sonar' + (currentalertsound == "sonar" ? " (current)" : ""),
            },
            {
                text: 'Robot' + (currentalertsound == "robot" ? " (current)" : ""),
            },
            {
                text: 'Coin' + (currentalertsound == "coin" ? " (current)" : ""),
            },
            {
                text: 'Jump' + (currentalertsound == "jump" ? " (current)" : ""),
            },
            {
                text: 'Cancel',
                color: 'red'
            }
        ],
        verticalButtons: true,
        onClick: function (dialog, index) {
            var alertsound = "sonar";
            switch (index) {
                case 0:
                    alertsound = "sonar";
                    break;
                case 1:
                    alertsound = "robot";
                    break;
                case 2:
                    alertsound = "coin";
                    break;
                case 3:
                    alertsound = "jump";
                    break;
                default:
                    return;
            }
            localStorage.setItem("alertsound", alertsound);
            // Reload sound effect stuff to apply new sound
            initSFX();
            // Play the selected sound
            playSound("alert");
        }
    }).open();
}

function pickMapSource() {
    var currentmapsource = localStorage.getItem("mapsource");
    app.dialog.create({
        title: 'Map',
        buttons: [
            {
                text: 'Helena Offline' + (currentmapsource == "offline" ? " (current)" : ""),
            },
            {
                text: 'Liberty' + (currentmapsource == "liberty" ? " (current)" : ""),
            },
            {
                text: 'Terrain' + (currentmapsource == "terrain" ? " (current)" : ""),
            },
            {
                text: 'Dark Fiord' + (currentmapsource == "fiord" ? " (current)" : ""),
            },
            {
                text: 'Cancel',
                color: 'red'
            }
        ],
        verticalButtons: true,
        onClick: function (dialog, index) {
            var mapsource = "offline";
            switch (index) {
                case 0:
                    mapsource = "offline";
                    break;
                case 1:
                    mapsource = "liberty";
                    break;
                case 2:
                    mapsource = "terrain";
                    break;
                case 3:
                    mapsource = "fiord";
                    break;
                default:
                    return;
            }
            localStorage.setItem("mapsource", mapsource);
            // Re-init map to load new style
            reloadMap();
        }
    }).open();
}