@page "/quizstart"
@using Blazored.LocalStorage;
@using FWLAZ_Web.Data
@using FWLAZ_Web.Objects
@inject ILocalStorageService localStorage;
@inject NavigationManager nav;
@inject SessionState CurrentSession;


<h2>@CurrentSession.LoadGame?.Title</h2>

@if (CurrentSession.LoadGame?.CurrentQuestion != null)
{
    <h3>Frage @(CurrentSession.LoadGame.QuestionAnswers?.Count + 1) / @(CurrentSession.LoadGame.QuestionAnswers?.Count + @CurrentSession.LoadGame.Questions?.Count)  </h3>
    <h4>@CurrentSession.LoadGame.CurrentQuestion?.Text [Nr. @CurrentSession.LoadGame.CurrentQuestion?.Number]</h4>
    <div class="mt-3">
        @if (CurrentSession.LoadGame?.CurrentQuestion != null)
        {
            @foreach (Answer aw in CurrentSession.LoadGame.CurrentQuestion.Answers)
            {
                <div class="mt-3">
                    @if (!ShowSolution)
                    {
                        <button class="btn form-control @(SelectedAnswers.Contains(aw) ? "btn-primary active" : "btn-outline-primary" )" type="button" @onclick="() => ToggleAnswer(aw)">@aw.Position. @aw.Text</button>
                    }
                    else
                    {
                        if (SelectedAnswers.Contains(aw) && aw.IsCorrect)
                        {
                            <button class="btn form-control btn-success" type="button">@aw.Position. @aw.Text</button>
                        }
                        else if (SelectedAnswers.Contains(aw) && !aw.IsCorrect)
                        {
                            <button class="btn form-control btn-danger" type="button">@aw.Position. @aw.Text</button>
                        }
                        else if (aw.IsCorrect)
                        {
                            <button class="btn form-control btn-outline-success" type="button">@aw.Position. @aw.Text</button>
                        }
                        else if (!aw.IsCorrect)
                        {
                            <button class="btn form-control btn-outline-danger" type="button">@aw.Position. @aw.Text</button>
                        }
                    }
                </div>
            }
        }
    </div>
    <div class="mt-3">
        <button class="btn btn-primary" type="button" @onclick="NextQuestion">Weiter</button>
    </div>
}
else
{
    <h3>Ergebnis</h3>
    <div class="w-25">

        <table class="table">
            <tbody>

            <tr class="table-primary">
                <td>Fragen Gesamt</td>
                <td>@(CurrentSession.LoadGame.QuestionAnswers.Count)</td>
            </tr>
            <tr class="table-success">
                <td>Korrekt beantwortet</td>
                <td>@(CurrentSession.LoadGame.QuestionAnswers.Where(qa => qa.IsCorrect).Count())</td>
            </tr>
            <tr class="table-danger">
                <td>Falsch beantwortet</td>
                <td>@(CurrentSession.LoadGame.QuestionAnswers.Where(qa => !qa.IsCorrect).Count())</td>
            </tr>
            </tbody>
        </table>
    </div>
}

@code {
    private string PrevURL = "/";

    private bool ShowSolution { get; set; }
    private List<Answer> SelectedAnswers { get; set; } = new();

    protected override async Task OnInitializedAsync()
    {
        if (CurrentSession.LoadGame == null)
        {
            nav.NavigateTo(PrevURL);
            return;
        }
        if (CurrentSession.LoadGame.QuestionAnswers == null) CurrentSession.LoadGame.QuestionAnswers = new();
        await InvokeAsync(StateHasChanged);
    }

    private void ToggleAnswer(Answer aw)
    {
        if (CurrentSession.LoadGame == null) return;
        if (CurrentSession.LoadGame.QuizIsMultipleChoice)
        {
            if (SelectedAnswers.Contains(aw))
            {
                SelectedAnswers.Remove(aw);
            }
            else
            {
                SelectedAnswers.Add(aw);
            }
        }
        else
        {
            SelectedAnswers.Clear();
            SelectedAnswers.Add(aw);
        }

    }
    private async Task NextQuestion(MouseEventArgs e)
    {
        if (ShowSolution == false)
        {
            ShowSolution = true;
        }
        else
        {
            ShowSolution = false;
            if (CurrentSession.LoadGame == null) return;
            if (CurrentSession.LoadGame.QuestionAnswers == null) CurrentSession.LoadGame.QuestionAnswers = new();
            if (CurrentSession.LoadGame.CurrentQuestion != null)
            {
                CurrentSession.LoadGame.QuestionAnswers.Add(new(CurrentSession.LoadGame.CurrentQuestion)
                    {
                        GivenAnswers = SelectedAnswers,
                    });
            }
            SelectedAnswers.Clear();
            CurrentSession.LoadGame.NextQuestion();
            await CurrentSession.SaveAsync();
        }

    }
}
