/*
 * 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/.
 */


function logout() {
    app.dialog.confirm(
            "Are you sure you want to log out?",
            "Log out?",
            function () {
                localStorage.removeItem('password');
                localStorage.removeItem('username');
                localStorage.removeItem('lastsync');
                restartApplication();
            }
    );
}

function resyncAndRestart() {
    app.toast.show({
        text: "Syncing settings and restarting...",
        position: "bottom",
        destroyOnClose: true,
        closeTimeout: 1000 * 10
    });
    syncNow(function () {
        restartApplication();
    });
}

function clearCaches() {
    app.toast.show({
        text: "Clearing caches. You may need to restart the app to see a difference.",
        position: "bottom",
        destroyOnClose: true,
        closeTimeout: 1000 * 10
    });
    setStorage("geocode_cache", "{}");
    if ('caches' in window) {
        clearAllCaches();
    }
}

$('.item-link[data-setting=apptheme] select').on("change", function () {
    setStorage("apptheme", $('.item-link[data-setting=apptheme] select').val());

    applyColorTheme();
});

$('.item-link[data-setting=liststyle] select').on("change", function () {
    setStorage("liststyle", $('.item-link[data-setting=liststyle] select').val());
});

$('.item-link[data-setting=animation] select').on("change", function () {
    setStorage("animation", $('.item-link[data-setting=animation] select').val());

    if (getStorage("animation") != "auto") {
        setAnimations();
    }
    if (getStorage("animation") == "auto") {
        toggleAnimations(auto_disable_animations == false);
    }
});

$('.item-content[data-setting=showhelp] .toggle input').on("change", function () {
    var checked = $(this).prop('checked');
    setStorage("show_help", checked);
});

$('.item-content[data-setting=oldhomeui] .toggle input').on("change", function () {
    var checked = $(this).prop('checked');
    setStorage("oldhomeui", checked);
});

$('.item-link[data-setting=units] select').on("change", function () {
    setStorage("units", $('.item-link[data-setting=units] select').val());
});

$('.item-link[data-setting=trackzoom] select').on("change", function () {
    setStorage("trackzoom", $('.item-link[data-setting=trackzoom] select').val());
});

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

    if (platform_type == "cordova") {
        loadSettings();
    } 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=runinbackground] .toggle input').on("change", function () {
    var checked = $(this).prop('checked');
    setStorage("runinbackground", checked);

    if (platform_type == "cordova") {
        loadSettings();
    } 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=hardwarescanner] .toggle input').on("change", function () {
    var checked = $(this).prop('checked');
    setStorage("hardwarescanner", checked);
    setupHardwareScanner();
});

$('.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();
    setStorage("alertvolume", val);
    setVolume("alert", val);
    playSound("alert");
});

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

$('.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();
    setStorage("alertinterval", val);
});

$('.item-link[data-setting=mapsource] select').on("change", function () {
    setStorage("mapsource", $('.item-link[data-setting=mapsource] select').val());

    reloadMap();
});

$('.item-content[data-setting=mapscale] .toggle input').on("change", function () {
    var checked = $(this).prop('checked');
    setStorage("mapscale", checked ? "true" : "false");

    reloadMap();
});

$('.item-content[data-setting=maptype] .toggle input').on("change", function () {
    var checked = $(this).prop('checked');
    setStorage("maptype", checked ? "leaflet" : "mapbox");

    maptype = checked ? "leaflet" : "mapbox";

    reloadMap();
});

$('.item-link[data-setting=alertsound] select').on("change", function () {
    setStorage("alertsound", $('.item-link[data-setting=alertsound] select').val());
    // Reload sound effect stuff to apply new sound
    initSFX();
    // Play the selected sound
    playSound("alert");
});