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.DataInbox;

public partial class Index : ComponentBase, IDisposable
{
    [Inject] private DataInboxService DataInboxService { 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 DataInboxFilterStateService FilterStateService { get; set; } = default!;
    [Inject] private IJSRuntime JSRuntime { get; set; } = default!;
    [Inject] private DateTimeHelper DateTimeHelper { get; set; } = default!;


    private PagedResult<DataInboxDto> dataInbox = new();
    private DataInboxFilterRequest filterRequest = new();
    private bool isLoading = false;

    protected override async Task OnInitializedAsync()
    {
        await DateTimeHelper.InitializeAsync();
        filterRequest = FilterStateService.FilterRequest;
        await LoadDataInbox();

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

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

    private async Task LoadDataInbox()
    {
        isLoading = true;

        try
        {
            FilterStateService.UpdateFilter(filterRequest);
            dataInbox = await DataInboxService.GetDataInboxAsync(filterRequest);
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Loading data inbox failed: {ex.Message}");
        }
        finally
        {
            isLoading = false;
        }
    }

    private async Task SearchDataInbox()
    {
        filterRequest.Page = 1;
        await LoadDataInbox();
    }

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

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

    private void OnRowClick(DataInboxDto dataInboxItem)
    {
        NavigationManager.NavigateTo($"/datainbox/{dataInboxItem.Id}");
    }

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

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