<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="https://yandex.st/jquery/2.0.3/jquery.min.js" type="text/javascript"></script>
    <script type='text/javascript' src="{% static 'js/xmlhttp.js' %}"></script>
    <script type='text/javascript' src="{% static 'js/cgi_switcher.js' %}"></script>
</head>
<script type="text/javascript">

    let nsv_ctype = '{{ ctype }}';
    let nsv_service = '{{ service }}';
    let nsv_serv_list = [];
    let nsv_services_tries = 0;

    function nsv_services_retry(err) {
        console.log(err);
        if (++nsv_services_tries < 4) {
            setTimeout(nsv_data_load, 3000);
        } else {
            console.log('failed to load services: ' + err);
        }
    }

    function nsv_data_load() {
        let xmlhttp = getXmlHttp();
        let url = '{% url "main" %}?format=json';
        xmlhttp.open('GET', url);
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState === 4) {
                if (xmlhttp.status === 200) {
                    try {
                        let res = JSON.parse(xmlhttp.responseText);
                        if (res['success']) {
                            nsv_serv_list = res['result'];
                            nsv_generate_services();
                        } else {
                            nsv_services_retry(xmlhttp.responseText);
                        }
                    } catch(err) { nsv_services_retry(err); }
                } else {
                    nsv_services_retry('not 200 ' + xmlhttp.responseText);
                }
            }
        };
        xmlhttp.send();
    }

    function nsv_markup_ctypes(s_ctypes) {
        let ct_main_el = document.getElementById('ctype_selector');
        let color = ct_main_el ? ct_main_el.style.color : 'black';
        for (let i=0; i<s_ctypes.length; ++i) {
            let el = document.getElementById('nav-ctype-' + s_ctypes[i]);
            if (!el) { continue; }
            el.style.fontWeight = 'bold';
            el.style.color = color;
            el.style.marginLeft = '5px';
        }
    }

    function nsv_generate_services() {
        let sr_select_el = document.getElementById('nav-services-list');
        if (nsv_serv_list.hasOwnProperty('metaservices')) {
            try {
                let is_metaservice = (nsv_serv_list['metaservices'].indexOf(nsv_service) !== -1);
                let evt = new CustomEvent('ismetaservice', {detail: {'is_meta': is_metaservice}});
                window.dispatchEvent(evt);
            } catch(err) {
                alert(err);
            }
        }
        if (nsv_serv_list.hasOwnProperty(nsv_ctype)) {
            sr_select_el.innerHTML = '';

            let prdm = ['deploy_manager', 'indexerproxy', 'searchproxy'];
            for (let i=0; i<prdm.length; ++i) {
                let serv = prdm[i];
                if (serv === 'deploy_manager' &&
                    !(nsv_ctype === 'stable' || nsv_ctype === 'prestable')) {
                    continue;
                }
                let srv_el = document.createElement('div');
                srv_el.className = 'nav-service-div';
                let link_el = document.createElement('a');
                link_el.innerHTML = serv;
                link_el.className = 'nav-service-link';
                link_el.href = window.location.pathname + set_cgi_param('service', serv);
                srv_el.appendChild(link_el);
                sr_select_el.appendChild(srv_el);
            }
            let sep = document.createElement('div');
            sep.className = 'nav-service-div';
            sep.innerHTML = '<br>';
            sr_select_el.appendChild(sep);

            nsv_serv_list[nsv_ctype].sort();
            for (let i=0; i<nsv_serv_list[nsv_ctype].length; ++i) {
                let service = nsv_serv_list[nsv_ctype][i];
                let srv_el = document.createElement('div');
                srv_el.className = 'nav-service-div';
                let link_el = document.createElement('a');
                link_el.innerHTML = service;
                link_el.className = 'nav-service-link';
                link_el.href = window.location.pathname + set_cgi_param('service', service);
                srv_el.appendChild(link_el);
                sr_select_el.appendChild(srv_el);
            }
        } else {
            console.log(nsv_serv_list);
        }
        let s_ctypes = [];
        for (let ct in nsv_serv_list) {
            if (!nsv_serv_list.hasOwnProperty(ct)) { continue; }
            try {
                if(nsv_serv_list[ct].indexOf(nsv_service) !== -1) {
                    s_ctypes.push(ct);
                }
            } catch (err) { console.log(err); }
        }
        nsv_markup_ctypes(s_ctypes);
    }

    $(document).ready(function() {
        let sr_select_el = document.getElementById('nav-services-list');
        if (!!sr_select_el) {
            nsv_data_load();
        } else {
            console.log('no service selector');
        }
    });

</script>
<body>

</body>
</html>
