@using Core.Entities
@using Core.Utility
@model Tuple<EventDefinition[], Student[], EventStudentRankings[], Team[], AssignmentParameters>
@{
    ViewData["Title"] = "Home Page";
	var maxStudentPicks = Model.Item3.MaxBy(picks => picks.StudentRankings.Count).StudentRankings.Count;
	var parameters = Model.Item5;
	var unassignedEvents = Model.Item1.Where(e => Model.Item4.All(t => t.EventDefinition != e));
	
}

<table class="table-primary">
	<tr>
        <td colspan="8">
            Effort Limit:<strong>@parameters.EffortUpperBound</strong>
            &nbsp;&nbsp;Require Regionals:<strong>@parameters.RequireRegional</strong>
            &nbsp;&nbsp;Require On-Site Activity:<strong>@parameters.RequireOnSite</strong>
        </td>
	</tr>
</table>
<br/>
<table class="table-primary">
	<thead>
	<tr><td>Student</td><td>Level of Effort Total</td></tr>
	</thead>
	@foreach (var student in Model.Item2.OrderBy(s => s.FirstName))
	{
		var assignments
			= Model.Item4.Where(ea => ea.Students.Contains(student))
				.Select(ea => ea.EventDefinition)
				.Distinct()
				@* .OrderBy(e =>
				{

					var r = student.RankedEventPicks.IndexOf(e);
					r = r >= 0 ? r : 10;
					//r = r * (4 - e.LevelOfEffort.Value);
					return r;
				}) *@
        ;

		<tr>
			<td><strong>@student.FirstName</strong></td>
			<td>@assignments.Sum(a => a.LevelOfEffort)</td>

			@foreach (var evt in assignments)
			{
                @* var h = student.RankedEventPicks.IndexOf(evt) + 1; *@
				@* @{ class="GetOrderClass(h);" } *@
				<td class="">
					@evt.ShortName @Html.Partial("EffortStarsPartial", evt.LevelOfEffort)
					@{
						if (evt.EventFormat == 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><tr><td>&nbsp;</td></tr></table>

<table class="table-primary">
	<thead>
	<tr><td>Teams</td></tr>
	</thead>
	@foreach (var evt in Model.Item1.OrderByDescending(e => e.EventFormat is EventFormat.Team))
	{
		var assignments = Model.Item4.FirstOrDefault(i => i.EventDefinition == evt);
		@if (assignments == null)
		{
			continue;
		}

		<tr class="table-primary">
			<td class="table-primary"><strong>@evt.Name</strong></td>
			<td colspan="1">
				@Html.Partial("EffortStarsPartial", evt.LevelOfEffort)
				@if (evt.EventFormat is EventFormat.Individual)
				{
					<text>(ind)</text>
				}

				@if (evt.RegionalEvent)
				{
					<text>(reg)</text>
				}

				@if (evt.OnSiteActivity)
				{
					<text>(act)</text>
				}@evt.TeamSize</td>
			@*<td style="nowrap">@evt.MaxTeamCountState</td>*@
			@if (assignments != null)
			{
				foreach (var student in assignments.Students)
				{
					var h = student.RankedEventPicks.IndexOf(evt) + 1;
					<td class="@{ GetOrderClass(h); }">@student.FirstName</td>
				}
			}
			else
			{
				<td></td>
			}
		</tr>
	}

	<tr>
		@{
			var unassigned = string.Join(", ", unassignedEvents.Select(e => e.Name));
		}
		<td colspan="8">Unassigned EventDefinitions: @unassigned</td>
	</tr>
</table>

<table><tr><td></td></tr></table>

<table>
	<thead>
	<th>Name</th>
	<th>Grade</th>
	<th>TSA Year</th>
	<th>1</th>
	<th>2</th>
	<th>3</th>
	<th>4</th>
	<th>5</th>
	</thead>
	@foreach (var s in Model.Item2.OrderBy(n => n.FirstName))
	{
		<tr>
			<td>@s.FirstName</td>
			<td>@s.Grade.Ordinal()</td>
			<td>@s.TsaYear.Ordinal()</td>
			@for (var i = 0; i < 7; i++)
			{
				var h = i + 1;
				var evt = s.RankedEventPicks.Skip(i).FirstOrDefault();
				if (evt == null)
					continue;
                <td class="@{ GetOrderClass(h); }">
					@evt.ShortName @Html.Partial("EffortStarsPartial", evt.LevelOfEffort)
                    @if (evt.EventFormat == 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>
	<thead>
	<tr><td>EventDefinition</td><td>Level of Effort</td><td>Individual</td><td>Regional</td><td>On-site Activity</td><td>Team Size</td><td>Max Team Count</td></tr>
	</thead>
	@foreach (var evt in Model.Item1)
	{
		var esp = Model.Item3.FirstOrDefault(i => i.EventDefinition == evt);

        <tr>
	        <td>
		        @evt.Name
	        </td>

			<td>@Html.Partial("EffortStarsPartial", evt.LevelOfEffort)</td>
	        <td>@if (evt.EventFormat is EventFormat.Individual) { <text>ind</text> }</td>
	        <td>@if (evt.RegionalEvent) { <text>reg</text> }</td>
            <td>@if (evt.OnSiteActivity) { <text>act</text> }</td>
            <td style="nowrap">@evt.TeamSize</td>
            <td style="nowrap">@evt.MaxTeamCountState</td>

            @for (var i = 0; i < maxStudentPicks; i++)
            {
                var d = esp?.StudentRankings.Skip(i).FirstOrDefault();
                <td class="@{GetOrderClass(d?.Item2 ?? int.MaxValue);}">@d?.Item1.FirstName</td>
            }
        </tr>
	}
</table>


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