<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<!--
Sample avahi-daemon configuration for server detection:
/etc/avahi/services/bizapps.service

<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
 <name>BizApps</name>
  <service>
   <type>_http._tcp</type>
   <port>80</port>
   <txt-record>appid=bizappserver</txt-record>
   <txt-record>webprotocol=http</txt-record>
   <txt-record>webpath=/accounthub</txt-record>
   <txt-record>model=other</txt-record>
  </service>
</service-group>
-->
<br />

<div class="alert alert-blue">This tool scans for Business Apps servers on the local network.  Tap on a server to view it in a web browser.  Note: Cloud installations will not appear here.</div>
<div class="list-group" id="zero-list">
    <div class="list-group-item" id="searching-message">
        <i class="fa fa-spinner fa-spin"></i> Searching...
    </div>
    <div class="list-group-item" id="not-found-message" style="display: none;">
        <i class="fa fa-search"></i> No services found on local network.
    </div>
</div>
<div class="btn btn-primary" onclick="stopScanning(); scanZeroconf();"><i class="fa fa-refresh"></i> Rescan</div>

<script>
    var zeroconf = cordova.plugins.zeroconf;
    zeroconf.registerAddressFamily = 'ipv4'; // or 'ipv6' ('any' by default)
    zeroconf.watchAddressFamily = 'ipv4'; // or 'ipv6' ('any' by default)

    function scanZeroconf() {
        $("#searching-message").css('display', 'block');
        $("#not-found-message").css('display', 'none');
        zeroconf.watch('_http._tcp.', 'local.', function (result) {
            var action = result.action;
            var service = result.service;
            // Ignore other services
            if (typeof service.txtRecord.appid !== 'undefined' && service.txtRecord.appid == "bizappserver") {
                var ipaddr = service.ipv4Addresses[0];
                var divid = "#server" + ipaddr.replace(/\./gi, '');
                if (action == 'added') {
                    console.log('service added', service);
                    var model = service.txtRecord.model;
                    if ((service.txtRecord.webprotocol == "http" || service.txtRecord.webprotocol == "https") && Number.isInteger(service.port)) {
                        var url = service.txtRecord.webprotocol
                                + "://"
                                + ipaddr
                                + (service.port == 80 || service.port == 443 ? "" : ":" + service.port)
                                + service.txtRecord.webpath;
                        var itemcontent = "<b>" + $('<span>').text(ipaddr).html() + "</b><br />URL: " + $('<span>').text(url).html() + "<br />Model: " + $('<span>').text(model).html();
                        if (document.getElementById(divid) == null) {
                            $('#zero-list').append("<div class=\"list-group-item\" id=\"" + divid + "\" data-zeroconf=\"1\">" + itemcontent + "</div>");
                        } else {
                            $(divid).html(itemcontent);
                        }
                        document.getElementById(divid).onclick = function () {
                            window.open(url, "_system");
                        }
                    }
                } else {
                    console.log('service removed', service);
                    $(divid).remove();
                }
            }
        });
        setTimeout(stopScanning, 10 * 1000);
    }

    function stopScanning() {
        zeroconf.unwatch('_http._tcp.', 'local.');
        $("#searching-message").css('display', 'none');
        if ($(".list-group-item[data-zeroconf]").length != true) {
            $("#not-found-message").css('display', 'block');
        }
    }

    setnavbar("app", "Server Locator", "settings");
    scanZeroconf();
</script>