@page
@using global::c3stream.DataModels
@model WatchlistModel
@{
	var cookie = c3stream.UpdateCookie(Request, Response, "/Watchlist");
	ViewData["Title"] = "Watchlist";
	await using var db = new Database.DbConn();
	var states = db.States.ToList();
	var marked = db.States.Where(p => p.UserId == cookie && p.State == "marked").Select(p => p.TalkId).ToList();
	var watchlist = c3stream.GetEventsByGuid(marked);
}

<table class="table">
	<thead>
	<tr>
		<th scope="col">Conference</th>
		<th scope="col">Event</th>
		<th scope="col">
			@Html.Raw(Request.Query["orderby"] == "published" ? "<a href=\"/Watchlist\">Published" : "<a href=\"/Watchlist?orderby=published\">Date")
		</th>
		<th scope="col">Category</th>
		<th scope="col">Title</th>
		<th scope="col">Speaker(s)</th>
		<th scope="col">Lang</th>
		<th scope="col">Actions</th>
	</tr>
	</thead>
	<tbody>
	@foreach (var talk in Request.Query["orderby"] == "published" ? watchlist.OrderByDescending(p => p.ReleaseDate) : watchlist.OrderBy(p => p.Date)) {
		var state = states.FirstOrDefault(p => p.TalkId == talk.Guid && p.UserId == cookie)?.State;
		var isWatched = state == "watched";
		var isMarked = state == "marked";
		var file = $"{talk.Slug}.mp4";
		var conference = c3stream.GetConferenceByEventGuid(talk.Guid);
		var eventName = talk.Tags.Count <= 1 ? conference.Acronym : talk.Tags[0].Replace("-", "-<br/>");
		var category = talk.Tags.Count switch {
			0 => "<no category>",
			1 => talk.Tags[0],
			2 => "<no category>",
			3 => talk.Tags[2],
			4 => talk.Tags[3],
			5 => talk.Tags[3],
			6 => talk.Tags[3],
			7 => talk.Tags[3],
			_ => "<unknown tag format>"
			};
		<tr>
			<td>@Html.Raw(conference.Acronym)</td>
			<td>@Html.Raw(eventName)</td>
			<td>@(Request.Query["orderby"] == "published" ? talk.ReleaseDate?.Date.ToShortDateString() : talk.Date?.Date.ToShortDateString())</td>
			<td>@category</td>
			@if (isWatched) {
				<td style="color: #95cb7a">@talk.Title</td>
			}
			else if (isMarked) {
				<td style="color: #da7d4f">@talk.Title</td>
			}
			else {
				<td>@talk.Title</td>
			}
			<td>@(talk.Persons.Any() ? talk.Persons.Aggregate((s, s1) => $"{s}, {s1}") : "<no speakers>")</td>
			<td>@talk.OriginalLanguage</td>
			<td>
				<div class="btn-group" role="group">
					<a href="@talk.FrontendLink.AbsoluteUri" role="button" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Play">
						<i class="fas fa-play-circle"></i>
					</a>
					@if (System.IO.File.Exists(System.IO.Path.Combine(c3stream.CachePath, conference.Acronym, file))) {
						<a href="@(c3stream.CacheUrl + $"{conference.Acronym}/{file}")" role="button" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Mirror">
							<i class="fas fa-cloud-download"></i>
						</a>
					}
					else {
						<a href="/" role="button" class="btn btn-primary btn-c3saction disabled">
							<i class="fas fa-cloud-download"></i>
						</a>
					}
					<a href="/Info?guid=@talk.Guid" role="button" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Info">
						<i class="fas fa-info-circle"></i>
					</a>
					@if (isWatched) {
						<button onclick="SetState('@talk.Guid', 'unwatched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Mark unwatched">
							<i class="fas fa-times"></i>
						</button>
						<button class="btn btn-primary btn-c3saction disabled">
							<i class="fas fa-clock"></i>
						</button>
					}
					else if (isMarked) {
						<button onclick="SetState('@talk.Guid', 'watched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Mark watched">
							<i class="fas fa-check"></i>
						</button>
						<button onclick="SetState('@talk.Guid', 'unwatched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Remove from watch later">
							<i class="fas fa-undo-alt"></i>
						</button>
					}
					else {
						<button onclick="SetState('@talk.Guid', 'watched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Mark watched">
							<i class="fas fa-check"></i>
						</button>
						<button onclick="SetState('@talk.Guid', 'marked')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Add to watch later">
							<i class="fas fa-clock"></i>
						</button>
					}
				</div>
			</td>
		</tr>
	}
	</tbody>
</table>
