@inject IHttpClientFactory HttpClientFactory
@inject ISnackbar Snackbar
@inject IDialogService Dialog

<div class="d-flex w-100 h-100 flex-column ps-2">
    <div class="d-flex flex-row justify-content-between w-100 my-2">
        <MudText Typo="Typo.h6" Class="d-flex align-items-center">Properties</MudText>
        <div class="d-flex flex-row">
            <MudTooltip RootClass="d-flex align-items-center" Text="Synchronize" Placement="Placement.Bottom" Color="Color.Primary" Arrow="true">
                <MudIconButton Icon="@Icons.Material.Filled.Sync" Variant="Variant.Text" Color="Color.Primary" Size="Size.Small" Disabled="@(Element is null)" OnClick="SynchronizeProperty" />
            </MudTooltip>
            <MudTooltip RootClass="d-flex align-items-center" Text="Edit" Placement="Placement.Bottom" Color="Color.Info" Arrow="true">
                <MudIconButton Icon="@Icons.Material.Filled.Edit" Size="Size.Small" Color="Color.Info" Disabled="@(IsEdit || ElementProperties.Count == 0 || Element is null)" OnClick="(() => IsEdit = true)" />
            </MudTooltip>
            <MudTooltip RootClass="d-flex align-items-center" Text="Save" Placement="Placement.Bottom" Color="Color.Info" Arrow="true">
                <MudIconButton Icon="@Icons.Material.Filled.Save" Variant="Variant.Text" Color="Color.Info" Size="Size.Small" Disabled="@(!IsEdit || Element is null)" OnClick="Save" />
            </MudTooltip>
        </div>
    </div>
    <div class="d-flex w-100 flex-grow-1 overflow-x-hidden overflow-y-auto mt-2 align-content-start flex-wrap ps-3">
        @foreach (var property in ElementProperties)
        {
            <div class="d-flex mb-1 pe-3" style="width:33%">
                @if (property.Type == typeof(int).ToString())
                {
                    if (int.TryParse(property.DefaultValue, out int value))
                    {
                        <MudNumericField T="int" Margin="Margin.Dense" Label="@property.Name" Value="value" Variant="Variant.Outlined" ReadOnly="@(!IsEdit)"
                        ValueChanged="((data) => DefaultIntValueChanged(property.Id, data))" />
                    }
                }
                else if (property.Type == typeof(double).ToString())
                {
                    if (double.TryParse(property.DefaultValue, out double value))
                    {
                        <MudNumericField T="double" Margin="Margin.Dense" Label="@property.Name" Value="value" Variant="Variant.Outlined" Step="0.1" ReadOnly="@(!IsEdit)"
                        ValueChanged="((data) => DefaultDoubleValueChanged(property.Id, data))" />
                    }
                }
                else if (property.Type == typeof(bool).ToString())
                {
                    if (bool.TryParse(property.DefaultValue, out bool value))
                    {
                        <MudCheckBox Class="m-0" Size="Size.Small" T="bool" LabelPlacement="Placement.Start" Label="@property.Name" Value="value" Color="Color.Success"
                        UncheckedColor="Color.Default" ReadOnly="@(!IsEdit)" ValueChanged="((data) => DefaultBooleanValueChanged(property.Id, data))" />
                    }
                }
                else if (property.Type == typeof(string).ToString())
                {
                    <MudTextField T="string" Label="@property.Name" Value="@property.DefaultValue" Variant="Variant.Outlined" Margin="Margin.Dense" ReadOnly="@(!IsEdit)"
                    ValueChanged="((data) => property.DefaultValue = data)" />
                }
            </div>
        }
    </div>
</div>

