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

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

@inject IJSRuntime JSRuntime
@inject IHttpClientFactory HttpClientFactory
@inject ISnackbar Snackbar

<PageTitle>Map Element</PageTitle>

<div class="d-flex w-100 h-100 flex-row position-relative p-2">
    <div class="d-flex h-100 flex-column" style="width: 40%;">
        <div class="d-flex flex-row" style="height: 70%;">
            <div class="d-flex h-100 w-50">
                <ElementModelTable MapInfo="@MapInfo" ElementModels="ElementModels" ElementModelSelectChanged="ElementModelSelectChanged" />
            </div>
            <MudDivider Vertical="true" FlexItem="true" Style="border: 0.5px solid gray" />
            <div class="d-flex h-100 w-50">
                <ElementDefaultProperty @ref="@ElementDefaultPropertyRef" />
            </div>
        </div>
        <MudDivider Vertical="true" FlexItem="true" Style="border: 0.5px solid gray" />
        <div class="d-flex flex-grow-1" >
            <ElementImage @ref="@ElementImageRef" />
        </div>
    </div>
    <MudDivider Vertical="true" FlexItem="true" Style="border: 0.5px solid gray" />
    <div class="d-flex h-100 flex-column" style="width: 60%">
        <div class="d-flex w-100" style="height: 70%;">
            <MapElement @ref="MapElementRef" ElementSelectChanged="ElementSelectChanged" />
        </div>
        <MudDivider Vertical="true" FlexItem="true" Style="border: 0.5px solid gray" />
        <div class="d-flex flex-grow-1">
            <ElementPropertyTable @ref="@ElementPropertyRef" ElementPropertyChagned="((value) => MapElementRef.UpdateProperty(value))" />
        </div>
    </div>

    <MudOverlay @bind-Visible="IsLoading" DarkBackground="true" Absolute="true">
        <MudText Typo="Typo.h1" Color="@Color.Error" Style="font-weight: bold;">Is Loadding ...</MudText>
    </MudOverlay>
</div>

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

    public List<ElementModelDto> ElementModels { get; set; } = [];
    public List<ElementDto> Elements { get; set; } = [];

    private bool IsLoading { get; set; }
    private MapInfoDto MapInfo = new();

    private ElementDefaultProperty ElementDefaultPropertyRef = default!;
    private ElementImage ElementImageRef = default!;
    private MapElement MapElementRef = default!;
    private ElementPropertyTable ElementPropertyRef = default!;
    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) MapInfo = reponse.Data;
        await LoadElementModels();
        StateHasChanged();
    }

    private async Task LoadElementModels()
    {
        IsLoading = true;
        ElementModels.Clear();
        StateHasChanged();

        var elModels = await Http.GetFromJsonAsync<MessageResult<IEnumerable<ElementModelDto>>>($"api/ElementModels/map/{Id}");
        if (elModels is null) Snackbar.Add("Lỗi giao tiếp với hệ thống", Severity.Error);
        else if (!elModels.IsSuccess) Snackbar.Add($"Có lỗi xảy ra: {elModels.Message}", Severity.Error);
        else if (elModels.Data is null || !elModels.Data.Any())
        {
            Snackbar.Add("Không có mô hình phần tử nào được tìm thấy", Severity.Warning);
        }
        else
        {
            ElementModels.AddRange(elModels.Data.OrderBy(el => el.Name));
        }
        IsLoading = false;
        StateHasChanged();
    }

    private async Task ElementModelSelectChanged(ElementModelDto? model)
    {
        ElementDefaultPropertyRef.SetElementModel(model);
        ElementImageRef.SetElementModel(model);
        await MapElementRef.SetData(model);
    }

    private void ElementSelectChanged(ElementDto? model) => ElementPropertyRef.SetProperties(model);
}
