@page
@using MediaManager.database
@using MediaManager.database.Tables
@model MediaManager.Pages.Shows
@{
	ViewData["Title"] = "Shows";
}

<div class="text-center">
	<h1 class="display-5">
		Shows
		<a class="btn btn-lg btn-primary" href="/AddShow">Add</a>

	</h1>
	<table class="table table-striped table-hover">
		<thead>
		<tr>
			<th scope="col">Title</th>
			<th scope="col">Rating</th>
			<th scope="col">Rewatchability</th>
			<th scope="col">Watch count</th>
			<th scope="col">Last Watch</th>
			<th scope="col">Status</th>
			<th scope="col">Progress</th>
			<th scope="col">Comment</th>
			<th scope="col">Actions</th>
		</tr>
		</thead>
		<tbody>
		@foreach (var show in new Database.DbConn().Shows.Where(p => p.UserId == Model.AuthorizedUser.UserId).OrderBy(p => p.Title.ToLower())) {
			<tr>
				<td>
					<b>@show.Title</b>
					<br/>
					<small>@show.Year</small>
				</td>
				<td class="td-progress">
					@if (show.Rating > 0) {
						<div class="progress">
							<div class="progress-bar progress-@(show.Rating)0" role="progressbar" style="width: @(show.Rating * 10)%">@(show.Rating)</div>
						</div>
					}
					else {
						<div class="progress">
							<div class="progress-bar progress-00" role="progressbar" style="width: 0"></div>
						</div>
					}
				</td>
				<td class="td-progress">
					@if (show.Rewatchability > 0) {
						<div class="progress">
							<div class="progress-bar progress-@(show.Rewatchability)0" role="progressbar" style="width: @(show.Rewatchability * 10)%">@(show.Rewatchability)</div>
						</div>
					}
					else {
						<div class="progress">
							<div class="progress-bar progress-00" role="progressbar" style="width: 0"></div>
						</div>
					}
				</td>
				<td class="td-progress">
					<div class="progress">
						<div class="progress-bar progress-@(Math.Min(show.WatchCount, 5) * 2)0" role="progressbar" style="width: @(Math.Min(show.WatchCount, 5) * 20)%">@(show.WatchCount)</div>
					</div>
				</td>
				<td>
					@if (show.LastSeen.Year > 2000) {
						@show.LastSeen.ToString("yyyy-MM-dd")
					}
					else {
						@Html.Raw("-")
					}
				</td>
				<td>
					@Enum.GetName(show.WatchStatus)!.Replace("FirstWatch", "First Watch")
				</td>
				<td class="td-progress">
					<div class="progress position-relative">
						@{
							var progressf = 100d / show.TotalEpisodes * show.SeenEpisodes;
							var progress = (int)progressf;
						}
						<div class="progress-bar progress-@(progress - progress % 10)" role="progressbar" style="width: @progress%"></div>
						@if (progress >= 90) {
							<small class="justify-content-center d-flex position-absolute w-100" style="color: #fff">@show.SeenEpisodes / @show.TotalEpisodes</small>
						}
						else {
							<small class="justify-content-center d-flex position-absolute w-100">@show.SeenEpisodes / @show.TotalEpisodes</small>
						}
					</div>
				</td>
				<td>
					@show.Comment
				</td>
				<td>
					<div class="btn-group" role="group">
						@if (show.SeenEpisodes == show.TotalEpisodes && show.WatchStatus == WatchStatus.FirstWatch) {
							<a class="btn btn-sm btn-secondary" href="/EditShow/@show.ShowId?action=waiting">Waiting</a>
						}
						@if (show.SeenEpisodes == show.TotalEpisodes && (show.WatchStatus == WatchStatus.FirstWatch || show.WatchStatus == WatchStatus.Rewatch)) {
							<a class="btn btn-sm btn-success" href="/EditShow/@show.ShowId?action=finish">Finish</a>
						}
						@if (show.SeenEpisodes < show.TotalEpisodes && (show.WatchStatus == WatchStatus.Unwatched || show.WatchStatus == WatchStatus.FirstWatch || show.WatchStatus == WatchStatus.Rewatch)) {
							<a class="btn btn-sm btn-secondary" href="/EditShow/@show.ShowId?action=autoinc">W+1</a>
						}
						else if (show.WatchStatus == WatchStatus.Finished) {
							<a class="btn btn-sm btn-success" href="/EditShow/@show.ShowId?action=rewatch">Start rewatch</a>
						}
						else if (show.WatchStatus == WatchStatus.Waiting) {
							<a class="btn btn-sm btn-success" href="/EditShow/@show.ShowId?action=autoinc">New episode (W+1)</a>
						}
						<a class="btn btn-sm btn-primary" href="/EditShow/@show.ShowId">Edit</a>
					</div>
				</td>
			</tr>
		}
		</tbody>
	</table>
</div>
