using System;
using System.Net.Http;
using Blazorise.DataGrid;
using HeartTrack.Models;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;

namespace HeartTrack.Pages
{
	public partial class Activities
	{
        private List<Activity> activities;

        private int totalActivity;

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

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

        [Inject]
        public IStringLocalizer<Activities> Localizer { get; set; }

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

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

            //var response = await Http.GetStringAsync("http://localhost:8080/api");
            //var activityList = JsonConvert.DeserializeObject<List<Activity>>(response);

            var response = (await Http.GetFromJsonAsync<Activity[]>("http://localhost:8080/api")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();

            //await Http.GetFromJsonAsync<Activity[]>("http://localhost:8080/api");
            //Console.WriteLine(response);

            //var response = (await Http.GetFromJsonAsync<Activity[]>($"{NavigationManager.BaseUri}fake-data-activity.json")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();

            if (!e.CancellationToken.IsCancellationRequested)
            {
                totalActivity = (await Http.GetFromJsonAsync<List<Activity>>("http://localhost:8080/api")).Count;
                //(await Http.GetFromJsonAsync<List<Activity>>($"{NavigationManager.BaseUri}fake-data-activity.json")).Count;
                activities = new List<Activity>(response); // an actual data for the current page
                //Console.WriteLine(totalActivity);
            }
        }
    }
}

