<%
"use strict";

import "/js/utility.js"
%>

<html>
<head>
<title><i18n>MythTV System Overview</i18n></title>
<link rel="stylesheet" type="text/css" href="/css/site.css">
<link rel="stylesheet" type="text/css" href="/misc/css/overview.css">
<script type="text/javascript"  src="/js/util.qjs"></script>
<script type="text/javascript"  src="/3rdParty/jquery/jquery-2.1.4.min.js"></script>
<script type="text/javascript"  src="/3rdParty/jquery/jqueryUI/js/jquery-ui-1.10.3.js"></script>
<script type="text/javascript"  src="/3rdParty/jquery/galleria/galleria.js"></script>
<script type="text/javascript"  src="/misc/js/overview.js"></script>
</head>
<body>

    <div class="header">
        <h2><i18n>Last Ten Recordings</i18n></h2>
    </div>

    <div class="contentContainer" id="lasttencontent">
<%

            var oDvr = new Dvr();
            var oMyth = new Myth();
            var list = oDvr.GetRecordedList( true, 0, 10, "", "", "", "", "" );

            // For padding integer values with leading zeros
            function pad(n)
            {
                return n<10 ? '0'+n : n
            }

            for (var nIdx=0; nIdx < list.Programs.length; nIdx++)
            {
                var program = list.Programs[ nIdx ];
                var title = '';
                if (program.SubTitle == '')
                    title = program.Title;
                else if (program.Episode > 0)
                    title = program.Title + " " + program.Season + "x" + pad(program.Episode) + " (" + program.SubTitle + ")";
                else
                    title = program.Title + " (" + program.SubTitle + ")";

                title = escapeHTML(title);

                var desc = formatStr('<i18n>%1<br>Recorded %2 at %3.</i18n>',
                    escapeHTML(program.Description), oMyth.GetFormatDate(program.StartTime),
                    oMyth.GetFormatTime(program.StartTime));
%>
                <img title="<%=title%>" alt="<%=desc%>" height="382" src="/Content/GetPreviewImage?RecordedId=<%=program.Recording.RecordedId%>&amp;Height=371&amp;Format=JPG"/>
<%
            } 
%>
    </div>

    <div class="header">
        <h2><i18n>Current Tuner Activity</i18n></h2>
    </div>

    <div class="contentContainer">
        <div class="content indented" id="encodercontent">
            <ul>
<%
            var oDvr = new Dvr();
            var encoderlist = oDvr.GetEncoderList();
            for (var nIdx=0; nIdx < encoderlist.Encoders.length; nIdx++)
            {
                var encoder = encoderlist.Encoders[ nIdx ];
                var statestring = '';
                var state = encoder.State;
                switch(state)
                {
                    case 1:
                        var recording = encoder.Recording;
                        statestring = formatStr('<i18n>Encoder %1 is currently watching "%2" on Live TV.</i18n>',
                            encoder.Id, escapeHTML(recording.Title));
                        break;
                    case 6:
                        var recording = encoder.Recording;
                        statestring = formatStr('<i18n>Encoder %1 is currently recording "%2."  This program is being viewed.</i18n>',
                            encoder.Id, escapeHTML(recording.Title));
                        break;
                    case 7:
                        var recording = encoder.Recording;
                        var endtime = oMyth.GetFormatTime(recording.EndTime);
                        statestring = formatStr('<i18n>Encoder %1 is currently recording "%2."  This recording will end at %3.</i18n>',
                            encoder.Id, escapeHTML(recording.Title), endtime);
                        break;
                    default:
                        statestring = formatStr('<i18n>Encoder %1 is currently idle.</i18n>',
                            encoder.Id);
                        break;
                }

                os.writeln("<li>" + statestring + "</li>");
            }
%>

            </ul>
        </div>
    </div>

    <div class="header">
        <h2><i18n>Frontend Status</i18n></h2>
    </div>

    <div class="contentContainer">
        <div class="content frontendList">
<%
    var myth = new Myth();
    var frontendList = myth.GetFrontends(false).Frontends;
    for (var nIdx = 0; nIdx < frontendList.length; nIdx++)
    {
        var status = qsTr("Offline");
        var statusClass = "frontendOffline";
        if (frontendList[nIdx].OnLine)
        {
            status = qsTr("Online");
            statusClass = "frontendOnline";
        }

        var frontendIP = frontendList[nIdx].IP;
        if (frontendIP.length > 15) // IPv6 Address
            frontendIP = "[" + frontendIP + "]"; // Yay for stupid design of IPv6
%>
            <div class="frontendRow" id="frontendRow-<%=frontendList[nIdx].Name%>">
                <div class="frontendName">
                    <a href="http://<%=frontendIP%>:<%=frontendList[nIdx].Port%>/"><%=frontendList[nIdx].Name%></a>
                </div>
                <div class="frontendStatus <%=statusClass%>" id="frontendStatus-<%=frontendList[nIdx].Name%>">
                    <%=status%>
                </div>
                <script type="text/javascript" nonce="<%=CSP_NONCE%>">MythOverview.GetFrontendStatus('<%=frontendList[nIdx].Name%>',
                                                                 '<%=frontendIP%>:<%=frontendList[nIdx].Port%>')</script>
            </div>
<%
    }
%>
        </div>
    </div>

</body>
</html>
