@page
@model ConferenceModel
@using System.Net
@using static ConferenceModel
@{
    int tagFormat;
    switch (Request.Query["c"]) {
        case "36c3":
            tagFormat = 2;
            break;
        case "35c3":
        case "34c3":
            tagFormat = 1;
            break;
        case "33c3":
            tagFormat = 0;
            break;
        default:
            Response.Redirect("/");
            return;
    }

    c3stream.UpdateCookie(Request, Response, $"/Conference?c={Request.Query["c"]}&");

    ViewData["Title"] = Request.Query["c"];
    var wc = new WebClient();
    var jsonpath = System.IO.Path.Combine(c3stream.DataPath, Request.Query["c"] + ".json");
    var json = System.IO.File.Exists(jsonpath) ? System.IO.File.ReadAllText(jsonpath) : wc.DownloadString($"https://api.media.ccc.de/public/conferences/{Request.Query["c"]}");
    var conference = Conference.FromJson(json);
    wc.Dispose();
}

<table class="table">
    <thead>
    <tr>
        <th scope="col">Event</th>
        <th scope="col">
            @Html.Raw(Request.Query["orderby"] == "published" ? $"<a href=\"/Conference?c={Request.Query["c"]}&bookmark={Request.Query["bookmark"]}\">Published" : $"<a href=\"/Conference?c={Request.Query["c"]}&bookmark={Request.Query["bookmark"]}&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" ? conference.Events.OrderByDescending(p => p.ReleaseDate) : conference.Events.OrderBy(p => p.Date)) {
        TalkMetadata metadata;
        if (EventMetadata.Any(p => p.Guid == talk.Guid?.ToString())) {
            metadata = EventMetadata.First(p => p.Guid == talk.Guid?.ToString());
        }
        else {
            metadata = GetEvent(talk.Guid?.ToString());
            EventMetadata.Add(metadata);
        }
        var isWatched = EventMetadata.Any(p => p.Guid == talk.Guid?.ToString() && p.State.FirstOrDefault(q => q.Guid == Request.Cookies["bookmark"])?.State == "watched");
        var isMarked = EventMetadata.Any(p => p.Guid == talk.Guid?.ToString() && p.State.FirstOrDefault(q => q.Guid == Request.Cookies["bookmark"])?.State == "marked");
        var file = metadata.Talk.Recordings.FirstOrDefault(p => System.IO.File.Exists(System.IO.Path.Combine(c3stream.CachePath, conference.Acronym, p.Filename)));
        var eventName = tagFormat == 0 ? conference.Acronym : talk.Tags[0].Replace("-", "-<br/>");
        var category = tagFormat switch {
            0 => talk.Tags[0],
            1 => talk.Tags[2],
            2 => talk.Tags[3],
            _ => ""
            };
        <tr>
            <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: limegreen">@talk.Title</td>
            }
            else if (isMarked) {
                <td style="color: orangered">@talk.Title</td>
            }
            else {
                <td>@talk.Title</td>
            }
            <td>@talk.Persons.Aggregate((s, s1) => $"{s}, {s1}")</td>
            <td>@talk.OriginalLanguage</td>
            <td>
                <div class="btn-group w-100" role="group">
                    <a href="@talk.FrontendLink.AbsoluteUri" target="_blank" type="button" class="btn btn-primary w-100" data-toggle="tooltip" data-placement="top" title="Play">
                        <i class="fas fa-play-circle"></i>
                    </a>
                    @if (file != null) {
                        <a href="@(c3stream.CacheUrl + $"{conference.Acronym}/{file.Filename}")" target="_blank" type="button" class="btn btn-primary w-100" data-toggle="tooltip" data-placement="top" title="Mirror">
                            <i class="fas fa-cloud-download"></i>
                        </a>
                    }
                    else {
                        <a href="/" target="_blank" type="button" class="btn btn-primary disabled">
                            <i class="fas fa-cloud-download"></i>
                        </a>
                    }
                    <a href="/Info?guid=@talk.Guid?.ToString()&bookmark=@Request.Query["bookmark"]" target="_blank" type="button" class="btn btn-primary 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 w-100" data-toggle="tooltip" data-placement="top" title="Mark unwatched">
                            <i class="fas fa-times"></i>
                        </button>
                        <button class="btn btn-primary disabled">
                            <i class="fas fa-clock"></i>
                        </button>
                    }
                    else if (isMarked) {
                        <button onclick="SetState('@talk.Guid', 'watched')" class="btn btn-primary 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 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 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 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>