@page "/settings/quizzes/edit"
@page "/settings/quizzes/edit/{id}"
@using FWLAZ_Web.Data
@using Microsoft.AspNetCore.Authorization
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<LocalDbContext> DbFactory;
@inject NavigationManager nav;
@attribute [Authorize]
<h3>QuizEdit</h3>

<div class="d-flex gap-3">
    <div class="border border-2 rounded p-3 w-100">
        <div class="mb-3">
            <label class="form-label" for="name">Name</label>
            <input id="name" name="name" type="text" class="form-control" @bind="@SelectedItem.Name" />
        </div>
        <div class="mb-3 form-check">
            <input class="form-check-input" type="checkbox" @bind="@SelectedItem.IsMultipleChoice" id="chkMultipleChoice"/>
            <label class="form-check-label"  for="chkMultipleChoice">Is Multiple-Choice</label>
        </div>
        <h3>Question Groups</h3>
        <div class="input-group m-3">
            <input id="qgnew" name="qgnew" type="text" class="form-control" @bind="@NewQuestionGroup" />
            <button @onclick="AddQuestionGroup" class="btn btn-primary">Add</button>
        </div>
        <div class="m-3">
            @foreach (QuestionGroup qg in SelectedItem.Questiongroups)
            {
                <div class="row">
                    <div class="col-sm-1">
                        <input class="form-control col-sm" type="number" @bind="qg.Order" />
                    </div>
                    <div class="col-sm">
                        <input class="form-control" type="text" @bind="qg.Name" />
                    </div>
                </div>
            }
        </div>
        <button @onclick="OnSubmit" class="btn btn-primary">Save</button>
        <button @onclick="@(() => nav.NavigateTo(PrevURL))" class="btn btn-secondary">Cancel</button>
    </div>
</div>

@code {
    [Parameter]
    public string? id { get; set; }

    private LocalDbContext? DbContext;
    public Quiz SelectedItem = null!;

    private string? NewQuestionGroup;

    private string PrevURL = "/settings/quizzes";

    protected override async Task OnInitializedAsync()
    {
        if (id != null)
        {
            DbContext ??= await DbFactory.CreateDbContextAsync();
            if (DbContext != null) SelectedItem = DbContext.Quiz.Include(qu => qu.Questiongroups).Single(qu => qu.Id == Convert.ToInt32(id));
        }
        SelectedItem ??= new();

        //await InvokeAsync(StateHasChanged);
    }

    private async Task OnSubmit(EventArgs e)
    {
        DbContext ??= await DbFactory.CreateDbContextAsync();
        if (SelectedItem == null || DbContext == null)
        {
            // nav.NavigateTo(PrevURL);
            return;
        }

        if (id != null)
        {
            DbContext.Quiz.Update(SelectedItem);
        }
        else
        {
            DbContext.Quiz.Add(SelectedItem);
        }
        await DbContext.SaveChangesAsync();
        nav.NavigateTo(PrevURL);
    }

    private void AddQuestionGroup(MouseEventArgs e)
    {
        if (NewQuestionGroup != null && NewQuestionGroup.Trim().Length > 0)
        {
            SelectedItem.Questiongroups.Add(new QuestionGroup(NewQuestionGroup));
            NewQuestionGroup = null;
        }
    }
}
