@using InnerTube.Models
@using System.Web
@model LightTube.Contexts.ChannelContext

@{
	ViewBag.Metadata = new Dictionary<string, string>();
	ViewBag.Metadata["og:title"] = Model.Channel.Name;
	ViewBag.Metadata["og:url"] = $"{Url.ActionContext.HttpContext.Request.Scheme}://{Url.ActionContext.HttpContext.Request.Host}{Url.ActionContext.HttpContext.Request.Path}{Url.ActionContext.HttpContext.Request.QueryString}";
	ViewBag.Metadata["og:image"] = $"{Url.ActionContext.HttpContext.Request.Scheme}://{Url.ActionContext.HttpContext.Request.Host}/proxy/image?url={HttpUtility.UrlEncode(Model.Channel.Avatars.FirstOrDefault()?.Url?.ToString())}";
	ViewBag.Metadata["twitter:card"] = $"{Url.ActionContext.HttpContext.Request.Scheme}://{Url.ActionContext.HttpContext.Request.Host}/proxy/image?url={HttpUtility.UrlEncode(Model.Channel.Avatars.LastOrDefault()?.Url?.ToString())}";
	ViewBag.Metadata["og:description"] = Model.Channel.Description;
	ViewBag.Title = Model.Channel.Name;
	Layout = "_Layout";

	DynamicItem[] contents;
	try
	{
		contents = ((ItemSectionItem)((ItemSectionItem)Model.Channel.Videos[0]).Contents[0]).Contents;
	}
	catch
	{
		contents = Model.Channel.Videos;
	}
}

<div class="channel-page">
	@if (Model.Channel.Banners.Length > 0)
	{
		<img class="channel-banner" alt="Channel Banner" src="@Model.Channel.Banners.Last().Url">
	}

	<div class="channel-info-container">
		<div class="channel-info">
			<a href="/channel/@Model.Channel.Id" class="avatar">
				<img src="@Model.Channel.Avatars.LastOrDefault()?.Url" alt="Channel Avatar">
			</a>
			<div class="name">
				<a>@Model.Channel.Name</a>
				<span>@Model.Channel.Subscribers</span>
			</div>
			<button class="subscribe-button" data-cid="@Model.Channel.Id">Subscribe</button>
		</div>
	</div>

	<h3>About</h3>
	<p>@Html.Raw(Model.Channel.GetHtmlDescription())</p>
	<br><br>
	<h3>Uploads</h3>
	<div class="video-grid">
		@foreach (VideoItem video in contents.Where(x => x is VideoItem).Cast<VideoItem>())
		{
			<a href="/watch?v=@video.Id" class="video">
				<div class="thumbnail" style="background-image: url('@video.Thumbnails.LastOrDefault()?.Url')"><span class="video-length">@video.Duration</span></div>
				<div class="info">
					<span class="title max-lines-2">@video.Title</span>
					<div>
						<div>
							<span>@video.Views views</span>
							<span>@video.UploadedAt</span>
						</div>
					</div>
				</div>
			</a>	
		}
	</div>

	<div class="pagination-buttons">
		@if (!string.IsNullOrWhiteSpace(Model.ContinuationToken))
		{
			<a href="/channel?id=@Model.Id">First Page</a>
		}
		<div class="divider"></div>	
		<span>•</span>
		<div class="divider"></div>
		@if (!string.IsNullOrWhiteSpace(contents.FirstOrDefault(x => x is ContinuationItem)?.Id))
		{
			<a href="/channel/@Model.Id?continuation=@(contents.FirstOrDefault(x => x is ContinuationItem)?.Id)">Next Page</a>
		}
	</div>
</div>