@code {
    [Parameter]
    public EventCallback<ElementDto> ElementPropertyChagned { get; set; }

    private List<ElementProperty> ElementProperties = [];

    private bool IsEdit = false;
    private ElementDto? Element = null;
    private HttpClient Http = default!;

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

        Http = HttpClientFactory.CreateClient("MapManagerAPI");
    }

    public void SetProperties(ElementDto? element)
    {
        ElementProperties.Clear();
        Element = element;
        if (element is not null && !string.IsNullOrEmpty(element.Content))
        {
            var properties = JsonSerializer.Deserialize<List<ElementProperty>>(element.Content, JsonOptionExtends.Read);
            if (properties is not null) ElementProperties.AddRange(properties);
        }
        StateHasChanged();
    }

    private void DefaultIntValueChanged(Guid id, int value)
    {
        var property = ElementProperties.FirstOrDefault(p => p.Id == id);
        if (property is null) return;
        property.DefaultValue = value.ToString();
    }

    private void DefaultDoubleValueChanged(Guid id, double value)
    {
        var property = ElementProperties.FirstOrDefault(p => p.Id == id);
        if (property is null) return;
        property.DefaultValue = value.ToString();
    }

    private void DefaultBooleanValueChanged(Guid id, bool value)
    {
        var property = ElementProperties.FirstOrDefault(p => p.Id == id);
        if (property is null) return;
        property.DefaultValue = value.ToString();
    }

    private async Task Save()
    {
        if (Element is null) return;

        var result = await (await Http.PutAsJsonAsync($"api/Elements", new ElementUpdateModel()
            {
                Id = Element.Id,
                Name = Element.Name,
                IsOpen = Element.IsOpen,
                OffsetX = Element.OffsetX,
                OffsetY = Element.OffsetY,
                Content = JsonSerializer.Serialize(ElementProperties, JsonOptionExtends.Write),
            })).Content.ReadFromJsonAsync<MessageResult<ElementDto>>();
        if (result == null) Snackbar.Add("Lỗi giao tiếp với hệ thống", Severity.Error);
        else if (!result.IsSuccess) Snackbar.Add(result.Message, Severity.Error);
        else if (result.Data is null) Snackbar.Add("Lỗi dữ liệu", Severity.Error);
        else
        {
            await ElementPropertyChagned.InvokeAsync(result.Data);
            Snackbar.Add("Cập nhật thành công", Severity.Success);
        }

        IsEdit = false;
        StateHasChanged();
    }

    private async Task SynchronizeProperty()
    {
        if (Element is null) return;
        var parameters = new DialogParameters<ConfirmDialog>
        {
            { x => x.Content, $"Bạn có chắc chắn muốn đồng bộ tham số với Element Model không?" },
            { x => x.ConfirmText, "Yes" },
            { x => x.Color, Color.Primary }
        };
        var confirm = await Dialog.ShowAsync<ConfirmDialog>("Đồng bộ tham số", parameters);
        var result = await confirm.Result;
        if (result is not null && result.Data is not null && bool.TryParse(result.Data.ToString(), out bool data) && data)
        {
            var elementModel = await LoadElementModels(Element.ModelId);
            if (elementModel is null) Snackbar.Add("Element Model không tồn tại", Severity.Error);
            else 
            {
                List<ElementProperty> ModelProperties = [];
                var modelProperties = JsonSerializer.Deserialize<List<ElementProperty>>(elementModel.Content, JsonOptionExtends.Read);
                if (modelProperties is not null) ModelProperties.AddRange(modelProperties);
                parameters = new DialogParameters<ConfirmDialog>
                {
                    { x => x.Content, $"Bạn có muốn đồng bộ giá trị với Element Model không?" },
                    { x => x.CancelText, "No" },
                    { x => x.ConfirmText, "Yes" },
                    { x => x.Color, Color.Primary }
                }; 
                confirm = await Dialog.ShowAsync<ConfirmDialog>("Đồng bộ tham số", parameters);
                result = await confirm.Result;
                if (result is not null)
                {
                    var valueSynchronize = result.Data is not null && bool.TryParse(result.Data.ToString(), out _);
                    foreach (var modelProperty in ModelProperties)
                    {
                        var property = ElementProperties.FirstOrDefault(em => em.Id == modelProperty.Id);
                        if(property is null) ElementProperties.Add(modelProperty);
                        else 
                        {
                            property.Name = modelProperty.Name;
                            property.DefaultValue = valueSynchronize ? modelProperty.DefaultValue : property.DefaultValue;
                        }
                    }
                    ElementProperties.RemoveAll(ep => !ModelProperties.Any(mp => mp.Id == ep.Id));
                    await Save();
                }
            }
            StateHasChanged();
        }
    }

    private async Task<ElementModelDto?> LoadElementModels(Guid modelId)
    {
        var elModel = await Http.GetFromJsonAsync<MessageResult<ElementModelDto>>($"api/ElementModels/{modelId}");
        if (elModel is null) Snackbar.Add("Lỗi giao tiếp với hệ thống", Severity.Error);
        else if (!elModel.IsSuccess) Snackbar.Add($"Có lỗi xảy ra: {elModel.Message}", Severity.Error);
        else return elModel.Data;
        return null;
    }
}
