OIS = {};

(function ($) {
    "use strict";

    function formatDate(value) {
        if (value) {
            var date = new Date(Date.parse(value));
            return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
        } else {
            return '';
        }
    }
    
    function textToHTML(text) {
        return (text || "")
            .replace(/&/g, "&amp;")
            .replace(/</g, "&lt;")
            .replace(/>/g, "&gt;")
            .replace(/\t/g, "    ")
            .replace(/ /g, "&#8203;&nbsp;&#8203;")
            .replace(/\r\n|\r|\n/g, "<br />");
    }

    OIS.ShowAppInfo = function (Info) {

        $('#ois-app-title').text(Info.App.Title);
        $('#ois-app-name').text(Info.App.Name);
        $('#ois-app-oid').text(Info.App.OID);
        $('#ois-app-server-oid').text(Info.Server.OID);
        $('#ois-app-auto-session').text(Info.App.AutoSession);
        $('#ois-app-session-timeout').text(Info.App.SessionTimeout);
        $('#ois-pool-total').text(Info.Pool.Total);
        $('#ois-pool-used').text(Info.Pool.Used);
        $('#ois-pool-unused').text(Info.Pool.Unused);
        $('#ois-pool-max').text(Info.Pool.Max);
        $('#ois-pool-recycle-interval').text(Info.Pool.RecycleInterval);

        var $tbody = $('#ois-session-list > tbody');
        $tbody.html('');
        Info.Sessions.forEach(function (item) {
            $tbody.append('<tr><td>' + item.ID + '</td><td>' + item.Host + '</td><td>' + formatDate(item.Last) + '</td><td>' + item.IsExecuting + '</td><td>' + item.IsTerminated + '</td><td>' + item.InTransaction + '</td></tr>');
        });

        $tbody = $('#ois-handler-list > tbody');
        $tbody.html('');
        Info.Handlers.forEach(function (item) {
            $tbody.append(
                '<tr>' +
                    '<td>' + item.Enabled + '</td>' +
                    '<td>' + item.OID + '</td>' +
                    '<td>' + item.Name + '</td>' +
                    '<td>' + item.Url + '</td>' +
                    '<td>' + item.Type + '</td>' +
                    '<td><a href="' + item.WSDL + '">' + item.WSDL + '</a></td>' +
                    '<td>' + item.CallCount + '</td>' +
                    '<td>' + item.CallMinTime + '</td>' +
                    '<td>' + item.CallMaxTime + '</td>' +
                    '<td>' + item.CallAvgTime + '</td>' +
                    '<td>' + item.ErrorCount + '</td>' +
                    '<td>' + formatDate(item.LastErrorTime) + '</td>' +
                    '<td>' + item.LastErrorMessage + '</td>' +
                    '</tr>'
            );
        });
    };

    OIS.LoadAppInfo = function () {

        $.getJSON('\\admin\\info')
            .done(function (data, textStatus, jqXHR) {
                $('#ajax-error').hide();
                OIS.ShowAppInfo(data);
            })
            .fail(function (jqXHR, textStatus, errorThrown) {
                $('#ajax-error').show();
            })
            .always(function () {

            });
    };
    
    OIS.LoadLog = function ($tbody) {

        $.get('\\admin\\log')
            .done(function (data, textStatus, jqXHR) {
                $tbody.html('');
                var rows = Papa.parse(data).data,
                    id = 0;
                rows.forEach(function (row) {
                    if (Array.isArray(row) && row.length > 3) {
                        id++;
                        
                        $tbody.append('<tr id="r' + id + '"><td>' + row[0] + '</td><td>' + row[2] + '</td><td><div class="large-cell"><div></td></tr>');
                        var $row = $('#r' + id),
                            $txt = $('.large-cell', $row);
                        
                        $txt.html(textToHTML(row[3]));
                        
                        $row.on('click', function (event) {
                            $txt.toggleClass('large-cell');
                        });
                    }
                });
            })
            .fail(function (jqXHR, textStatus, errorThrown) {
                
            })
            .always(function () {

            });
    };
})(jQuery);
