@using Iceshrimp.Assets.PhosphorIcons
@using Iceshrimp.Frontend.Core.Services
@using Iceshrimp.Frontend.Localization
@using Iceshrimp.Shared.Schemas.Web
@using Microsoft.Extensions.Localization
@inject IStringLocalizer<Localization> Loc;
@inject ApiService                     Api;
@inject NavigationManager              NavigationManager;


<div class="follow-request-card">
    <UserProfileCard User="FollowRequest.User" ShowBio="false"></UserProfileCard>
    <div class="buttons">
        @if (_followBack)
        {
            <button @onclick="AcceptAndFollowBack" class="button accept">
                <Icon Name="Icons.ArrowsLeftRight"/>@Loc["Accept and follow back"]
            </button>
        }
        <button @onclick="Accept" class="button accept">
            <Icon Name="Icons.Check"/>@Loc["Accept"]
        </button>
        <button @onclick="Reject" class="button decline">
            <Icon Name="Icons.X"/>@Loc["Decline"]
        </button>
    </div>
</div>

@code {
    [Parameter] [EditorRequired] public required FollowRequestResponse FollowRequest { get; set; }
    [Parameter] [EditorRequired] public required EventCallback<string> OnDelete      { get; set; }
    private                                      bool                  _followBack = false;

    protected override async Task OnInitializedAsync()
    {
        var profile                      = await Api.Users.GetUserProfileAsync(FollowRequest.User.Id);
        if (profile != null) _followBack = profile.Relations == Relations.RequestedBy;
    }

    private async Task Accept()
    {
        await Api.FollowRequests.AcceptFollowRequestAsync(FollowRequest.Id);
        await OnDelete.InvokeAsync(FollowRequest.Id);
    }

    private async Task AcceptAndFollowBack()
    {
        await Api.FollowRequests.AcceptFollowRequestAsync(FollowRequest.Id);
        await Api.Users.FollowUserAsync(FollowRequest.User.Id);
        await OnDelete.InvokeAsync(FollowRequest.Id);
    }

    private async Task Reject()
    {
        await Api.FollowRequests.RejectFollowRequestAsync(FollowRequest.Id);
        await OnDelete.InvokeAsync(FollowRequest.Id);
    }
}