using Newtonsoft.Json;
using System.Net.Http.Json;

namespace DigitalData.Core.Client
{
    /// <summary>
    /// Extension methods for HttpClient and HttpResponseMessage.
    /// </summary>
    public static class HttpExtensions
    {
        /// <summary>
        /// Reads the content of the HTTP response message as a string.
        /// </summary>
        /// <param name="response">The HTTP response message.</param>
        /// <returns>A task representing the response content as a string.</returns>
        public static async Task<string> Text(this HttpResponseMessage response) => await response.Content.ReadAsStringAsync();

        /// <summary>
        /// Reads the content of the HTTP response message and deserializes it to a specified type.
        /// </summary>
        /// <typeparam name="T">The type to deserialize the response content to.</typeparam>
        /// <param name="response">The HTTP response message.</param>
        /// <returns>A task representing the deserialized response content.</returns>
        public static async Task<T?> Json<T>(this HttpResponseMessage response) => await response.Content.ReadFromJsonAsync<T>();

        /// <summary>
        /// Reads the content of the HTTP response message and deserializes it to a dynamic object.
        /// </summary>
        /// <param name="response">The HTTP response message.</param>
        /// <returns>A task representing the deserialized response content as a dynamic object.</returns>
        public static async Task<dynamic> Json(this HttpResponseMessage response)
        {
            string json = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<dynamic>(json) ?? string.Empty;
        }

        /// <summary>
        /// Reads the content of the HTTP response message and deserializes it to a list of dynamic objects.
        /// </summary>
        /// <param name="response">The HTTP response message.</param>
        /// <returns>A task representing the deserialized response content as a list of dynamic objects.</returns>
        public static async Task<IEnumerable<dynamic>> JsonList(this HttpResponseMessage response)
        {
            string json = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<dynamic>(json) ?? string.Empty;
        }

        /// <summary>
        /// Executes an action when a task is completed.
        /// </summary>
        /// <typeparam name="T">The type of the task result.</typeparam>
        /// <param name="task">The task to await.</param>
        /// <param name="toDo">The action to execute when the task is completed.</param>
        public static async void ThenAsync<T>(this Task<T> task, Action<T> toDo)
        {
            var then = await task;
            toDo(then);
        }

        /// <summary>
        /// Executes a function when a task is completed and returns the result.
        /// </summary>
        /// <typeparam name="T">The type of the task result.</typeparam>
        /// <typeparam name="I">The return type of the function.</typeparam>
        /// <param name="task">The task to await.</param>
        /// <param name="toDo">The function to execute when the task is completed.</param>
        /// <returns>A task representing the result of the function.</returns>
        public static async Task<I> ThenAsync<T, I>(this Task<T> task, Func<T, I> toDo)
        {
            var then = await task;
            return toDo(then);
        }

        /// <summary>
        /// Executes an asynchronous function when a task is completed and returns the result.
        /// </summary>
        /// <typeparam name="T">The type of the task result.</typeparam>
        /// <typeparam name="I">The return type of the asynchronous function.</typeparam>
        /// <param name="task">The task to await.</param>
        /// <param name="toDoAsync">The asynchronous function to execute when the task is completed.</param>
        /// <returns>A task representing the result of the asynchronous function.</returns>
        public static async Task<I> ThenAsync<T, I>(this Task<T> task, Func<T, Task<I>> toDoAsync)
        {
            var then = await task;
            return await toDoAsync(then);
        }

        /// <summary>
        /// Executes an action for each element in an enumerable collection.
        /// </summary>
        /// <typeparam name="T">The type of the elements in the collection.</typeparam>
        /// <param name="values">The enumerable collection of elements.</param>
        /// <param name="action">The action to execute for each element.</param>
        public static void ForEach<T>(this IEnumerable<T> values, Action<T> action)
        {
            foreach (var value in values)
                action(value);
        }
    }
}