UI/CompletionStandingsCard.razor
@using Sandbox
@using Sandbox.UI
@using System
@using System.Linq
@inherits Panel
@namespace BlockParty

<root class="medal-card completion-card">
	<div class="heading">
		<label class="title">LEVELS BEATEN</label>
		@if ( Expanded )
		{
			<button class="square-btn i-close close" onclick=@OnClose><div class="img"></div></button>
		}
	</div>
	@if ( CompletionStandings.UpdatedAt == default )
	{
		@if ( !Expanded && CompletionStandings.Notice is not null )
		{
			<label class="notice">@CompletionStandings.Notice</label>
		}
	}
	else if ( CompletionStandings.Players.Count == 0 )
	{
		<label class="notice">No completions recorded yet.</label>
	}
	else
	{
		<div class="medal-list">
			@foreach ( var player in CompletionStandings.Players.Take( Expanded ? CompletionStandings.Players.Count : 5 ) )
			{
				<div class="medal-row @(player.SteamId == (long)Game.SteamId ? "you" : "")">
					<label class="rank">@player.Rank</label>
					<SteamAvatar [email protected] Size=@(Expanded ? 36 : 28)></SteamAvatar>
					<LiteralLabel class="name" [email protected] />
					<label class="completion-count @(CompletionStandings.IsComplete( player.Value ) ? "complete" : "")">@player.Value.ToString( "0" )</label>
				</div>
			}
		</div>
		@if ( CompletionStandings.Personal is CompletionEntry me
			&& !CompletionStandings.Players.Take( Expanded ? CompletionStandings.Players.Count : 5 ).Any( p => p.SteamId == me.SteamId ) )
		{
			<div class="medal-row you personal">
				<label class="rank">@me.Rank</label>
				<label class="name">YOU</label>
				<label class="completion-count @(CompletionStandings.IsComplete( me.Value ) ? "complete" : "")">@me.Value.ToString( "0" )</label>
			</div>
		}
	}
	@if ( Expanded )
	{
		@if ( CompletionStandings.HasMore )
		{
			<button class="view-all" [email protected]>@(CompletionStandings.Loading ? "LOADING..." : "LOAD MORE")</button>
		}
		@if ( CompletionStandings.UpdatedAt != default )
		{
			<label class="freshness">@Freshness</label>
		}
		@if ( CompletionStandings.Notice is not null )
		{
			<label class="explanation">@CompletionStandings.Notice</label>
		}
	}
	else
	{
		<button class="view-all" onclick=@OnOpen>VIEW MORE</button>
	}
</root>

@code
{
	[Parameter] public bool Expanded { get; set; }
	[Parameter] public Action OnOpen { get; set; }
	[Parameter] public Action OnClose { get; set; }
	private double AgeMinutes => CompletionStandings.UpdatedAt == default ? 0 : Math.Max( 0, (DateTimeOffset.UtcNow - CompletionStandings.UpdatedAt).TotalMinutes );
	private string Freshness => $"Updated {(AgeMinutes < 1 ? "just now" : AgeMinutes < 60 ? $"{(int)AgeMinutes}m ago" : $"{(int)(AgeMinutes / 60)}h ago")}";
	protected override int BuildHash() => HashCode.Combine( Expanded, CompletionStandings.Revision, (int)AgeMinutes, LevelMap.Nodes.Count );
}