<!DOCTYPE html>
<html>
    <head>
        <title>Dispatcher Configuration</title>
        <script src="jquery.min.js"></script>
        <style>
            input[type=text], input[type=number], input[type=password], select {
                width: 20rem;
                padding: 0.8rem 1rem;
                margin: 0.4rem 0;
                display: inline-block;
                border: 1px solid #ccc;
                border-radius: 4px;
                box-sizing: border-box;
            }
        </style>
    </head>
    <body>
        <h3>Dispatcher Configuration</h3>
        <hr>

        <fieldset>
            <legend>General</legend>
            <label>AMQP URL:</label>
            <input type="text" id="amqpURL"/>
        </fieldset>
        <fieldset>
            <legend>Connectors</legend>
            <fieldset>
                <legend>DAPNET Connector</legend>
                <label for="dapnet_enabled">Enabled:</label>
                <input type="checkbox" id="dapnet_enabled"/>
                </br>
                <label for="dapnet_endpoint">Endpoint:</label>
                <input type="text" id="dapnet_endpoint"/>
                </br>
                <label for="dapnet_duplex_timeout">Duplex Timeout(in seconds) : recommended 90s</label>
                <input type="number" min="10" max="1200" id="dapnet_duplex_timeout"/>
                </br>
                <label for="dapnet_username">Username:</label>
                <input type="text" id="dapnet_username"/>
                </br>
                <label for="dapnet_password">Password:</label>
                <input type="password" id="dapnet_password"/>
            </fieldset>
            <fieldset>
                <legend>LoRaWAN Connector</legend>
                <label for="lorawan_enabled">Enabled:</label>
                <input type="checkbox" id="lorawan_enabled"/>
                </br>
                <label for="lorawan_duplex_timeout">Duplex Timeout(in seconds) : recommended value equals LPWAN-FetchTime</label>
                <input type="number" min="10" max="9000" id="lorawan_duplex_timeout"/>
                </br>
                <label for="lorawan_mqttserver">MQTT Endpoint:</label>
                <input type="text" id="lorawan_mqttserver"/>
                <button onClick="setLoRaWANMQTT('mqtt://eu.thethings.network:1883')">Preset TTNv2</button>
                <button onClick="setLoRaWANMQTT('mqtt://eu1.cloud.thethings.network:1883')">Preset TTNv3</button>
                </br>
                <label for="lorawan_username">Username:</label>
                <input type="text" id="lorawan_username"/>
                </br>
                <label for="lorawan_password">Password:</label>
                <input type="password" id="lorawan_password"/>
            </fieldset>
            <fieldset>
                <legend>POCSAG Connector</legend>
                <label for="pocsag_enabled">Enabled:</label>
                <input type="checkbox" id="pocsag_enabled"/>
                </br>
                <label for="pocsag_duplex_timeout">Duplex Timeout(in seconds): [recommended 90s]</label>
                <input type="number" min="10" max="1200" id="pocsag_duplex_timeout"/>
            </fieldset>
            <fieldset>
                <legend>eCityruf Connector</legend>
                <label for="ecityruf_enabled">Enabled:</label>
                <input type="checkbox" id="ecityruf_enabled"/>
                </br>
                <label for="ecityruf_duplex_timeout">Duplex Timeout(in seconds): [recommended 180s]</label>
                <input type="number" min="10" max="1200" id="ecityruf_duplex_timeout"/>
            </fieldset>
            <fieldset>
                <legend>eMessage Puppeteering Connector</legend>
                <label for="emp_enabled">Enabled:</label>
                <input type="checkbox" id="emp_enabled"/>
                </br>
                <label for="emp_duplex_timeout">Duplex Timeout(in seconds): [recommended 180s]</label>
                <input type="number" min="10" max="1200" id="emp_duplex_timeout"/>
            </fieldset>

            emPuppettering
            <fieldset>
                <legend>eMessage Connector</legend>
                <label for="emessage_enabled">Enabled:</label>
                <input type="checkbox" id="emessage_enabled"/>
                </br>
                <label for="emessage_username">Username:</label>
                <input type="text" id="emessage_username"/>
                </br>
                <label for="emessage_password">Password:</label>
                <input type="password" id="emessage_password"/>
                </br>
                <label for="emessage_start_interval">Start Interval(in seconds): [recommended 5s]</label>
                <input type="number" min="1" max="60" id="emessage_start_interval"/>
                </br>
                <label for="emessage_duplex_timeout">Duplex Timeout(in seconds): [recommended 60s]</label>
                <input type="number" min="10" max="1200" id="emessage_duplex_timeout"/>
                </br>
                <label for="emessage_response_timeout">Response Timeout(in seconds): [recommended 320s]</label>
                <input type="number" min="10" max="1200" id="emessage_response_timeout"/>
            </fieldset>
            
            <hr>
            <!--
                <button>Check Settings</button>
                <button>Save & Restart Dispatcher</button>
            -->
            <button onClick="saveConfigFromForm()">Save & Restart Dispatcher</button>
            </br>
            <b>Config Diff:</b>
            <pre id="diffView"></pre>
        </fieldset>
        <hr>
        <b>config.json</b>
        <pre id="jsonView"></pre>
    </body>
    <script>
        let config
        async function loadConfigToForm() {
            config = await $.getJSON('/config')
            $('#jsonView').text(JSON.stringify(config, null, '\t'))
            $('#amqpURL').val(config.general.amqp[0])
            // connectors.dapnet
            $('#dapnet_enabled').attr('checked', config.connectors.dapnet.enabled)
            $('#dapnet_duplex_timeout').val(config.connectors.dapnet.duplexTimeout)
            $('#dapnet_endpoint').val(config.connectors.dapnet.endpoint)
            $('#dapnet_username').val(config.connectors.dapnet.username)
            $('#dapnet_password').val(config.connectors.dapnet.password)
            // connectors.dapnet
            $('#lorawan_enabled').attr('checked', config.connectors.lorawan.enabled)
            $('#lorawan_duplex_timeout').val(config.connectors.lorawan.duplexTimeout)
            $('#lorawan_mqttserver').val(config.connectors.lorawan.mqttserver)
            $('#lorawan_username').val(config.connectors.lorawan.username)
            $('#lorawan_password').val(config.connectors.lorawan.password)

            $('#pocsag_enabled').attr('checked', config.connectors.pocsag.enabled)
            $('#pocsag_duplex_timeout').val(config.connectors.pocsag.duplexTimeout)

            $('#ecityruf_enabled').attr('checked', config.connectors.ecityruf.enabled)
            $('#ecityruf_duplex_timeout').val(config.connectors.ecityruf.duplexTimeout)

            $('#emp_enabled').attr('checked', config.connectors.emPuppettering.enabled)
            $('#emp_duplex_timeout').val(config.connectors.emPuppettering.duplexTimeout)

            $('#emessage_enabled').attr('checked', config.connectors.emessage.enabled)
            $('#emessage_username').val(config.connectors.emessage.username)
            $('#emessage_password').val(config.connectors.emessage.password)
            $('#emessage_response_timeout').val(config.connectors.emessage.responseTimeout)
            $('#emessage_duplex_timeout').val(config.connectors.emessage.duplexTimeout)
            $('#emessage_start_interval').val(config.connectors.emessage.startInterval)
        }
        async function saveConfigFromForm() {
            let newConfig = await $.getJSON('/config') // make a copy from the current dispatcher config
            newConfig.general.amqp = [ $('#amqpURL').val() ]
            // connectors.dapnet
            newConfig.connectors.dapnet.enabled = $('#dapnet_enabled').prop('checked')
            newConfig.connectors.dapnet.duplexTimeout = parseInt($('#dapnet_duplex_timeout').val())
            newConfig.connectors.dapnet.endpoint = $('#dapnet_endpoint').val()
            newConfig.connectors.dapnet.username = $('#dapnet_username').val()
            newConfig.connectors.dapnet.password = $('#dapnet_password').val()
            // connectors.lorawan
            newConfig.connectors.lorawan.enabled = $('#lorawan_enabled').prop('checked')
            newConfig.connectors.lorawan.duplexTimeout = parseInt($('#lorawan_duplex_timeout').val())
            newConfig.connectors.lorawan.mqttserver = $('#lorawan_mqttserver').val()
            newConfig.connectors.lorawan.username = $('#lorawan_username').val()
            newConfig.connectors.lorawan.password = $('#lorawan_password').val()
            // connectors.pocsag
            newConfig.connectors.pocsag.enabled = $('#pocsag_enabled').prop('checked')
            newConfig.connectors.pocsag.duplexTimeout = parseInt($('#pocsag_duplex_timeout').val())
            // connectors.ecityruf
            newConfig.connectors.ecityruf.enabled = $('#ecityruf_enabled').prop('checked')
            newConfig.connectors.ecityruf.duplexTimeout = parseInt($('#ecityruf_duplex_timeout').val())
            // connectors.emPuppettering
            newConfig.connectors.emPuppettering.enabled = $('#emp_enabled').prop('checked')
            newConfig.connectors.emPuppettering.duplexTimeout = parseInt($('#emp_duplex_timeout').val())
            
            // connectors.emessage
            newConfig.connectors.emessage.enabled = $('#emessage_enabled').prop('checked')
            newConfig.connectors.emessage.username = $('#emessage_username').val()
            newConfig.connectors.emessage.password = $('#emessage_password').val()
            newConfig.connectors.emessage.responseTimeout = parseInt($('#emessage_response_timeout').val())
            newConfig.connectors.emessage.duplexTimeout = parseInt($('#ecityruf_duplex_timeout').val())
            newConfig.connectors.emessage.startInterval = parseInt($('#emessage_start_interval').val())
            
            console.log(newConfig)
            await $.ajax({
                type: 'POST',
                url: '/config',
                data: JSON.stringify(newConfig),
                contentType: 'application/json',
                dataType: 'json'
            })
            $.post('/restart')
            document.body.style = 'display:none'
            setTimeout(() => window.location.reload(), 1e3)
        }
        function setLoRaWANMQTT(newMQTT) {
            $('#lorawan_mqttserver').val(newMQTT)
        }
        $(document).ready(loadConfigToForm)
    </script>
</html>