
using EonaCat.HttpClient;
using EonaCat.HttpClient.Extensions;
using System.Globalization;

namespace EonaCat.Testers.HttpClient
{ 
    public static class HttpClientTest
    {
        private const string Large_Json_file = "https://raw.githubusercontent.com/json-iterator/test-data/master/large-file.json";

        public static async Task Main()
        {
            Console.WriteLine("Test");
            var httpClient = new EonaCat.HttpClient.HttpClient(Large_Json_file);
            httpClient.OnReadProgressChanged += HttpClient_OnReadProgressChanged;
            httpClient.OnWriteProgressChanged += HttpClient_OnWriteProgressChanged;
            var getRequest = httpClient.GetRequest();
            var response = await getRequest.ExecuteAsync<List<Large_Json>>().ConfigureAwait(false);
            Console.WriteLine($"We got {(response.HasResult ? response.Result.Count : 0)} results - elapsed: {response.Elapsed.Value}");

            Console.WriteLine("Test2");
            var httpClient2 = new EonaCat.HttpClient.HttpClient("https://google.com");
            httpClient2.OnReadProgressChanged += HttpClient_OnReadProgressChanged;
            httpClient2.OnWriteProgressChanged += HttpClient_OnWriteProgressChanged;
            var getRequest2 = httpClient2.GetRequest();
            var response2 = await getRequest2.ExecuteAsHttpResponseMessageAsync().ConfigureAwait(false);
            Console.WriteLine($"We got {(response2.HasResult ? await response2.Result.Content.ReadAsStringWithProgressAsync("test", (x) => { Console.WriteLine($"{x.PercentageAsString}%");  }) : 0)}  - elapsed: {response2.Elapsed.Value}");

        }

        private static void HttpClient_OnWriteProgressChanged(object? sender, ProgressArgs e)
        {
            Console.WriteLine($"WRITE: {e.Identifier} : {e.PercentageAsString}");
        }

        private static void HttpClient_OnReadProgressChanged(object? sender, ProgressArgs e)
        {
            Console.WriteLine($"READ: {e.Identifier} : {e.PercentageAsString}");
        }
    }
}