using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Components;
using RpgRoller.Contracts;

namespace RpgRoller.Components.Pages.HomeControls;

[ExcludeFromCodeCoverage]
public partial class CampaignManagementPanel
{
    protected override void OnParametersSet()
    {
        if (string.IsNullOrWhiteSpace(CampaignState.Model.RulesetId) && Rulesets.Count > 0)
            CampaignState.Model.RulesetId = Rulesets[0].Id;
    }

    private void OpenCreateCampaignModal()
    {
        CampaignState.Model.Name = string.Empty;
        if (string.IsNullOrWhiteSpace(CampaignState.Model.RulesetId) && Rulesets.Count > 0)
            CampaignState.Model.RulesetId = Rulesets[0].Id;

        CampaignState.ResetValidation();
        ShowCreateCampaignModal = true;
    }

    private void CloseCreateCampaignModal()
    {
        CampaignState.ResetValidation();
        ShowCreateCampaignModal = false;
    }

    private async Task SubmitCreateCampaignAsync()
    {
        CampaignState.ResetValidation();

        if (string.IsNullOrWhiteSpace(CampaignState.Model.Name))
            CampaignState.Errors["name"] = "Campaign name is required.";

        if (string.IsNullOrWhiteSpace(CampaignState.Model.RulesetId))
            CampaignState.Errors["rulesetId"] = "Ruleset is required.";

        if (CampaignState.Errors.Count > 0)
        {
            CampaignState.ErrorMessage = "Resolve validation issues before submitting.";
            return;
        }

        IsCreatingCampaign = true;
        try
        {
            var campaign = await ApiClient.RequestAsync<CampaignSummary>("POST", "/api/campaigns", new CreateCampaignRequest(CampaignState.Model.Name.Trim(), CampaignState.Model.RulesetId));

            CampaignState.Model.Name = string.Empty;
            ShowCreateCampaignModal = false;
            await CampaignCreated.InvokeAsync(campaign.Id);
        }
        catch (ApiRequestException ex)
        {
            CampaignState.ErrorMessage = ex.Message;
        }
        finally
        {
            IsCreatingCampaign = false;
        }
    }

    [Inject]
    private RpgRollerApiClient ApiClient { get; set; } = null!;

    private FormState<CampaignFormModel> CampaignState { get; } = new();
    private bool IsCreatingCampaign { get; set; }
    private bool ShowCreateCampaignModal { get; set; }

    [Parameter]
    public IReadOnlyList<CampaignSummary> Campaigns { get; set; } = [];

    [Parameter]
    public Guid? SelectedCampaignId { get; set; }

    [Parameter]
    public CampaignRoster? SelectedCampaign { get; set; }

    [Parameter]
    public IReadOnlyList<RulesetDefinition> Rulesets { get; set; } = [];

    [Parameter]
    public bool IsMutating { get; set; }

    [Parameter]
    public Func<Guid, string> OwnerLabel { get; set; } = _ => string.Empty;

    [Parameter]
    public Func<CharacterSummary, bool> CanEditCharacter { get; set; } = _ => false;

    [Parameter]
    public Func<CharacterSummary, bool> CanDeleteCharacter { get; set; } = _ => false;

    [Parameter]
    public bool CanDeleteCampaign { get; set; }

    [Parameter]
    public EventCallback<ChangeEventArgs> CampaignSelectionChanged { get; set; }

    [Parameter]
    public EventCallback<Guid> CampaignCreated { get; set; }

    [Parameter]
    public EventCallback DeleteCampaignRequested { get; set; }

    [Parameter]
    public EventCallback CreateCharacterRequested { get; set; }

    [Parameter]
    public EventCallback<CharacterSummary> EditCharacterRequested { get; set; }

    [Parameter]
    public EventCallback<CharacterSummary> DeleteCharacterRequested { get; set; }
}