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

namespace DiunaBI.UI.Shared.Components;

public partial class LayerListComponent : ComponentBase
{
    [Inject] private LayerService LayerService { get; set; } = default!;
    [Inject] private ISnackbar Snackbar { get; set; } = default!;
    [Inject] private NavigationManager NavigationManager { get; set; } = default!;


    private PagedResult<LayerDto>  layers = new();
    private LayerFilterRequest filterRequest = new();
    private bool isLoading = false;
    
    protected override async Task OnInitializedAsync()
    {
        await LoadLayers();
    }

    private async Task LoadLayers()
    {
        isLoading = true;

        try
        {
            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();
        await LoadLayers();
    }

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