using DiunaBI.UI.Shared.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using DiunaBI.Application.DTOModels;
using DiunaBI.Application.DTOModels.Common;
using MudBlazor;
using Microsoft.JSInterop;

namespace DiunaBI.UI.Shared.Pages.Layers;

public partial class Index : ComponentBase, IDisposable
{
    [Inject] private LayerService LayerService { get; set; } = default!;
    [Inject] private EntityChangeHubService HubService { get; set; } = default!;
    [Inject] private ISnackbar Snackbar { get; set; } = default!;
    [Inject] private NavigationManager NavigationManager { get; set; } = default!;
    [Inject] private LayerFilterStateService FilterStateService { get; set; } = default!;
    [Inject] private IJSRuntime JSRuntime { get; set; } = default!;


    private PagedResult<LayerDto>  layers = new();
    private LayerFilterRequest filterRequest = new();
    private bool isLoading = false;

    protected override async Task OnInitializedAsync()
    {
        filterRequest = FilterStateService.FilterRequest;
        await LoadLayers();

        // Subscribe to SignalR entity changes
        HubService.EntityChanged += OnEntityChanged;
    }

    private async void OnEntityChanged(string module, string id, string operation)
    {
        // Only react if it's a Layers change
        if (module.Equals("Layers", StringComparison.OrdinalIgnoreCase))
        {
            await InvokeAsync(async () =>
            {
                await LoadLayers();
                StateHasChanged();
            });
        }
    }

    private async Task LoadLayers()
    {
        isLoading = true;

        try
        {
            FilterStateService.UpdateFilter(filterRequest);
            layers = await LayerService.GetLayersAsync(filterRequest);
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Loading layers failed: {ex.Message}");
        }
        finally
        {
            isLoading = false;
        }
    }

    private async Task SearchLayers()
    {
        filterRequest.Page = 1;
        await LoadLayers();
    }

    private async Task OnPageChanged(int page)
    {
        filterRequest.Page = page;
        await LoadLayers();
    }

    private async Task ClearFilters()
    {
        filterRequest = new LayerFilterRequest();
        FilterStateService.ClearFilter();
        await LoadLayers();
    }

    private async Task OnTypeClear()
    {
        filterRequest.Type = null;
        filterRequest.Page = 1;
        await LoadLayers();
    }

    private async Task OnTypeChanged(LayerType? type)
    {
        filterRequest.Type = type;
        filterRequest.Page = 1;
        await LoadLayers();
    }

    private void OnRowClick(LayerDto layer)
    {
        NavigationManager.NavigateTo($"/layers/{layer.Id}");
    }

    private async Task OnRowRightClick(MouseEventArgs e, LayerDto layer)
    {
        var url = NavigationManager.ToAbsoluteUri($"/layers/{layer.Id}").ToString();
        await JSRuntime.InvokeVoidAsync("open", url, "_blank");
    }

    public void Dispose()
    {
        HubService.EntityChanged -= OnEntityChanged;
    }
}
