@page
@using global::c3stream.DataModels
@using System.Net.Http
@using System.Net
@using System.IO
@model InfoModel
@{
	ViewData["Title"] = "Info";
}
@{
	if (string.IsNullOrWhiteSpace(Request.Query["guid"]) || c3stream.GetEventByGuid(Request.Query["guid"]) == null) {
		Response.Redirect("/");
		return;
	}

	var cookie = c3stream.UpdateCookie(Request, Response, $"/Info?guid={Request.Query["guid"]}");

	await using var db = new Database.DbConn();

	var talk = c3stream.GetEventByGuid(Request.Query["guid"]);
	var state = db.States.FirstOrDefault(p => p.TalkId == Request.Query["guid"].ToString() && p.UserId == cookie)?.State;
	if (talk == null) {
		Response.Redirect("/");
		return;
	}
	if (state == null) {
		state = "unwatched";
	}

	var title = talk.Title;
	var speakers = talk.Persons.Any() ? talk.Persons.Aggregate((s, s1) => $"{s}, {s1}") : "<no speakers>";
	var description = talk.Description;
	if (string.IsNullOrEmpty(description)) {
		description = "&lt;missing description&gt;";
	}

	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];
	var logoPath = System.IO.Path.Combine(c3stream.LogoPath, conference.Acronym + ".png");

	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>"
		};
}

@if (!System.IO.File.Exists(logoPath)) {
	using var httpClient = new HttpClient();
	await using var stream = httpClient.GetStreamAsync(conference.LogoUri).Result;
	await using var fileStream = new FileStream(logoPath, FileMode.CreateNew);
	await stream.CopyToAsync(fileStream);
}

<img src="@(c3stream.LogoUrl + $"{conference.Acronym}.png")" alt="Conference logo" style="max-height: 110px; float: right;"/>

@if (isWatched) {
	<h3 style="color: #95cb7a">@title - <i>@speakers</i></h3>
}
else if (isMarked) {
	<h3 style="color: #da7d4f">@title - <i>@speakers</i></h3>
}
else {
	<h3>@title - <i>@speakers</i></h3>
}

<h5>@eventName - @category - @talk.Date?.Date.ToShortDateString()</h5>
<div class="btn-group" role="group" style="margin-bottom: 10px">
	<a href="@talk.FrontendLink.AbsoluteUri" role="button" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="right" 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="right" 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>
	}
	@if (isWatched) {
		<button onclick="SetState('@talk.Guid', 'unwatched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="left" 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="left" 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="left" 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="left" 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="left" title="Add to watch later">
			<i class="fas fa-clock"></i>
		</button>
	}
</div>

<p style="text-align: justify">
	@Html.Raw(description.Replace("\n", "<br/>").Replace("<p>", "").Replace("</p>", ""))
</p>

Share this talk:<br/>
<code onclick="copyToClipboard(this)">https://@Request.Host.Value/Info?guid=@Request.Query["guid"]</code>
