@using Iceshrimp.Assets.PhosphorIcons
@using Iceshrimp.Frontend.Core.Miscellaneous
@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 GlobalComponentSvc             Global;
@if (_buttonType == ButtonType.Self) return;

<span class="follow-button">
    @switch (_buttonType)
        {
            case ButtonType.Follow:
                <button @onclick="Action" class="btn">
                    <span class="text">@Loc["Follow"]</span>
                    <Icon Name="Icons.Plus" class="icon"/>
                </button>
                break;
            case ButtonType.Unfollow:
                <button @onclick="Action" class="btn inverted">
                    <span class="text">@Loc["Unfollow"]</span>
                    <Icon Name="Icons.Minus" class="icon"/>
                </button>
                break;
            case ButtonType.FollowBack:
                <button @onclick="Action" class="btn">
                    <span class="text">@Loc["Follow back"]</span>
                    <Icon Name="Icons.Plus" class="icon"/>
                </button>
                break;
            case ButtonType.CancelRequest:
                <button @onclick="Action" class="btn inverted">
                    <span class="text">@Loc["Cancel request"]</span>
                    <Icon Name="Icons.X" class="icon"/>
                </button>
                break;
            case ButtonType.Blocking:
                <button @onclick="Action" class="btn">
                    <span class="text">@Loc["Unblock"]</span>
                    <Icon Name="Icons.Prohibit" class="icon"/>
                </button>
                break;
        }
</span>

@code {
    [Parameter] [EditorRequired] public required UserResponse        User        { get; set; }
    [Parameter] [EditorRequired] public required UserProfileResponse UserProfile { get; set; }
    private                                      ButtonType          _buttonType;

    protected override void OnInitialized()
    {
        ChooseButton();
    }

    private enum ButtonType
    {
        Follow,
        Unfollow,
        FollowBack,
        FollowLocked,
        CancelRequest,
        Blocking,
        Self
    }

    private async Task Action()
    {
        try
        {
            switch (_buttonType)
            {
                case ButtonType.Follow:
                case ButtonType.FollowBack:
                    await Follow();
                    break;
                case ButtonType.Unfollow or ButtonType.CancelRequest:
                    await Unfollow();
                    break;
                case ButtonType.Blocking:
                    await Unblock();
                    break;
            }
        }
        catch (ApiException e)
        {
            await Global.NoticeDialog?.Display(e.Response.Message ?? Loc["An unknown error occurred"], NoticeDialog.NoticeType.Error)!;
        }
    }

    private async Task Unfollow()
    {
        await Api.Users.UnfollowUserAsync(User.Id);
        UserProfile.Relations -= (int)Relations.Following;
        ChooseButton();
        StateHasChanged();
    }

    private async Task Follow()
    {
        await Api.Users.FollowUserAsync(User.Id);
        UserProfile.Relations += (int)Relations.Requested;
        ChooseButton();
        StateHasChanged();
        UserProfile = await Api.Users.GetUserProfileAsync(UserProfile.Id) ?? throw new Exception("How did it stop existing");
        ChooseButton();
        StateHasChanged();
    }

    private async Task Unblock()
    {
        await Api.Users.UnblockUserAsync(User.Id);
        UserProfile.Relations -= (int)Relations.Blocking;
        ChooseButton();
        StateHasChanged();
    }

    private void ChooseButton()
    {
        if (UserProfile.Relations == Relations.None)
        {
            _buttonType = ButtonType.Follow;
        }

        if (UserProfile.Relations.HasFlag(Relations.FollowedBy))
        {
            _buttonType = ButtonType.FollowBack;
        }

        if (UserProfile.Relations.HasFlag(Relations.Following))
        {
            _buttonType = ButtonType.Unfollow;
        }

        if (UserProfile.Relations.HasFlag(Relations.Requested))
        {
            _buttonType = ButtonType.CancelRequest;
        }

        if (UserProfile.Relations.HasFlag(Relations.Blocking))
        {
            _buttonType = ButtonType.Blocking;
        }

        if (UserProfile.Relations.HasFlag(Relations.Self))
        {
            _buttonType = ButtonType.Self;
        }
    }
}