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

var routes = [
    {
        path: '/home',
        name: 'home',
        async: function (routeTo, routeFrom, resolve, reject) {
            var total = packages.length;
            var undelivered = countRemainingPackages();
            var delivered = total - undelivered;
            var percent = (total > 0 ? (delivered / packages.length) : 1);
            resolve({
                templateUrl: './pages/home.html'
            }, {
                context: {
                    packageTotal: total,
                    packageLeft: undelivered,
                    packageDelivered: delivered,
                    packagePercent: percent
                }
            });
        }
    },
    {
        path: '/manage',
        url: './pages/manage.html',
        name: 'manage'
    },
    {
        path: '/list',
        templateUrl: './pages/list.html',
        name: 'list',
        on: {
            pageAfterIn: function () {
                loadPackageList();

                var searchbar = app.searchbar.create({
                    el: '.package-list-searchbar',
                    searchContainer: '#addresslist',
                    searchIn: '.item-title',
                    backdrop: false,
                    on: {
                        search(sb, query, previousQuery) {
                            console.log(query, previousQuery);
                        }
                    }
                });
            }
        },
        keepAlive: true
    },
    {
        path: '/map',
        url: './pages/map.html',
        name: 'map',
        keepAlive: true,
        on: {
            pageAfterIn: function () {
                if (map == null) {
                    createMap();
                }
            }
        }
    },
        {
        path: '/credits',
        url: './pages/credits.html',
        name: 'credits'
    },
    {
        path: '/settings',
        name: 'settings',
        async: function (routeTo, routeFrom, resolve, reject) {
            resolve({
                templateUrl: './pages/settings.html'
            }, {
                context: {
                    settings: [
                        {
                            setting: "alertsound",
                            title: "Alert sound",
                            text: "Select which sound to play when a package is nearby.",
                            onclick: "pickAlertSound()"
                        },
                        {
                            setting: "alertvolume",
                            title: "Alert volume",
                            min: 0,
                            max: 100,
                            step: 1,
                            value: localStorage.getItem("alertvolume"),
                            slider: true
                        },
                        {
                            setting: "alertinterval",
                            title: "Alert interval (seconds)",
                            min: 15,
                            max: 120,
                            step: 15,
                            value: localStorage.getItem("alertinterval") == null ? 30 : localStorage.getItem("alertinterval"),
                            slider: true
                        },
                        {
                            setting: "wakelock",
                            title: "Keep screen on",
                            text: "Improves GPS accuracy and alert sound reliability, but uses more battery.",
                            toggle: true,
                            checked: localStorage.getItem("wakelock") == "true",
                            onclick: ""
                        },
                        {
                            setting: "darktheme",
                            title: "Use dark theme",
                            text: "Saves power on phones with OLED screens.",
                            toggle: true,
                            checked: localStorage.getItem("darktheme") == "true",
                            onclick: ""
                        },
                        {
                            setting: "units",
                            title: "Measurement units",
                            text: "Use feet and miles for showing distance",
                            toggle: true,
                            checked: localStorage.getItem("units") == "imperial",
                            onclick: ""
                        },
                        {
                            setting: "mapsource",
                            title: "Map style",
                            text: "Choose which map style to use.",
                            onclick: "pickMapSource()"
                        },
                        {
                            setting: "versions",
                            title: "PackageHelper app v1.0.0",
                            text: "Copyright &copy; 2019 Netsyms Technologies.  Licensed under the Mozilla Public License 2.0.",
                            onclick: ""
                        },
                        {
                            setting: "opensource",
                            title: "Credits and open source info",
                            text: "",
                            onclick: "router.navigate('/credits')"
                        },
                        {
                            setting: "privacy",
                            title: "Privacy policy and legal",
                            text: "",
                            onclick: "openBrowser('https://netsyms.com/legal?pk_campaign=PackageHelpterApp')"
                        }]
                }
            });
        }
    },
];