<%
// This file is used SERVER SIDE only, it MUST comply to ECMA Script 5.1 and not
// use jQuery or any other client side extensions.
//
// e.g. ECMA Script 5.1 cannot parse ISO DateTime strings

"use strict";

import "/js/utility.js"

    var myth = new Myth();

    // FIXME: This should use a new services method so that we can use QLocale::toString() instead
    function filesizeToString(bytes)
    {
        var size = bytes * (1.0 / (1024.0 * 1024.0 * 1024.0));
        return size.toFixed(2) + " GB";
    }

    function buildDetailHTML(program, type)
    {
        if (!isValidObject(program) || program.Title === "")
            return qsTr("No matching program found");

        var dvr = new Dvr();

        var durationStr = "";
        var duration = program.EndTime - program.StartTime;

        if (type == "recording")
            duration = program.Recording.EndTs - program.Recording.StartTs; // Use Recording Start/End Times

        durationStr += Math.floor(duration / 1000 / 60 / 60) > 0 ? Math.floor(duration / 1000 / 60 / 60) + "hr " : "";
        durationStr += (Math.floor(duration / 1000 / 60) % 60) > 0 ? (Math.floor(duration / 1000 / 60) % 60) + "m" : "";

        var html = "";

        if (program.Season > 0)
        {
            html += '<label>' + qsTr("Season:") + ' </label><span id="detailSeason">' + program.Season + '</span> ';
        }
        if (program.Episode > 0)
        {
            html += '<label>' + qsTr("Episode:") + ' </label><span id="detailEpisode">' + program.Episode;
            if (program.TotalEpisodes > 0)
            {
                html += '/' + program.TotalEpisodes;
            }
            html += '</span><br />';
        }
        else if (program.Season > 0)
        {
            html += '<br />';
        }
        if (type == "program" && program.Recording.Status != 0) // Don't show status for recordings
        {
            html += '<label>' + qsTr("Recording Status:") + ' </label><span id="detailRecStatus">' + dvr.RecStatusToString(program.Recording.Status) + '</span><br />';
        }
        if (program.FileSize > 0)
        {
            html += '<label>' + qsTr("Size:") + ' </label><span id="detailSize">' + filesizeToString(program.FileSize) + '</span><br />';
        }
        if (type == "program")
        {
            html += '<label>' + qsTr("Start/End Times:") + ' </label><span id="detailStartEnd">' + escapeHTML(myth.GetFormatTime(program.StartTime)) + ' - ' + escapeHTML(myth.GetFormatTime(program.EndTime)) + '</span><br />';
        }
        html += '<label>' + qsTr("Duration:") + ' </label><span id="detailDuration">' + durationStr + '</span><br />';
        if (program.Category != "")
        {
            html += '<label>' + qsTr("Category:") + ' </label><span id="detailCategory">' + escapeHTML(toCapitalCase(program.Category)) + '</span><br />';
        }
        if (program.Recording.RecGroup != "")
        {
            html += '<label>' + qsTr("Recording Group:") + ' </label><span id="detailRecGroup">' + escapeHTML(program.Recording.RecGroup) + '</span>';
        }

        return html;
    }

    // Why, you ask, don't we call GetProgramDetails() directly via ajax and
    // parse the JSON into html on the client side since it would be smaller?
    //
    // Simply because the raw values from the services API aren't ready for
    // display to the user, they need formatting or converting
    // (enum integers to strings, dates to the users preferred format etc)
    // That means calling yet more services API methods to get the values we
    // want to display and would easily negate the benefits of using the JSON.
    //
    // Besides, the the backend is going to be powerful enough to build the HTML
    // faster than the average mobile/tablet can do it client-side
    //
    function getProgramDetailHTML()
    {
        var startTime = myth.ParseISODateString(getArg("StartTime"));

        var guide = new Guide();
        var program = guide.GetProgramDetails(getArg("ChanId"), startTime);
        return buildDetailHTML(program, "program");
    }

    function getRecordingDetailHTML()
    {
        var dvr = new Dvr();
        var program = dvr.GetRecorded(getArg("RecordedId"));
        return buildDetailHTML(program, "recording");
    }

    var ret;
    switch (getArg("_action"))
    {
        case "getProgramDetailHTML":
            ret = getProgramDetailHTML();
            break;
        case "getRecordingDetailHTML":
            ret = getRecordingDetailHTML();
            break;
        default:
            break;
    }
%>
<%=ret%>
