@using Core.Entities

@model Tuple<Student[]>

@{
	ViewData["Title"] = "Student Teams";
}

<table class="table-primary">
	<thead>
	<tr>
		<td>Student</td><td>Level of Effort Total</td>
	</tr>
	</thead>
	@foreach (var student in Model.Item1.OrderBy(s => s.FirstName))
	{
		var assignments
			= student.Teams.Where(ea => ea.Students.Contains(student))
				.Select(ea => ea.Event)
				.Distinct()
				.OrderBy(e =>
				{
					var r = student.RankedEventPicks.IndexOf(e);
					r = r >= 0 ? r : 10;
					//r = r * (4 - e.LevelOfEffort.Value);
					return r;
				}).ToList();

		<tr>
			<td>
				<strong>@student.FirstNameLastName</strong> @if(!string.IsNullOrEmpty(student.Officer  )) {<text>(@student.Officer)</text>}
			</td>
			<td>@assignments.Sum(a => a.LevelOfEffort)</td>

			@foreach (var evt in assignments)
			{
				var h = student.RankedEventPicks.IndexOf(evt) + 1;
				<td class="@{ GetOrderClass(h); }">
					@evt.ShortName @Html.Partial("EffortStarsPartial", evt.LevelOfEffort)
					@{
						if (evt.Format == EventFormat.Individual)
						{
							<sup class="activity">(ind)</sup>
						}
						if (evt.RegionalEvent)
						{
							<sup class="activity">(reg)</sup>
						}
						if (evt.OnSiteActivity)
						{
							<sup class="activity">(act)</sup>
						}
					}
				</td>
			}
		</tr>
	}
</table>	
@* 
<table class="table">
	<tr>
		<th>Student</th>
		<th>Team</th>
		<th>Teammates</th>
	</tr>
	@foreach (var student in Model.Item1.OrderBy(s => s.Name))
	{
		var teams = student.Teams;

		<tr class="table-primary">
			<td class="table-primary">
				<strong>@student.FirstNameLastName</strong>
				@if (!string.IsNullOrEmpty(student.Officer))
				{
					<text>(</text> @student.Officer <text>)</text>
				}
			</td>
		</tr>
		@foreach (var team in student.Teams)
		{
			<tr>
				<td></td>
				<td class="@{ GetTeamClass(team); }">@team.Name
					@{
						var ind = new List<string>();
						if (team.Captain == student)
						{
							<span>(Cpt .)</span>
						}
						if (!team.Event.InterviewOrPresentation)
						{
							ind.Add("a");
						}
						if (team.Event.Format is EventFormat.Individual)
						{
							ind.Add("i");
						}
						if (ind.Count > 0)
						{
							 <span class="activity">(@string.Join(",", ind))</span>
						}
					}
				</td>
				<td>
					@string.Join(", ", team.Students.Where(s => s != student).OrderByDescending(s => s.TsaYear + s.Grade).Select(s => s.FirstName))
				</td>
			</tr>
		}
	}
</table>
<p>
	(a) denotes an event that has activity other than interview or presentation at state
	<br />
	(i) denotes an individual event
</p> *@


<table class="table-primary">
	<thead>
		<tr>
			<td>Student</td>
		</tr>
	</thead>
	@foreach (var student in Model.Item1.OrderBy(s => s.LastNameFirstName))
	{
		var assignments
		= student.Teams.Where(ea => ea.Students.Contains(student))
		.Select(ea => ea.Event)
		.Distinct()
		.OrderBy(e =>
		{
			var r = student.RankedEventPicks.IndexOf(e);
			r = r >= 0 ? r : 10;
			//r = r * (4 - e.LevelOfEffort.Value);
			return r;
		}).ToList();

		<tr>
			<td>
				<strong>@student.FirstName</strong>
			</td>

			@foreach (var evt in assignments)
			{
				var h = student.RankedEventPicks.IndexOf(evt) + 1;
				<td>
					@evt.Name 
					@{
						if (evt.Format == EventFormat.Individual)
						{
							<sup class="activity">(individual)</sup>
						}
						if (evt.RegionalEvent)
						{
							<sup class="activity">(regional)</sup>
						}
					}
				</td>
			}
		</tr>
	}
</table>

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

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