using Blazored.LocalStorage;
using Blazorise.DataGrid;
using Microsoft.AspNetCore.Components;
using TpBlazorr2.Models;

namespace TpBlazorr2.Pages;
public partial class Liste
{
    private List<Item> items;

    private int totalItem;

    [Inject]
    public IWebHostEnvironment WebHostEnvironment { get; set; }

    [Inject]
    public HttpClient Http { get; set; }

    [Inject]
    public ILocalStorageService LocalStorage { get; set; }

    [Inject]
    public NavigationManager NavigationManager { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        // Do not treat this action if is not the first render
        if (!firstRender)
        {
            return;
        }

        var currentData = await LocalStorage.GetItemAsync<Item[]>("data");

        // Check if data exist in the local storage
        if (currentData == null)
        {
            // this code add in the local storage the fake data (we load the data sync for initialize the data before load the OnReadData method)
            var originalData = Http.GetFromJsonAsync<Item[]>($"{NavigationManager.BaseUri}fake-data.json").Result;
            await LocalStorage.SetItemAsync("data", originalData);
        }
    }

    private async Task OnReadData(DataGridReadDataEventArgs<Item> e)
    {
        if (e.CancellationToken.IsCancellationRequested)
        {
            return;
        }

        // When you use a real API, we use this follow code
        //var response = await Http.GetJsonAsync<Data[]>( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" );
        var response = (await LocalStorage.GetItemAsync<Item[]>("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();

        if (!e.CancellationToken.IsCancellationRequested)
        {
            totalItem = (await LocalStorage.GetItemAsync<List<Item>>("data")).Count;
            items = new List<Item>(response); // an actual data for the current page
        }
    }
}