@page "/navigation-maps/settings/{Id:guid}"
@attribute [Authorize]

@using RobotNet.MapShares.Dtos
@using RobotNet.Shares
@using RobotNet.WebApp.Maps.Components.Setting

@inject IJSRuntime JSRuntime
@inject IHttpClientFactory HttpClientFactory
@inject NavigationManager Nav

<PageTitle>Map Setting</PageTitle>

<div class="d-flex flex-column w-100 h-100">
    <div class="d-flex h-100 w-100">
        <MudTabs Class="w-100 h-100" Elevation="2" ApplyEffectsToContainer="true" PanelClass="overflow-x-auto w-100 h-100" Color="@Color.Primary" ActivePanelIndexChanged="ActivePanelChanged">
            <ChildContent>
                <MudTabPanel Text="Default Settings">
                    <div class="pa-6">
                        <MapSettingDefault @ref=MapConfigDefaultRef Id="@Id" />
                    </div>
                </MudTabPanel>
                <MudTabPanel Text="Default Action">
                    <MapSettingAction @ref=MapConfigActionRef Id="@Id" />
                </MudTabPanel>
            </ChildContent>
            <Header>
                <MudText Class="d-flex flex-nowrap align-items-center me-5 text-truncate" Typo="Typo.h6">@MapName</MudText>
                <MudDivider Class="me-2" Vertical="true" FlexItem="true"/>
                @if (PanelSelected == 1)
                {
                    <MudTooltip RootClass="d-flex align-items-center" Text="Delete" Placement="Placement.Bottom" Color="Color.Info" Arrow="true">
                        <MudIconButton Class="mx-2" Icon="@Icons.Material.Filled.Delete" Variant="Variant.Filled" Color="Color.Secondary" OnClick="DeleteAction"></MudIconButton>
                    </MudTooltip>
                    <MudTooltip RootClass="d-flex align-items-center" Text="Create" Placement="Placement.Bottom" Color="Color.Info" Arrow="true">
                        <MudIconButton Class="mx-2" Icon="@Icons.Material.Filled.Add" Variant="Variant.Filled" Color="Color.Info" OnClick="CreateAction"></MudIconButton>
                    </MudTooltip>
                }
                <MudTooltip RootClass="d-flex align-items-center"  Text="Save" Placement="Placement.Bottom" Color="Color.Info" Arrow="true">
                    <MudIconButton Class="mx-2" Icon="@Icons.Material.Filled.Save" Variant="Variant.Filled" Color="Color.Info" OnClick="OnSave"></MudIconButton>
                </MudTooltip>
                <MudTooltip RootClass="d-flex align-items-center" Text="Reset" Placement="Placement.Bottom" Color="Color.Info" Arrow="true">
                    <MudIconButton Class="mx-2" Icon="@Icons.Material.Filled.Refresh" Variant="Variant.Filled" Color="Color.Info" OnClick="OnReset"></MudIconButton>
                </MudTooltip>
                <MudTooltip RootClass="d-flex align-items-center" Text="Exit" Placement="Placement.Bottom" Color="Color.Info" Arrow="true">
                    <MudIconButton Class="mx-2" Icon="@Icons.Material.Filled.KeyboardReturn" Variant="Variant.Filled" Color="Color.Info" OnClick="@(() => Nav.NavigateTo($"/navigation-maps"))"></MudIconButton>
                </MudTooltip>
            </Header>
        </MudTabs> 
    </div>
</div>


@code {
    [Parameter, EditorRequired]
    public Guid Id { get; set; }

    private MapSettingDefault MapConfigDefaultRef = null!;
    private MapSettingAction MapConfigActionRef = null!;
    private int PanelSelected = 0;

    private string MapName = "";
    private HttpClient Http = default!;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (!firstRender) return;

        Http = HttpClientFactory.CreateClient("MapManagerAPI");
        var reponse = await Http.GetFromJsonAsync<MessageResult<MapInfoDto>>($"api/MapsManager/{Id}");
        if (reponse is not null && reponse.Data is not null) MapName = reponse.Data.Name;
        StateHasChanged();
    }

    private async Task OnSave()
    {
        switch (PanelSelected)
        {
            case 0:
                await MapConfigDefaultRef.Save();
                break;
            case 1:
                await MapConfigActionRef.Save();
                break;
        }
    }

    private void OnReset()
    {
        switch (PanelSelected)
        {
            case 0:
                MapConfigDefaultRef.Reset();
                break;
            case 1:
                MapConfigActionRef.Reset();
                break;
        }
    }

    private void ActivePanelChanged(int value)
    {
        PanelSelected = value;
    }

    private void CreateAction()
    {
        MapConfigActionRef.ShowCreateAction();
    }

    private async Task DeleteAction()
    {
        await MapConfigActionRef.DeleteAction();
    }
}
