#!/mod/bin/jimsh

package require cgi
source /mod/webif/lib/setup
require pretty_size progressbar plugin

httpheader

set file [cgi_get file]
set type [cgi_get type]

if {$file == 0} exit

set sz [pretty_size [file size $file]]

if {$type eq "ts"} {
	require epg.class ts.class

	set ts [ts fetch $file]

	# Causes other series information to be automatically populated
	set epname [$ts episode_name]
	set series [$ts series_name]
	set tvdb [$ts get tvdb_series]
	set idata [$ts get tvdb_data]

	puts "
<table class=keyval>
<tr>
	<th>Title</th>
	<td class=va>
	"
	if {[file exists "[file rootname $file].thm"]} {
		puts "<img class=\"bmp va\"
		    src=\"/browse/bmp.jim?file=[cgi_quote_url $file]\">"
	}

	if {[dict exists $idata thumb]} {
		puts "
		<img class=\"rollimg va\" 
		    src=\"[tvdb bannerurl $idata(thumb)]\">
		<div id=rollimg class=hidden></div>
		"
	}
	puts "[$ts get title]</td>
</tr><tr>
	<th>Synopsis</th>
	<td>[$ts get synopsis]</td>
</tr><tr>
	<th>Episode</th>
	"
	puts "<td>[$ts epstr] - $epname (<i>$series</i>)"
	if {[llength $tvdb]} {
		puts "<button class=tvdbchoose>Change</button>"
	}
	puts "</td>
</tr><tr>
	"
	if {[llength $tvdb] && [llength $idata]} {
		puts "</tr><tr><th>TheTVDB</th><td class=va>"
		set sid [$tvdb get seriesid]
		puts "<a target=_blank href=[\
		    tvdb seriesurl $sid]>View on
		    <img class=va height=20 src=/img/tvdb-sm.png></a>"
		if {[[settings] tvdb_debug]} {
			puts " | "
			puts "<a href=/db/index.jim?db=$sid.db target=_blank>
			    <span class=footnote>View database($sid)</span></a>"
		}
		puts "
			<br>
			<img class=collapser id=xtvdb x=tvdb_extra
			    class=va src=/img/tree/plusonly.png>
			Found episode using [$ts get tvdb_method]
			<div id=tvdb_extra class=hidden>
				<div class=blood>
					$idata(overview)
				</div>
		"
		if {[[settings] tvdb_debug]} {
			puts "
				<div class=bluefootnote>
					[$tvdb get _phrases]
				</div>
				<div class=footnote>
					[lrange [$tvdb get _smatches] 0 8]
				</div>
			"
		}
		puts "
			</div>
		      </td></tr>"
	}

	if {[$ts flag "Guidance"] > 0} {
		puts "
			<th>Guidance</th>
			<td><img class=va
			    src=/img/Guidance_purple.png height=21>
			    [$ts get guidance]
			</td>
			</tr><tr>
		"
	}
	if {[$ts flag "GGuidance"] > 0} {
		puts "
			<th>General Guidance</th>
			<td><img class=va
			    src=/img/Guidance_blue.png height=21>
			    [$ts get guidance]
			</td>
			</tr><tr>
		"
	}

	puts "
	<th>Definition</th>
	<td>
	"
	if {[$ts flag Radio]} {
		puts "<img class=va src=/img/radio.png height=21>"
	} else {
		if {[$ts get definition] eq "HD"} {
			puts "<img class=va src=/images/172_1_00_HD.png height=21>"
		} else {
			puts "<img class=va src=/images/172_1_26_SD.png height=21>"
		}
	}
	puts "</td>
</tr><tr>
	<th>Channel</th>
	<td>[epg channelicon [$ts get channel_name] 50 "vertical-align: middle"]
	    [$ts get channel_num] - [$ts get channel_name]</td>
</tr>
	"
	lassign [$ts genre_info] txt img
	puts "
<tr>
	<th>Genre</th>
	<td><img src=\"$img\">$txt ([$ts genrenib])</td>
</tr><tr>
	<th>Start Time</th>
	<td>"
	puts [clock format [$ts get start] -format "%c %Z"]
	puts "<font class=also>(Scheduled: [
	    clock format [$ts get schedstart] -format "%c %Z"])</font>"
	puts "</td>
