<%
"use strict";

import "/js/utility.js"
import "/tv/js/constants.js"
import "/tv/js/tvutil.qjs"

    var playlistFormat = "";
    if (getArg("PlaylistFormat"))
        playlistFormat = getArg("PlaylistFormat");

    var mediaType = "";
    if (getArg("MediaType"))
        mediaType = getArg("MediaType");

    var mediaID = "";
    if (getArg("MediaID"))
        mediaID = getArg("MediaID");

    var mythLogo = formatStr("http://%1:%2/images/mythtv.png", Server.SERVER_NAME, Server.SERVER_PORT);
    var playlistTitle = qsTr("MythTV PlayList");
    var url = "";
    var creator = "";
    var title = "";
    var duration = "";
    var album = "";
    var trackNumber = 0;
    var artworkUrl = mythLogo;
    switch (mediaType)
    {
        case "recording":
            var dvr = new Dvr();
            var program = dvr.GetRecorded(mediaID);

            if (program.SubTitle)
                title = formatStr("%1 - %2", program.Title, program.SubTitle);
            else
                title = program.Title;

            duration = (program.Recording.EndTs - program.Recording.StartTs) / 1000;
            url = formatStr("http://%1:%2/Content/GetRecording?RecordedId=%3", Server.SERVER_NAME, Server.SERVER_PORT, mediaID);
            //creator = program.Channel.ChannelName;
            if (program.Artwork.length)
                artworkUrl = formatStr("http://%1:%2/%3", Server.SERVER_NAME, Server.SERVER_PORT, program.Artwork[0].URL);
            //trackNumber = playlistItemNum;
            break;
        case "video":
            var video = new Video();
            var videoMetadata = video.GetVideo(mediaID);
            title = videoMetadata.Title;
            duration = videoMetadata.Length;
            url = "/Content/GetVideo?Id=" + mediaID;
            break;
        case "music":
            var music = new Music();
            var musicMetadata = music.GetTrack(mediaID);
            title = musicMetadata.Title;
            creator = musicMetadata.Artist;
            url = "/Content/GetMusic?Id=" + mediaID;
            break;
    }

    switch (playlistFormat)
    {
        case "m3u": // ASCII - Technically we cannot do ASCII, so fallthrough for now
        case "m3u8": // UTF-8
            this.ResponseHeaders["Content-Type"] = "application/x-mpegurl; charset=utf-8";
            this.ResponseHeaders["Content-Disposition"] = formatStr("attachment; filename=%1.m3u8", mediaID);
            var m3uTitle;
            if (creator)
                m3uTitle = formatStr("%1 - %2", creator, title);
            else
                m3uTitle = title;
%>
#EXTM3U

#EXTINF:<%=duration%>, <%=m3uTitle%>
<%=url%>

<%
            break;
        case "asx":
            this.ResponseHeaders["Content-Type"] = "video/x-ms-asf; charset=utf-8";
            this.ResponseHeaders["Content-Disposition"] = formatStr("attachment; filename=%1.asx", mediaID);
            var durationStr = toHHMMSS(duration);
            var asxTitle;
            if (creator)
                asxTitle = formatStr("%1 - %2", creator, title);
            else
                asxTitle = title;
%>
<ASX VERSION="3.0">
  <PARAM NAME="Encoding" VALUE="utf-8" />
  <TITLE><%=escapeXML(playlistTitle)%></TITLE>

  <ENTRY>
    <TITLE><%=escapeXML(asxTitle)%></TITLE>
    <REF HREF="<%=url%>" />
    <AUTHOR><%=escapeXML(creator)%></AUTHOR>
    <DURATION VALUE="<%=durationStr%>" />
  </ENTRY>

</ASX>
<%
            break;
        case "xspf":
            this.ResponseHeaders["Content-Type"] = "application/xspf+xml; charset=utf-8";
            this.ResponseHeaders["Content-Disposition"] = formatStr("attachment; filename=%1.xspf", mediaID);
            var durationStr = formatStr("%1:%2:%3", Math.floor(duration/3600), Math.floor(duration/60 % 60), duration % 60);
%>
<?xml version="1.0" encoding="UTF-8"?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
    <title><%=escapeXML(playlistTitle)%></title>
    <image><%=mythLogo%></image>
    <trackList>
        <track>
            <creator><%=escapeXML(creator)%></creator>
            <album><%=escapeXML(album)%></album>
            <title><%=escapeXML(title)%></title>
            <location><%=url%></location>
            <duration><%=duration * 1000%></duration>
            <image><%=artworkUrl%></image>
            <trackNum><%=trackNumber%></trackNum>
        </track>
    </trackList>
</playlist>
<%
            break;
    }
%>
