@* ReSharper disable once RedundantUsingDirective *@
@using Iceshrimp.Frontend.Components.Note
@using Iceshrimp.Frontend.Core.Services
@using Iceshrimp.Frontend.Core.Services.NoteStore
@using Iceshrimp.Shared.Schemas.Web
@inject NavigationManager NavigationManager
@inject ApiService        Api
@inject RelatedStore      Store
@implements IDisposable

@if (_hidden == false)
{
    <div class="@(Depth > 0 ? "descendant" : "root-note")">
        <div class="note-container">
            @if (_indented)
            {
                <div class="note-indent">
                    <UserAvatar User="@Note.User"/>
                    @if (Note.Descendants?.Count > 0)
                    {
                        <Line></Line>
                    }
                </div>
            }
            <div class="note" @onclick="OpenNote">
                <Note Indented="_indented" NoteResponse="Note"/>
            </div>
        </div>
        @if (Note.Descendants != null)
        {
            <div class="@(Note.Descendants?.Count > 1 ? "replies" : "reply")">

                @* We are checking for null 3 lines up. *@
                @for (var i = 0; i < Note.Descendants!.Count; i++)
                {
                    var note = Note.Descendants[i];
                    @if (Note.Descendants?.Count > 1)
                    {
                        <div class="indented-reply">
                            <div class="reply-indent">
                                @if (i == Note.Descendants.Count - 1)
                                {
                                    <EndIcon/>
                                    <div class="line-container"></div>
                                }
                                else
                                {
                                    <BranchIcon/>
                                    <div class="line-container">
                                        <Line/>
                                    </div>
                                }
                            </div>
                            <RecursiveNote Note="note" Depth="Depth + 1" MaxDepth="MaxDepth"/>

                        </div>
                    }
                    else
                    {
                        <RecursiveNote Note="note" Depth="Depth + 1" MaxDepth="MaxDepth"/>
                    }
                }
            </div>
        }
    </div>
}

@code {
    [Parameter] [EditorRequired] public required NoteResponse Note     { get; set; }
    [Parameter] [EditorRequired] public required int          Depth    { get; set; }
    [Parameter] [EditorRequired] public required int          MaxDepth { get; set; }
    private                                      bool         _indented           = false;
    private                                      bool         _hidden             = false;

    protected override void OnInitialized()
    {
        Store.NoteChanged += OnNoteChanged;
        if (Depth > 0 || Note.Descendants?.Count > 0)
        {
            _indented = true;
        }
        // If the note is filtered, we don't want to render its tree, and hide this note.

        if (Note.Filtered is { Hide: true })
        {
            Note.Descendants = null;
            _hidden          = true;
        }
    }

    private void OnNoteChanged(object? _, NoteResponse note)
    {
        if (note.Id == Note.Id)
        {
            var __ = Refresh();
        }
    }

    private async Task Refresh()
    {
        if (Depth < MaxDepth)
        {
            var res = await Store.GetDescendantsAsync(Note.Id, MaxDepth - Depth, true);
            if (res == null) return;
            Note.Descendants = res.Select(p => p.Value).ToList();
            StateHasChanged();
        }
    }

    private void OpenNote()
    {
        NavigationManager.NavigateTo($"/notes/{Note.Id}");
    }

    public void Dispose()
    {
        Store.NoteChanged -= OnNoteChanged;
    }
}