// get the time slots json object
var timeSlots = JSON.parse('{"0": {"start": 8.55, "end": 20.0, "mode": "call"}, "1": {"start": 7.59, "end": 21.01, "mode": "call"}, "2": {"start": 7.59, "end": 21.01, "mode": "call"}, "3": {"start": 7.59, "end": 21.01, "mode": "call"}, "4": {"start": 7.59, "end": 21.01, "mode": "call"}, "5": {"start": 7.59, "end": 21.01, "mode": "call"}, "6": {"start": 8.55, "end": 20.0, "mode": "call"}}');

var dateObj = new Date();
var currentHour = dateObj.getHours() + (dateObj.getUTCMinutes() / 100);

var APP = 'app';
var CALL = 'call';

var modes = {};
modes[APP] = "call-closed";
modes[CALL] = "call-opened";

var mode = APP;

if (document.URL.indexOf('mode=' + APP) !== -1) {
    mode = APP;
} else if (document.URL.indexOf('mode=' + CALL) !== -1) {
    mode = CALL;
} else if (timeSlots !== null) {
    var modesKeys = Object.keys(modes);
    var timeSlot = timeSlots[dateObj.getUTCDay()];

    // get the current mode
    var timeSlotMode = timeSlot['mode'];
    var theOtherMode = modesKeys[(modesKeys.indexOf(timeSlot['mode']) + 1) % modesKeys.length];
    mode = currentHour >= timeSlot['start'] && currentHour <= timeSlot['end'] ? timeSlotMode : theOtherMode;
}

if (mode == APP) {
    body = document.getElementById('body')
    // remove the default call mode
    body.classList.remove(modes[CALL]);
    body.classList.add(modes[mode]);
}

// Set timeSlots on ppn-input-scheduler if element exists on the page
document.addEventListener("DOMContentLoaded", () => {
    const el_scheduler = document.querySelector('ppn-input-scheduler');
    if (!el_scheduler) {
        console.warn('ppn-input-scheduler not found on the page');
        return;
    }
    el_scheduler.setAttribute('timeslots', JSON.stringify(timeSlots));
})