/*
 * Copyright 2020 Netsyms Technologies.
 * 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=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=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
        });
    }
});
