@using Microsoft.AspNetCore.Components
@using RpgRoller.Components.Pages.HomeControls

<main class="play-screen @(Workspace.State.MobilePanel == "log" ? "mobile-log" : "mobile-character")">
    <CharacterPanel
        IsCampaignDataLoading="@IsCampaignDataLoading"
        SelectedCampaign="Workspace.State.PlaySelectedCampaign"
        SelectedCharacterId="Workspace.State.PlaySelectedCharacterId"
        SelectedCharacter="Workspace.State.PlaySelectedCharacter"
        IsMutating="Workspace.State.IsMutating"
        SelectedCharacterSkills="Workspace.State.PlaySelectedCharacterSkills"
        SelectedCharacterSkillGroups="Workspace.State.PlaySelectedCharacterSkillGroups"
        SelectedCampaignRulesetId="@(Workspace.State.PlaySelectedCampaign?.RulesetId ?? string.Empty)"
        RollVisibility="Workspace.State.RollVisibility"
        RollVisibilityChanged="OnRollVisibilityChangedAsync"
        OwnerLabel="Workspace.State.OwnerLabel"
        SkillDefinitionLabel="Workspace.State.SkillDefinitionLabel"
        CanEditCharacter="Workspace.Campaigns.CanEditCharacter"
        CanEditSkill="Workspace.Play.CanEditSkill"
        CharacterSelected="Workspace.Play.SelectCharacterAsync"
        EditCharacterRequested="Workspace.Campaigns.OpenEditCharacterModal"
        SkillCreated="Workspace.Play.OnSkillCreatedAsync"
        SkillUpdated="Workspace.Play.OnSkillUpdatedAsync"
        SkillGroupCreated="Workspace.Play.OnSkillGroupCreatedAsync"
        SkillGroupUpdated="Workspace.Play.OnSkillGroupUpdatedAsync"
        SkillDeleted="Workspace.Play.OnSkillDeletedAsync"
        SkillGroupDeleted="Workspace.Play.OnSkillGroupDeletedAsync"
        ErrorOccurred="Workspace.Play.OnCharacterPanelErrorAsync"
        RollRequested="Workspace.Play.RollSkillAsync"/>

    <CampaignLogPanel
        IsCampaignDataLoading="@IsCampaignDataLoading"
        CampaignLog="Workspace.State.PlayVisibleCampaignLog"
        ExpandedRollId="Workspace.State.ExpandedCampaignLogRollId"
        FreshRollId="Workspace.State.FreshCampaignLogRollId"
        SelectedCharacterId="Workspace.State.PlaySelectedCharacterId"
        SelectedCharacterName="@(Workspace.State.PlaySelectedCharacter?.Name)"
        SelectedCampaignRulesetId="@(Workspace.State.PlaySelectedCampaign?.RulesetId ?? string.Empty)"
        RollVisibility="Workspace.State.RollVisibility"
        ResolveRollVisibility="ResolveRollVisibility"
        IsMutating="Workspace.State.IsMutating"
        ToggleRollDetailRequested="Workspace.Play.ToggleRollDetailAsync"
        ResolveRollDetail="Workspace.Play.ResolveRollDetail"
        IsRollDetailLoading="Workspace.Play.IsRollDetailLoading"
        GetRollDetailError="Workspace.Play.GetRollDetailError"
        CustomRollCreated="Workspace.Play.OnCustomRollCreatedAsync"
        ErrorOccurred="Workspace.Play.OnCampaignLogPanelErrorAsync"/>
</main>
<nav class="mobile-bottom-nav" aria-label="Play panel selector">
    <button type="button" class="switch @(Workspace.State.MobilePanel == "character" ? "active" : string.Empty)"
            @onclick='() => Workspace.Scope.SetMobilePanelAsync("character")'>
        Character
    </button>
    <button type="button" class="switch @(Workspace.State.MobilePanel == "log" ? "active" : string.Empty)"
            @onclick='() => Workspace.Scope.SetMobilePanelAsync("log")'>
        Log
    </button>
</nav>

<CharacterManagementModals Workspace="Workspace"/>

<RolemasterSkillRollModal
    Visible="Workspace.State.ShowRolemasterSkillRollModal"
    SkillName="@(Workspace.State.PendingRolemasterSkillRoll?.Name ?? string.Empty)"
    Expression="@(Workspace.State.PendingRolemasterSkillRoll?.DiceRollDefinition ?? string.Empty)"
    ModifierText="@Workspace.State.PendingRolemasterSituationalModifier"
    ModifierTextChanged="@(text => Workspace.State.PendingRolemasterSituationalModifier = text)"
    ErrorMessage="@Workspace.State.PendingRolemasterSkillRollError"
    IsMutating="Workspace.State.IsMutating"
    IsSubmitting="Workspace.State.IsSubmittingRolemasterSkillRoll"
    ConfirmRequested="Workspace.Play.SubmitRolemasterSkillRollAsync"
    CancelRequested="Workspace.Play.CancelRolemasterSkillRollAsync"/>

@code {
    [Parameter, EditorRequired] public WorkspacePageContext Workspace { get; set; } = null!;

    private bool IsCampaignDataLoading => !Workspace.HasSessionInitialized || Workspace.State.IsCampaignDataLoading;

    private async Task OnRollVisibilityChangedAsync(string visibility)
    {
        var normalizedVisibility = string.Equals(visibility, "private", StringComparison.OrdinalIgnoreCase)
            ? "private"
            : "public";

        Workspace.State.RollVisibility = normalizedVisibility;
        await Workspace.Session.OnRollVisibilityChangedAsync(visibility);
        await InvokeAsync(StateHasChanged);
    }

    private string ResolveRollVisibility()
    {
        return Workspace.State.RollVisibility;
    }
}