</tr><tr>
	<th>End Time</th>
	<td>"
	puts [clock format [$ts get end] -format "%c %Z"]
	puts "<font class=also>(Scheduled: [clock format \
	    [expr [$ts get schedstart] + [$ts get scheddur]] \
	    -format "%c %Z"])</font>"
	puts "</td>
</tr><tr>
	<th>Duration</th>
	<td>[$ts duration] minute(s).
	    <font class=also>(Scheduled: [expr [$ts get scheddur] / 60])
	    </font></td>
</tr><tr>
	<th>Status</th>
	<td>[$ts get status]</td>
</tr><tr>
	<th>Resumes</th>
	<td>[expr [$ts get resume] / 60] minutes into recording.
	    [progressbar $([$ts get resume] * 100 / [$ts duration 1])]
	</td>
</tr><tr>
	<th>Size</th>
	<td>$sz</td>
"
lassign [$ts dlnaloc] url
if {$url ne ""} {
	puts "</tr><tr><th>DLNA&nbsp;URL</th>
	   <td><a href=\"$url\">$url</a></td>"
}
puts "
</tr><tr>
	<th>Files</th>
	<td>
"

set fileset [$ts fileset]
puts -nonewline "<img class=collapser x=fileset id=xfileset class=va
	src=/img/tree/plusonly.png>"
puts -nonewline "[llength $fileset] file"
if {[llength $fileset] != 1} { puts -nonewline "s" }
puts -nonewline " in set."
puts "<div id=fileset class=hidden><ul>"
foreach f $fileset {
	puts "<li>[file tail $f] ([pretty_size [file size $f]])</li>" 
}
puts "</ul></div>"

puts "</td></tr>"

eval_plugins browsetsfile

puts "<tr>
	<th>Flags</th>
	<td>[$ts get flags]</td>
      </tr>
"

if {[$ts get bookmarks]} {
		puts "
    <tr>
	<th>Bookmarks</th>
	<td>[$ts get bookmarks] @ [join [lmap i [$ts bookmarks 1] {
		clock format $i -format "%T"
	    }] ", "]
	</td>
    </tr>
		"
}

puts "
	</table>
"
puts "<div class=hidden id=file>$file</div>"
puts {
<script type=text/javascript>
var file = $('#file').text();
$('button').button();
$('.tvdbchoose').on('click', function() {
	window.location.href = '/browse/tvdb/episode.jim?file=' +
	    encodeURIComponent(file);
});
$('.collapser').on('click', function() {
	target = $(this).attr('x');
	var $icon = $(this);
	$('#' + target).slideToggle('slow', function() {
		if ($(this).is(':visible'))
			$icon.attr('src', '/img/tree/minustop.png');
		else
			$icon.attr('src', '/img/tree/plusonly.png');
			
	});
});
$('img.rollimg').hover(
    function() {
	var img = this, $img = $(img), offset = $img.offset;

	$('#rollimg').css({
		top: offset.top,
		left: offset.left
	})
	.append($img.clone().removeClass('rollimg'))
	.removeClass('hidden');
    },
    function() {
	$('#rollimg').empty().addClass('hidden');
    }
);

</script>
}
	exit
}

# Otherwise, for a general file.

puts "
<table class=keyval>
<tr>
	<th>File</th>
	<td>$file</td>
</tr><tr>
	<th>Size</th>
	<td>$sz</td>
</tr><tr>
	<th>Info</th>
	<td class=pre id=ffmpeg>
		<img src=/img/spin.gif><i>Loading...</i>
	</td>
</tr>
</table>
"

set url "/browse/ffmpeg.jim?file=[cgi_quote_url $file]"
puts { <script type="text/javascript"> }
puts "var url = \"$url\";"
puts {
	$('#ffmpeg').load(url);
</script>
}

