@page "/events/edit"
@using Microsoft.EntityFrameworkCore
@inject AppDbContext context
@inject NavigationManager NavigationManager

<PageTitle>Edit Event - TSA Chapter Organizer</PageTitle>

<MudText Typo="Typo.h3">Edit</MudText>
<MudText Typo="Typo.h4">Event</MudText>
<MudDivider />


<EditForm Model="EventDefinition" OnValidSubmit="OnValidSubmit" Enhance>
    <DataAnnotationsValidator />
    <MudGrid>
        <MudItem xs="12" sm="7">
            <MudPaper Class="pa-4">
                <MudTextField T="string" Label="Event Name" @bind-Value="EventDefinition.Name" For="@(() => EventDefinition.Name)"></MudTextField>
                <MudTextField T="string" Label="Short Name" @bind-Value="EventDefinition.ShortName" For="@(() => EventDefinition.ShortName)"></MudTextField>

                <label for="@EventDefinition.EventFormat" class="form-label">Format:</label>
                <MudRadioGroup T="EventFormat" @bind-Value="@EventDefinition.EventFormat">
                    @* <MudRadio T="EventFormat" Value="EventFormat.Team">Team</MudRadio>
                    <MudRadio T="EventFormat" Value="EventFormat.Individual">Individual</MudRadio> *@
                    @foreach (EventFormat format in Enum.GetValues(typeof(EventFormat)))
                    {
                        <MudRadio T="EventFormat" value="@format">@(format.ToString())</MudRadio>
                    }
                </MudRadioGroup>
                <ValidationMessage For="() => EventDefinition.EventFormat" class="text-danger" />
                <MudTextField T="string" Label="Theme" AutoGrow="true" @bind-Value="EventDefinition.Description" For="@(() => EventDefinition.Description)"></MudTextField>
                <MudTextField T="string" Label="Theme" AutoGrow="true" @bind-Value="EventDefinition.Theme" For="@(() => EventDefinition.Theme)"></MudTextField>
                <MudTextField T="string" Label="Documentation" @bind-Value="EventDefinition.Documentation" For="@(() => EventDefinition.Documentation)"></MudTextField>
                <MudNumericField T="int?" Label="Level of Effort" @bind-Value="EventDefinition.LevelOfEffort" For="@(() => EventDefinition.LevelOfEffort)"></MudNumericField>
                <MudDivider></MudDivider>
                <MudTextField T="string" Label="Nationals Eligibility" @bind-Value="EventDefinition.Eligibility" For="@(() => EventDefinition.Eligibility)"></MudTextField>
                <MudNumericField T="int" Label="Minimum Team Size" @bind-Value="EventDefinition.MinTeamSize" For="@(() => EventDefinition.MinTeamSize)"></MudNumericField>
                <MudNumericField T="int" Label="Maxiumum Team Size" @bind-Value="EventDefinition.MaxTeamSize" For="@(() => EventDefinition.MaxTeamSize)"></MudNumericField>
                <MudNumericField T="int" Label="Team Count at Regionals" @bind-Value="EventDefinition.ChapterEligibilityCountRegionals" For="@(() => EventDefinition.ChapterEligibilityCountRegionals)"></MudNumericField>
                <MudNumericField T="int" Label="Team Count at State" @bind-Value="EventDefinition.ChapterEligibilityCountState" For="@(() => EventDefinition.ChapterEligibilityCountState)"></MudNumericField>
                <MudDivider></MudDivider>
                <MudTextField T="string" Label="Semifinalist Activity" @bind-Value="EventDefinition.SemifinalistActivity" For="@(() => EventDefinition.SemifinalistActivity)"></MudTextField>
                <MudCheckBox T="bool" Label="On Site Activity" @bind-Value="EventDefinition.OnSiteActivity" For="@(() => EventDefinition.OnSiteActivity)"></MudCheckBox>
                <MudCheckBox T="bool" Label="Requires Presubmission" @bind-Value="EventDefinition.Presubmission" For="@(() => EventDefinition.Presubmission)"></MudCheckBox>
            </MudPaper>
        </MudItem>
    </MudGrid>
    <MudButton StartIcon="@Icons.Material.Filled.ArrowBack" Href="events">Back</MudButton>
    <MudButton ButtonType="ButtonType.Submit" StartIcon="@Icons.Material.Filled.Save">Save</MudButton>
</EditForm>


@code {
    [SupplyParameterFromQuery]
    private int Id { get; set; }

    [SupplyParameterFromForm]
    private EventDefinition? EventDefinition { get; set; }

    protected override async Task OnInitializedAsync()
    {
        EventDefinition ??= await context.Events.FirstOrDefaultAsync(m => m.Id == Id);

        if (EventDefinition is null)
        {
            NavigationManager.NavigateTo("notfound");
        }
    }

    // To protect from overposting attacks, enable the specific properties you want to bind to.
    // For more information, see https://learn.microsoft.com/aspnet/core/blazor/forms/#mitigate-overposting-attacks.
    private void OnValidSubmit()
    {
        context.Attach(EventDefinition!).State = EntityState.Modified;

        try
        {
            context.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!EventDefinitionExists(EventDefinition!.Id))
            {
                NavigationManager.NavigateTo("notfound");
            }
            else
            {
                throw;
            }
        }

        NavigationManager.NavigateTo("/events");
    }

    private bool EventDefinitionExists(int id)
    {
        return context.Events.Any(e => e.Id == id);
    }
}
