@page "/settings/quizzes/questions/{id}"
@using FWLAZ_Web.Data
@using FWLAZ_Web.Objects
@using Microsoft.EntityFrameworkCore;
@inject IDbContextFactory<LocalDbContext> DbFactory;
@inject NavigationManager nav;


<h1>Questions for '@SelectedItem.Name'</h1>
<button type="button" @onclick="SaveQuiz" class="btn btn-primary">Save</button>

<div class="border border-2 rounded p-3 w-100 m-3">
    <h3>New Question</h3>
    <h3>Questiongroups</h3>
    @foreach (QuestionGroupSelect qg in questionGroups)
    {
        <div class="form-check form-check-inline">
            <input class="form-check-input" type="checkbox" @bind="qg.Checked" />
            <label class="form-check-label">@qg.questionGroup.Name</label>
        </div>
    }
    <div class="row">
        <div class="col-sm-1">
            <input class="form-control col-sm" type="number" @bind="NewQuestion.Number" />
        </div>
        <div class="col-sm">
            <input class="form-control" type="text" @ref="txtNewQuestionText" @bind="NewQuestion.Text" />
        </div>
    </div>

    <h3>Answers</h3>
    @foreach (Answer aw in NewQuestion.Answers)
    {
        <div class="input-group mb-3">
            <div class="input-group-text">
@*                 @if (SelectedItem.IsMultipleChoice)
                { *@
                    <input class="form-check-input mt-0" type="checkbox" @bind="aw.IsCorrect" @onkeyup="AddQuestion_Enter" />
@*                 }
                else
                {
                    <input class="form-check-input mt-0" type="radio" @onchange="() => SetSingleChoiceAnswer(NewQuestion, aw)" name="NewQuestion_Answers" @onkeyup="AddQuestion_Enter" />
                } *@
            </div>
            <input class="form-control col-sm" style="max-width:70px;" type="text" @bind="aw.Position" @onkeyup="AddQuestion_Enter" />
            <input class="form-control" type="text" @bind="aw.Text" @onkeyup="AddQuestion_Enter" />
            <button class="btn btn-outline-secondary" type="button" @onclick="() => RemoveAnswer(NewQuestion, aw)">Remove</button>
        </div>
    }
    <button type="button" @onclick="AddQuestion" class="btn btn-primary">Add Question</button>
    <button type="button" @onclick="() => AddAnswerBox(NewQuestion)" class="btn btn-secondary">More Answers</button>
</div>

<div class="border border-2 rounded p-3 w-100 m-3">
    <h3>Existing Questions</h3>
    <div class="accordion" id="accordion_questions">
        @{
            int tabcount = 1;
        }
        @foreach (Question question in SelectedItem.Questions)
        {
            <div class="accordion-item">
                <h2 class="accordion-header">
                    <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse_@tabcount" aria-expanded="false" aria-controls="collapse_@tabcount">
                        @question.Number. @question.Text
                    </button>
                </h2>
                <div id="collapse_@tabcount" class="accordion-collapse collapse" data-bs-parent="#accordion_questions">
                    <div class="accordion-body">
                        <div class="row">
                            <div class="col-sm-1">

                                <input class="form-control col-sm" type="number" @bind="question.Number" />
                            </div>
                            <div class="col-sm">
                                <input class="form-control" type="text" @bind="question.Text" />
                            </div>
                        </div>
                        <h3>Question groups</h3>
                        <div class="d-flex gap-3">
                            <div class="border border-2 rounded p-3 w-100">
                                <h4>Added</h4>
                                <ul class="list-group">
                                    @foreach (QuestionGroupSelect qgs in questionGroups.Where(qgs => question.QuestionGroups.Any(qg => qg.Id == qgs.questionGroup.Id)))
                                    {
                                        <li class="list-group-item list-group-item-action align-middle z-0" @onclick="args => RemoveQuestionGroup(question, qgs.questionGroup)">
                                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-dash-circle text-danger" viewBox="0 0 16 16">
                                                <path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14m0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16" />
                                                <path d="M4 8a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7A.5.5 0 0 1 4 8" />
                                            </svg>
                                            <label class="form-check-label stretched-link">@qgs.questionGroup.Name</label>
                                        </li>
                                    }
                                </ul>
                            </div>
                            <div class="border border-2 rounded p-3 w-100">
                                <h4>Remaining</h4>
                                <ul class="list-group">
                                    @foreach (QuestionGroupSelect qgs in questionGroups.Where(qgs => question.QuestionGroups.Any(qg => qg.Id == qgs.questionGroup.Id) == false))
                                    {
                                        <li class="list-group-item list-group-item-action align-middle z-0" @onclick="args => AddQuestionGroup(question, qgs.questionGroup)">
                                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-plus-circle text-success" viewBox="0 0 16 16">
                                                <path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14m0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16" />
                                                <path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4" />
                                            </svg>
                                            <label class="form-check-label stretched-link">@qgs.questionGroup.Name</label>
                                        </li>
                                    }
                                </ul>
                            </div>
                        </div>
                        <h3>Answers</h3>
                        @foreach (Answer aw in question.Answers)
                        {
                            <div class="input-group mb-3">
                                <div class="input-group-text">
                                    <input class="form-check-input mt-0" type="checkbox" @bind="aw.IsCorrect" />
                                </div>
                                <input class="form-control col-sm" style="max-width:70px;" type="text" @bind="aw.Position" />
                                <input class="form-control" type="text" @bind="aw.Text" />
                                <button class="btn btn-outline-secondary" type="button" @onclick="() => RemoveAnswer(question, aw)">Remove</button>
                            </div>
                        }
                        <button @onclick="() => AddAnswerBox(question)" class="btn btn-primary">More Answers</button>
                        <button @onclick="() => RemoveQuestion(question)" class="btn btn-danger">Delete question</button>
                    </div>
                </div>
            </div>
            tabcount++;
        }
    </div>
