'use strict';


const configure = (configuration, service) => {

    const fetchIt = (storage) => {

        // Setup fetching in web workers
        if (window.Worker) {
            const workerBaseURL = window.infoBeamerConfig.get('workerBaseURL');
            const fetchWorkerCode = workerBaseURL + "/generic_fetch_worker.js";

            // Schedule fetch worker
            const scheduleWorker = new Worker(fetchWorkerCode);
            const scheduleType = 'Schedule';
            const scheduleURL = window.infoBeamerConfig.get('scheduleURL');
            const scheduleFetchInterval = window.infoBeamerConfig.get('scheduleFetchInterval');

            // Configure schedule worker
            scheduleWorker.postMessage({fetchType: scheduleType,
                                        fetchURL: scheduleURL,
                                        fetchInterval: scheduleFetchInterval
                                       });

            // Obtain schedule results and call back
            scheduleWorker.onmessage = function(e) {
                if (e.data.msgType === scheduleType) {
                    let jsonData = e.data.json;

                    console.log("Got it");
                    console.log(jsonData);
                    console.log("---");


                    let scheduleData = service.process_data(jsonData);
                    //let scheduleData = e.data.json;

                    console.log(scheduleData);

                    storage.scheduleData = scheduleData;
                    console.log("Got it");
                    console.log(storage);
                    console.log("---");
                    // update_screen();
                }
            };


            // Add more workers here
        } else {
            console.log("Your browser doesn't support web workers.");
        }

    };


    return fetchIt;
};


export {
    configure
};
