const debug              = false;
const CACHE_NAME         = 'csvxml-cache-v1';
const urlsToCache = [

    '/index.htm',
    '/assets/js/jszip/dist/jszip.min.js',
    '/assets/js/csvxmlV2.min.js?v000003',
    '/assets/css/csvxml.min.css?v000003',

    '/assets/fonts/SourceSansPro-Regular.woff2',

    '/json/fields.de.json',
    '/json/fields.en.json',
    '/json/fields.hu.json',

    '/json/tls.de.json',
    '/json/tls.en.json',
    '/json/tls.hu.json',

];

self.addEventListener('install', function(event) {
    // Perform install steps
    event.waitUntil(
        Promise.all([
        caches.open(CACHE_NAME)
            .then(function(cache) {
                if (debug === true) console.log('Opened cache');
                return cache.addAll(urlsToCache.map(url => new Request(url, {credentials: 'same-origin'})));
            }),

        ])
    );
});

self.addEventListener('fetch', function(event) {

    if (event.request.url.startsWith(location.origin) === false) return;

    event.respondWith(
        caches.match(event.request)
            .then(function(response) {

                if (response) {
                    if (debug === true) console.log("Serving " + event.request.url + " from cache.");
                    return response;
                }

                // IMPORTANT: Clone the request. A request is a stream and
                // can only be consumed once. Since we are consuming this
                // once by cache and once by the browser for fetch, we need
                // to clone the response.
                var fetchRequest = event.request.clone();

                return fetch(fetchRequest).then(
                    function(response) {
                        // Check if we received a valid response
                        if(!response || response.status !== 200 || response.type !== 'basic') {
                            return response;
                        }

                        var responseToCache = response.clone();

                        caches.open(CACHE_NAME)
                            .then(function(cache) {
                                if (debug === true) console.log("Added " + event.request.url + "to cache");
                                cache.put(event.request, responseToCache);
                            });

                        return response;
                    }
                ).catch(
                    () => {
                        console.log("Problem");
                        if (event.request.url.indexOf("index.php?") == -1) return;
                        else {
                            console.log("Fixing");
                            return caches.open(CACHE_NAME).then(
                                function(cache) {
                                    // cache.keys().then(function(keys) { console.log(keys); })
                                    // return cache.match("index.php")
                                });
                        }
                    }
                    );
            })
        );
});