using Microsoft.AspNetCore.Components;
using Blazorise.DataGrid;
using Blazor.ViewClasses;
using Blazor.Modals;
using Blazor.Models;
using Blazored.LocalStorage;
using Blazored.Modal.Services;
using Blazor.Modals;
using Blazor.Services;
using Blazored.Modal;
using Blazored.Modal;
using Blazor.Pages.Chapters;

namespace Blazor.Pages.Admins;

public partial class Administrators
{
    public List<Administrator> administrators;

    private int totalItem;
    [CascadingParameter]
    public IModalService Modal { get; set; }

    [Inject]
    public IDataService DataService { get; set; }

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

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

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

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

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

        // When you use a real API, we use this follow code
        //var response = await Http.GetFromJsonAsync<ChaptersModel[]>( $"https://trusting-panini.87-106-126-109.plesk.page/api/administrators?page={e.Page}&pageSize={e.PageSize}" );
        var response = Http.GetFromJsonAsync<Administrator[]>($"https://trusting-panini.87-106-126-109.plesk.page/api/administrators").Result;

        if (!e.CancellationToken.IsCancellationRequested)
        {
            administrators = new List<Administrator>(response); // an actual data for the current page
            totalItem = administrators.Count;
            var currentData = await LocalStorage.GetItemAsync<Administrator[]>("data");
            if (currentData == null || currentData.Length != administrators.Count)
            {
                var originalData = Http.GetFromJsonAsync<Administrator[]>($"https://trusting-panini.87-106-126-109.plesk.page/api/administrators").Result;
                await LocalStorage.SetItemAsync("data", originalData);
            }
        }
    }

    //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<AdministratorModel[]>("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<AdministratorModel[]>($"https://trusting-panini.87-106-126-109.plesk.page/api/administrators").Result;
    //        await LocalStorage.SetItemAsync("data", originalData);
    //    }
    //}

    private async void OnDelete(int id)
    {
        var parameters = new ModalParameters();
        parameters.Add(nameof(Administrator.Id), id);

        var modal = Modal.Show<DeleteConfirmation>("Delete Confirmation", parameters);
        var result = modal.Result;

        if (result.IsCanceled)
        {
            return;
        }

        await DataService.Delete(id);

        // Reload the page
        NavigationManager.NavigateTo("administrators", true);
    }
}
