window.addEventListener('load', () => {

    function parseBoolean(value) {
        if(value === 'true') {
            return true;
        } else if (value === 'false') {
            return false;
        } else {
            return undefined;
        }
    }

    function generateQuery(query) {
        initialQuery = `https://holiday.bbr-dev.info/api/v1/holidays?country=${query['country']}`

        delete query['country'];

        for(const key in query) {
            initialQuery += `&${key}=${query[key]}`
        }
        document.querySelector('#query-link').href = initialQuery;
        document.querySelector('#result-content').innerHTML = "curl \"" + initialQuery + "\"";
    }

    let country = document.querySelector("#country");
    let contentType = document.querySelector("#content-type");
    let stateHoliday = document.querySelector("#state-holiday");
    let religiousHoliday = document.querySelector("#religious-holiday");

    // select which date/year/range is used
    let dateSelector = document.querySelector("#date-selector");

    let dYear = document.querySelector("#dsy-year");
    let dDate = document.querySelector("#dsd-date");

    let dStartRange = document.querySelector("#dsr-start-range");

    let dEndRange = document.querySelector("#dsr-end-range");

    let queryGenerator = document.querySelector("#query-generator");

    {
        const query = {};
        query['country'] = country.value;
        query['contentType'] = contentType.value;
        if(stateHoliday.value === 'true' || stateHoliday.value === 'false') {
            query['state_holiday'] = parseBoolean(stateHoliday.value);
        }
        if(religiousHoliday.value === 'true' || religiousHoliday.value === 'false') {
            query['religious_holiday'] = parseBoolean(religiousHoliday.value);
        }
        console.log(dateSelector.value,  dStartRange.value,  dEndRange.value);
        switch(dateSelector.value) {
            case 'year':
                query['year'] = dYear.value;
                break;
            case 'date':
                if(dDate.value) {
                    query['date'] = dDate.value;
                }
                break;
            case 'range':
                if(dStartRange.value) {
                    query['range_start'] = dStartRange.value;
                }
                if(dEndRange.value) {
                    query['range_end'] = dEndRange.value;
                }
            case 'all':
            default:
        }

        generateQuery(query);
    }

    queryGenerator.addEventListener('change', event => {
        const query = {};
        query['country'] = country.value;
        query['type'] = contentType.value;
        if(stateHoliday.value === 'true' || stateHoliday.value === 'false') {
            query['state_holiday'] = parseBoolean(stateHoliday.value);
        }
        if(religiousHoliday.value === 'true' || religiousHoliday.value === 'false') {
            query['religious_holiday'] = parseBoolean(religiousHoliday.value);
        }
        console.log(dateSelector.value,  dStartRange.value,  dEndRange.value);
        switch(dateSelector.value) {
            case 'year':
                query['year'] = dYear.value;
                break;
            case 'date':
                if(dDate.value) {
                    query['date'] = dDate.value;
                }
                break;
            case 'range':
                if(dStartRange.value) {
                    query['range_start'] = dStartRange.value;
                }
                if(dEndRange.value) {
                    query['range_end'] = dEndRange.value;
                }
            case 'all':
            default:
        }
        generateQuery(query);
    })
})
