/*
 * 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 cachedurls = [
    'package.json',
    'settings.js',
    'index.html',
    '.',
    'routes.js',
    'cache.js',

    'assets/audio/error.mp3',
    'assets/audio/ok.mp3',
    'assets/audio/scan.mp3',

    'assets/images/icons/logo.svg',
    'assets/images/barcode-dashed.svg',
    'assets/images/boxes.png',
    'assets/images/box-open-dashed.svg',
    'assets/images/box.png',
    'assets/images/check.png',
    'assets/images/envelope.png',
    'assets/images/envelopes.png',
    'assets/images/express.png',
    'assets/images/history-dashed.svg',
    'assets/images/largeboxes.png',
    'assets/images/largebox.png',
    'assets/images/multiple-items.png',
    'assets/images/note-dashed.svg',
    'assets/images/signature.png',
    'assets/images/signatures.png',
    'assets/images/smallpackage.png',
    'assets/images/smallpackages.png',
    'assets/images/bgs/add.svg',
    'assets/images/bgs/list.svg',
    'assets/images/bgs/map.svg',
    'assets/images/bgs/notes.svg',
    'assets/images/bgs/settings.svg',
    'assets/images/bgs/toolbox.svg',
    'assets/images/bgs/toolbox_addrlookup.svg',
    'assets/images/bgs/toolbox_log.svg',
    'assets/images/bgs/toolbox_scanner.svg',
    'assets/images/bgs/toolbox_sharelist.svg',
    'assets/images/bgs/toolbox_track.svg',
    'assets/images/bgs/toolbox_weather.svg',

    'assets/css/app.css',
    'assets/css/backdrop.css',
    'assets/css/oled.css',
    'assets/css/web-barcode.css',

    'assets/js/audio.js',
    'assets/js/autofill.js',
    'assets/js/list.js',
    'assets/js/location.js',
    'assets/js/login.js',
    'assets/js/main.js',
    'assets/js/manage.js',
    'assets/js/map.js',
    'assets/js/map_leaflet.js',
    'assets/js/map_mapbox.js',
    'assets/js/notes.js',
    'assets/js/packages.js',
    'assets/js/platform.js',
    'assets/js/settings.js',
    'assets/js/storage.js',
    'assets/js/sync.js',
    'assets/js/toolbox_scannerentries.js',
    'assets/js/toolbox_scanner.js',
    'assets/js/toolbox_sharelist.js',
    'assets/js/toolbox_track.js',
    'assets/js/toolbox_weather.js',
    'assets/js/util.js',

    'pages/credits.html',
    'pages/home.html',
    'pages/list.html',
    'pages/login.html',
    'pages/manage.html',
    'pages/map.html',
    'pages/myroute.html',
    'pages/settings.html',
    'pages/toolbox.html',
    'pages/help/list.html',
    'pages/help/map.html',
    'pages/myroute/editnote.html',
    'pages/toolbox/scanner.html',
    'pages/toolbox/sharelist.html',
    'pages/toolbox/track.html',
    'pages/toolbox/trackinginfo.html',
    'pages/toolbox/weather.html',
    'pages/toolbox/scanner/entries.html',
    'pages/toolbox/scanner/scanner.html',

    'node_modules/framework7/css/framework7.bundle.min.css',
    'node_modules/@fortawesome/fontawesome-free/css/all.min.css',
    'node_modules/material-design-icons/iconfont/material-icons.css',
    'node_modules/leaflet/dist/leaflet.css',
    'node_modules/leaflet.markercluster/dist/MarkerCluster.css',
    'node_modules/leaflet.markercluster/dist/MarkerCluster.Default.css',
    'node_modules/leaflet.locatecontrol/dist/L.Control.Locate.min.css',
    'node_modules/mapbox-gl/dist/mapbox-gl.css',
    'node_modules/framework7/js/framework7.bundle.min.js',
    'node_modules/jquery/dist/jquery.min.js',
    'node_modules/leaflet/dist/leaflet.js',
    'node_modules/leaflet.markercluster/dist/leaflet.markercluster.js',
    'node_modules/mapbox-gl/dist/mapbox-gl.js',
    'node_modules/jsbarcode/dist/JsBarcode.all.min.js',
    'node_modules/leaflet.locatecontrol/dist/L.Control.Locate.min.js',
    'node_modules/@zxing/library/umd/index.min.js',
    'node_modules/@fortawesome/fontawesome-free/webfonts/fa-brands-400.ttf',
    'node_modules/@fortawesome/fontawesome-free/webfonts/fa-brands-400.woff',
    'node_modules/@fortawesome/fontawesome-free/webfonts/fa-brands-400.woff2',
    'node_modules/@fortawesome/fontawesome-free/webfonts/fa-regular-400.ttf',
    'node_modules/@fortawesome/fontawesome-free/webfonts/fa-regular-400.woff',
    'node_modules/@fortawesome/fontawesome-free/webfonts/fa-regular-400.woff2',
    'node_modules/@fortawesome/fontawesome-free/webfonts/fa-solid-900.ttf',
    'node_modules/@fortawesome/fontawesome-free/webfonts/fa-solid-900.woff',
    'node_modules/@fortawesome/fontawesome-free/webfonts/fa-solid-900.woff2',
    'node_modules/material-design-icons/iconfont/MaterialIcons-Regular.ttf',
    'node_modules/material-design-icons/iconfont/MaterialIcons-Regular.woff',
    'node_modules/material-design-icons/iconfont/MaterialIcons-Regular.woff2'
];
for (i in SETTINGS.maptileurls) {
    cachedurls.push(SETTINGS.maptileurls[i].json);
}

for (i in SETTINGS.alertsounds) {
    cachedurls.push("assets/audio/" + SETTINGS.alertsounds[i].file);
}

for (i in SETTINGS.weathericons) {
    cachedurls.push("assets/images/weather-" + SETTINGS.weathericons[i] + ".svg");
}

var cacheFilled = false;

function fillCache(cachename) {
    if ('caches' in window) {
        // delete old caches
        caches.keys().then(function (cacheNames) {
            return Promise.all(cacheNames.map(function (oldCacheName) {
                if (oldCacheName != cachename) {
                    return caches.delete(oldCacheName);
                }
            }));
        });

        cacheFilled = true; // assume good
        caches.open(cachename).then(function (cache) {
            for (i in cachedurls) {
                try {
                    cache.add(cachedurls[i]);
                } catch (ex) {
                    cacheFilled = false;
                }
            }
        });

        // Do it again in a little while
        if (!cacheFilled) {
            setTimeout(function () {
                fillCache(cachename);
            }, 1000 * 30);
        }
    }
}

async function clearAllCaches() {
    const keys = await caches.keys();

    for (const key of keys) {
        caches.delete(key);
    }
}