</div>


@code {
    [Parameter]
    public string Id { get; set; } = null!;

    ElementReference txtNewQuestionText;

    public Quiz SelectedItem { get; set; } = null!;
    private Question NewQuestion = null!;

    private int DefaultAnswerCount = 3;

    private List<QuestionGroupSelect> questionGroups = new();

    private string PrevURL = "/settings/quizzes";

    private LocalDbContext? DbContext;

    protected override async Task OnInitializedAsync()
    {
        DbContext ??= await DbFactory.CreateDbContextAsync();
        if (DbContext == null) return;
        SelectedItem = DbContext.Quiz.Include(q => q.Questiongroups).ThenInclude(qg => qg.Questions).ThenInclude(qu => qu.Answers).Single(q => q.Id == Convert.ToInt32(Id));
        foreach (QuestionGroup qg in SelectedItem.Questiongroups)
        {
            questionGroups.Add(new(qg));
        }
        PrepareNewQuestion();
    }

    private void PrepareNewQuestion()
    {
        NewQuestion = new();
        if (SelectedItem.Questions.Count > 0)
        {
            NewQuestion.Number = SelectedItem.Questions.Max(q => q.Number) + 1;
        }
        else
        {
            NewQuestion.Number = 1;
        }
        NewQuestion.Quiz = SelectedItem;
        for (int i = 0; i < DefaultAnswerCount; i++)
        {
            AddAnswerBox(NewQuestion);
        }
    }

    private void SetSingleChoiceAnswer(Question question, Answer answer)
    {
        question.Answers.ForEach(aw => aw.IsCorrect = false);
        answer.IsCorrect = true;
    }

    private async Task AddQuestion_Enter(KeyboardEventArgs e)
    {
        if (e.Code == "Enter" || e.Code == "NumpadEnter") await AddQuestion(new MouseEventArgs());
    }

    private async Task AddQuestion(MouseEventArgs e)
    {

        NewQuestion.Answers.RemoveAll(aw => aw.Text == null || aw.Text.Trim().Length == 0);
        if (NewQuestion.Answers.Count == 0 ||
            NewQuestion.Text == null ||
            NewQuestion.Text.Trim().Length == 0 ||
            NewQuestion.Answers.Count(aw => aw.IsCorrect) == 0 ||
            questionGroups.Count(qg => qg.Checked == true) == 0
        ) return;

        foreach (QuestionGroupSelect qgs in questionGroups.Where(qg => qg.Checked))
        {
            NewQuestion.QuestionGroups.Add(qgs.questionGroup);
        }
        DefaultAnswerCount = NewQuestion.Answers.Count;
        SelectedItem.Questions.Add(NewQuestion);

        PrepareNewQuestion();
        await txtNewQuestionText.FocusAsync();
    }

    private void RemoveAnswer(Question question, Answer answer)
    {
        question.Answers.Remove(answer);
    }

    private void AddAnswerBox(Question question)
    {
        char CurLetter = 'a';
        for (int i = 0; i < question.Answers.Count; i++)
        {
            CurLetter = NextChar(CurLetter);
        }
        question.Answers.Add(new() { Position = CurLetter.ToString() });
    }

    private void RemoveQuestion(Question question)
    {
        SelectedItem.Questions.Remove(question);
    }

    private void RemoveQuestionGroup(Question question, QuestionGroup questionGroup)
    {
        question.QuestionGroups.Remove(questionGroup);
    }

    private void AddQuestionGroup(Question question, QuestionGroup questionGroup)
    {
        if (question.QuestionGroups.Any(qg => qg.Id == questionGroup.Id)) return;
        question.QuestionGroups.Add(questionGroup);
    }

    private char NextChar(char StartLetter)
    {
        if (StartLetter == 'z')
        {
            return 'a';
        }
        else if (StartLetter == 'Z')
        {
            return 'A';
        }
        else
        {
            return (char)(((int)StartLetter) + 1);
        }
    }

    private async Task SaveQuiz(MouseEventArgs e)
    {
        foreach (Question question in SelectedItem.Questions)
        {
            question.Answers.RemoveAll(aw => aw.Text == null || aw.Text.Trim().Length == 0);
        }

        DbContext ??= await DbFactory.CreateDbContextAsync();
        if (DbContext == null) return;
        DbContext.Quiz.Update(SelectedItem);
        await DbContext.SaveChangesAsync();
        nav.NavigateTo(PrevURL);
    }

}
