@using Microsoft.EntityFrameworkCore
@using WebApp.Models
@page "/teams/printout"
@inject IConfiguration Configuration
@inject AppDbContext Context

<PageTitle>@Configuration["ChapterSettings:Shortname"] TSA Teams @Configuration["ChapterSettings:CompetitionYear"]</PageTitle>

<MudText Typo="Typo.h3">@Configuration["ChapterSettings:Shortname"] TSA Teams @Configuration["ChapterSettings:CompetitionYear"]</MudText>
<MudText Typo="Typo.h5" Class="mb-4">Yearly theme: Unity Through Community</MudText>

<Legend></Legend>
@if (_teams == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <MudContainer Class="mt-3 mb-1 nobrk">

        <MudTable Items="_teams">
            <HeaderContent>
                <MudTh>Team</MudTh>
                <MudTh></MudTh>
                @for (var i = 0; i <= _maxTeamSize; i++)
                {
                    <MudTh></MudTh>
                }
            </HeaderContent>
            <RowTemplate>

                <MudTd>
                    @context.ToString()
                </MudTd>
                <MudTd>
                    <EventAttributes EventDefinition="context.Event"></EventAttributes>
                </MudTd>

                @{
                    var students 
                        = context.Students
                            .OrderByDescending(s => s == context.Captain)
                            .ThenBy(s => s.EventRankings.Find(e => e.EventDefinition == context.Event)?.Rank ?? int.MaxValue)
                            .ThenByDescending(e => e.Grade)
                            .ThenBy(e => e.FirstName)
                            .ToArray();
                }
                
                @for (var i = 0; i <= _maxTeamSize; i++)
                {
                    var student = i < students.Length ? students[i] : null;
                    if (student != null)
                    {
                        var rank = student.EventRankings
                            .Find(e => e.EventDefinition == context.Event)?.Rank ?? int.MaxValue;

                        <MudTd Class="@(EventRankClass(rank))">
                            @student.Name @if(context?.Captain == student) {<span> (Cpt)</span>}
                        </MudTd>
                    }
                    else
                    {
                        <MudTd></MudTd>
                    }
                }
                <MudTd>
                    @if (context.Event.EventFormat == EventFormat.Team)
                    {
                        @if (context.Students.Count < context.Event.MinTeamSize)
                        {
                            <span>Min Team Size: @context.Event.MinTeamSize</span>
                        }

                        @if (context.Students.Count > context.Event.MaxTeamSize)
                        {
                            <span>Max Team Size: @context.Event.MaxTeamSize</span>
                        }
                    }
                    else if (context.Event.EventFormat == EventFormat.Individual
                             && context.Students.Count > context.Event.ChapterEligibilityCountState)
                    {
                        <span>Max Team Count State: @context.Event.ChapterEligibilityCountState</span>
                    }
                </MudTd>
            </RowTemplate>
        </MudTable>
        
    </MudContainer>

    <MudContainer Class="mt-3 mb-1 nobrk pagebreak">

        <MudTable Items="_students">
            <HeaderContent>
                <MudTh>Student</MudTh>
                <MudTh>Effort</MudTh>
                @for (var i = 0; i <= 5; i++)
                {
                    <MudTh></MudTh>
                }
                <MudTh></MudTh>
            </HeaderContent>
            <RowTemplate>

                <MudTd>@context.Name</MudTd>
                <MudTd>@context.Teams.Sum(e => e.Event.LevelOfEffort)</MudTd>
                @{
                    var teams = context.Teams
                        .OrderBy(e => 
                            context.EventRankings.Find(ser => ser.EventDefinition == e.Event)?.Rank ?? int.MaxValue
                        ).ToArray(); 
                }
                @for (var i = 0; i <= 5; i++)
                {
                    var team = i < teams.Length ? teams[i] : null;

                    @if (team != null)
                    {
                        var rank = context.EventRankings
                            .Find(e => e.EventDefinition == team.Event)?.Rank ?? int.MaxValue;
                        <MudTh Class="@(EventRankClass(rank))">
                            @team.ToString()
                           <EventAttributes EventDefinition="team.Event"></EventAttributes>

                            @if (rank == int.MaxValue)
                            {
                                <span>❔</span>
                            }
                        </MudTh>
                    }
                    else
                    {
                        <MudTh></MudTh>
                    }

                }
                <MudTd>
                    @if (!context.Teams.Select(e => e.Event).Any(re => re.OnSiteActivity))
                    {
                        <span>No On-Site Activity</span>
                    }
                    @if (!context.Teams.Select(e => e.Event).Any(re => re.RegionalEvent))
                    {
                        <span>No Regional Event</span>
                    }
                </MudTd>
            </RowTemplate>
        </MudTable>
        
    </MudContainer>


    <MudContainer Class="mt-3 mb-1 nobrk pagebreak">

        <MudTable Items="_students">
            <HeaderContent>
                <MudTh>Student</MudTh>
                <MudTh>Grade, TSA Year</MudTh>
                <MudTh>Level of Effort</MudTh>
            </HeaderContent>
            <RowTemplate>

                <MudTd>@context.Name</MudTd>
                <MudTd>@AppIcons.GetOrdinal(context.Grade), @context.TsaYear</MudTd>
                <MudTd>@context.Teams.Sum(e => e.Event.LevelOfEffort)
                    @if (!context.Teams.Select(e => e.Event).Any(re => re.OnSiteActivity))
                    {
                        <span>No On-Site Activity</span>
                    }
                    @if (!context.Teams.Select(e => e.Event).Any(re => re.RegionalEvent))
                    {
                        <span>No Regional Event</span>
                    }
                </MudTd>
            </RowTemplate>
            <ChildRowContent>
                <MudTr>
                    <MudTable Items="@context.Teams.OrderBy(e =>
                                         context.EventRankings.Find(ser => ser.EventDefinition == e.Event)?.Rank ?? int.MaxValue
                                     )" Context="team">
                        <RowTemplate>
                            @{ var rank = context.EventRankings
                                   .Find(e => e.EventDefinition == team.Event)?.Rank ?? int.MaxValue; }
                            <MudTd Class="@(EventRankClass(rank))">
                                @team.ToString()
                                @AppIcons.EventEffort(team.Event)
                                @AppIcons.EventAttributes(team.Event)

                                @if (rank == int.MaxValue)
                                {
                                    <span>❔</span>
                                }
                            </MudTd>
                            <MudTd>@string.Join(", ", team.Students.Where(e => e != context).Select(e => e.FirstName))</MudTd>
                            <MudTd></MudTd>
                        </RowTemplate>
                    </MudTable>
                </MudTr>
            </ChildRowContent>
        </MudTable>

    </MudContainer>
}
@code {
    private Team[]? _teams;
    private int _maxTeamSize;
    private Student[]? _students;
    private bool _rankColorEnabled;

    private string EventRankClass(int rank)
    {
        if (!_rankColorEnabled)
            return "";
        return "event-rank-" + rank;
    }

    protected override async Task OnInitializedAsync()
    {
        _teams
                = await Context.Teams
                    .Include(e => e.Event)
                    .Include(e => e.Students)
                    .OrderBy(e => e.Event.Name)
                    .ThenBy(e => e.Identifier)
                    .ToArrayAsync();

        _maxTeamSize = _teams.Max(t => t.Students.Count);
        _students = 
            await Context.Students
                .Include(e => e.Teams)
                .ThenInclude(e => e.Captain)
                .Include(e => e.EventRankings)
                .ThenInclude(e => e.EventDefinition)
                .OrderBy(e => e.FirstName).ToArrayAsync();
    }
}
