@using Microsoft.EntityFrameworkCore
@using WebApp.Models
@page "/students/event-ranking"
@inject AppDbContext Context
@rendermode InteractiveServer

<PageTitle>Student Event Ranks - TSA Chapter Organizer</PageTitle>

<MudText Typo="Typo.h3"><MudIcon Icon="@AppIcons.EventRank"></MudIcon> Student Event Ranks</MudText>

@if (_students == null)
{
    <p><em>Loading...</em></p>
}
else
{

    <MudTable Items="_students" Hover="true" Breakpoint="Breakpoint.Sm" LoadingProgressColor="Color.Info">
        <HeaderContent>

            <MudTh>Name</MudTh>
            <MudTh>Grade</MudTh>
            <MudTh>TSA Year</MudTh>
            <MudTh></MudTh>
            <MudTh>Warnings</MudTh>
            @for (var i = 1; i <= 10; i++)
            {
                var ii = i;
                <MudTh>@AppIcons.GetOrdinal(ii)</MudTh>
            }
        </HeaderContent>
        <RowTemplate>

            <MudTd>@context.FirstName</MudTd>
            <MudTh>@context.Grade</MudTh>
            <MudTh>@context.TsaYear</MudTh>
            <MudTd><MudButton StartIcon="@Icons.Material.Filled.TableChart" Href="@($"students/event-ranking-edit/{context.Id}")">Edit</MudButton></MudTd>
            <MudTd>
                @if (!context.RankedEvents.Any(re => re.OnSiteActivity))
                {
                    <MudTooltip Text="No On-Site Activity">
                        <MudIcon Color="Color.Warning" Icon="@Icons.Material.Filled.LocalActivity"></MudIcon>
                    </MudTooltip>
                }
                @if (!context.RankedEvents.Any(re => re.RegionalEvent))
                {
                    <MudTooltip Text="No Regional Event">
                        <MudIcon Color="Color.Warning" Icon="@Icons.Material.Filled.PinDrop"></MudIcon>
                    </MudTooltip>
                }

                @if (context.RankedEvents.All(re => re.EventFormat != EventFormat.Individual))
                {
                    <MudTooltip Text="No Individual Event">
                        <MudIcon Color="Color.Warning" Icon="@Icons.Material.Filled.Person"></MudIcon>
                    </MudTooltip>
                }
            </MudTd>
            @for (var i = 1; i <= 10; i++)
            {
                var ii = i;
                var st = context.EventRankings.FirstOrDefault(e => e.Rank == i);
                <MudTd Class="@($"event-rank-{ii})")">
                    @if (st != null)
                    {
                        <span>@st.EventDefinition.ShortName&nbsp;
                        <EventAttributes EventDefinition="@st.EventDefinition"></EventAttributes>
                        </span>
                    }
                </MudTd>
            }
            
        </RowTemplate>
    </MudTable>
    <MudTable Items="_eventStudentRankings" Hover="true" Breakpoint="Breakpoint.Sm" LoadingProgressColor="Color.Info">
        <HeaderContent>
            <MudTh>Event</MudTh>
            <MudTh>Level of Effort</MudTh>
            <MudTh>Individual</MudTh>
            <MudTh>Regional</MudTh>
            <MudTh>On-site Activity</MudTh>
            <MudTh>Team Size</MudTh>
            @for (var i = 0; i < _maxEventStudentRankings; i++)
            {
                var i1 = i + 1;
                <MudTh>@i1</MudTh>
            }
        </HeaderContent>
        <RowTemplate>
            <MudTd>@context.Event.Name</MudTd>
            <MudTd>@context.Event.LevelOfEffort</MudTd>
            <MudTd>@if (context.Event.EventFormat ==  EventFormat.Individual) { <span>ⓘ</span> }</MudTd >
            <MudTd>@if (context.Event.RegionalEvent) { <span>ⓡ</span> }</MudTd >
            <MudTd>@if (context.Event.OnSiteActivity) { <span>ⓐ</span> }</MudTd >
            <MudTd>[@context.Event.MinTeamSize-@context.Event.MaxTeamSize]</MudTd >
            @for (var j = 0; j < _maxEventStudentRankings; j++)
            {
                var student = j < context.StudentRanking.Length ? context.StudentRanking[j] : null;
                var eventClass = student != null ? $"event-rank-{student.Item2}" : "";
                <MudTd Class="@eventClass">
                    @if (student != null)
                    {
                        @student.Item1.FirstName
                    }
                </MudTd>
            }
        </RowTemplate>
    </MudTable>
}
@code {
    private Student[]? _students;

    private class EventStudentRankings {
        public EventDefinition Event {get; set;  }
        public Tuple<Student,int> [] StudentRanking { get; set; }
    }

    private EventStudentRankings[] _eventStudentRankings;
    private int _maxEventStudentRankings;

    protected override async Task OnInitializedAsync()
    {
        _students = 
            await Context.Students
                .Include(e => e.EventRankings)
                .ThenInclude(e => e.EventDefinition)
                .OrderBy(e => e.FirstName).ToArrayAsync();

        _eventStudentRankings = 
            _students.SelectMany(s =>
                s.EventRankings, 
                (student, ranking) => new { e = ranking.EventDefinition, a = Tuple.Create(student, ranking.Rank) }
            )
            .GroupBy(e => e.e)
            .Select(e => 
                new EventStudentRankings
                {
                    Event = e.Key,
                    StudentRanking = e.Select(er => er.a).OrderBy(ser => ser.Item2).ThenByDescending(ser => ser.Item1.Grade + ser.Item1.TsaYear).ToArray()
                })
            .OrderBy(e => e.Event.Name)
            .ToArray();

        var events = await Context.Events.ToArrayAsync();
        var remainingEvents =
            events
                .Where(e => _eventStudentRankings.All(est => est.Event.Id != e.Id))
                .Select(e => new EventStudentRankings { Event = e, StudentRanking = Array.Empty<Tuple<Student, int>>() })
                .OrderBy(e => e.Event.Name)
                .ToArray();

        _eventStudentRankings = _eventStudentRankings.Concat(remainingEvents).ToArray();

        _maxEventStudentRankings = _eventStudentRankings.Max(esr => esr.StudentRanking.Length);
    }
}
