@using Core.Entities
@using Core.Utility
@model Tuple<Team[], Student[]>
@{
	ViewData["Title"] = "Teams";
}

<table class="table">
	<thead>
		<tr>
			<td>Team</td>
			@*<td>Time Slot</td>*@
			<td>Notes</td>
			<td>Team Members</td>
		</tr>
	</thead>
	@foreach (var team in Model.Item1.OrderBy(t => t.RegionalTimeSlotObj))
	{
		var students = team.Students;
		@if (team.Event.Format == EventFormat.Team)
		{
			<tr>
				<td>
					@* <strong class="@{ GetTeamClass(team);}">@team.Name</strong> *@
					@team.Name #@team.TeamNumber
				</td>
				@*<td>
					@team.RegionalTimeSlot
				</td>*@
				<td>
					@team.Event.RegionalNotes
				</td>
				<td>
					@{
						var first = true;
					}
					@foreach (var student in students.OrderByDescending(s => (s.Grade + s.TsaYear) * (team.Captain == s ? 2 : 1)).ThenBy(s => s.FirstNameLastName))
					{
						@if (!first)
						{
							<text>, </text>

						}
						first = false;
						@student.FirstNameLastName @if (team.Captain == student)
						                           {
							                           <text> (Cpt.) (@student.RegionalID)</text>
						                           }
					}
				</td>
			</tr>
		}
		else if (team.Event.Format == EventFormat.Individual)
		{
			foreach (var student in students)
			{
				<tr>
					<td>
						@team.Event.Name #@student.RegionalID
					</td>
					<td>@team.Event.RegionalNotes</td>
					<td>@student.FirstNameLastName</td>
				</tr>
			}
		}
	}
</table>

@{
	var s = Model.Item1.SelectMany(t => t.Students).Distinct().OrderBy(s => s.LastNameFirstName);
	<table class="table">
		<thead>
		<tr>
			<td>Student</td>
			<td>Regional ID</td>
			<td>Team</td>
			<td>Time Slot</td>
		</tr>
		</thead>
		@foreach (var student in s)
		{
			<tr>
				<td>@student.FirstNameLastName</td>
				<td>@student.RegionalID</td>
				<td></td>
				<td></td>
			</tr>
			var teams = Model.Item1.Where(t => t.Students.Contains(student));
			foreach (var team in teams.OrderBy(t => t.RegionalTimeSlotObj))
			{
				<tr>
					<td></td>
					<td></td>
					<td>@team.Name
						@if (team.Captain == student)
						{
							<text>(Cpt.)</text>
						}
					</td>
					<td>@team.Event.RegionalNotes @team.RegionalTimeSlot</td>
				</tr>
			}
		}
	</table>
}
<p>No regional events: @string.Join(", ", @Model.Item2.Select(s=> s.FirstName))</p>

@functions
{
	public void GetOrderClass(int pick)
	{
		@Html.Raw(LabelHelper.GetOrderClass(pick))
	}

	private void GetTeamClass(Team team)
	{
		if (team.Event.RegionalEvent)
		{
			@Html.Raw("regional")
			;
		}
	}
}