using ReAct_PME.Domain;
using Services;
using System.Net.Http.Json;

namespace ReAct_PME.WebUI.ServicesUI
{
    /// <summary>
    /// Service pour créer les appels aux API.
    /// Les appels sont génériques
    /// Pour des appels spécifiques, il faudra créer un service spécifique
    /// </summary>
    public class ApiService
    {
        private readonly HttpClient _httpClient;

        public ApiService(HttpClient httpClient)
        {
            _httpClient = httpClient;
        }

        public async Task<bool> CreateAsync<T>(string url, T data)
        {
            var response = await _httpClient.PostAsJsonAsync(url, data);
            if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                return false;
            }
            else if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError)
            {
                throw new Exception("Internal server error");
            }
            else if (!response.IsSuccessStatusCode)
            {
                var errorJson = await response.Content.ReadAsStringAsync();
                Console.WriteLine(errorJson); // Affiche le détail du problème
            }
            return response.IsSuccessStatusCode;
        }

        public async Task<bool> UpdateAsync<T>(string url, T data)
        {
            var response = await _httpClient.PutAsJsonAsync(url, data);
            if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                return false;
            }
            else if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError)
            {
                throw new Exception("Internal server error");
            }
            else if (!response.IsSuccessStatusCode)
            {
                var errorJson = await response.Content.ReadAsStringAsync();
                Console.WriteLine(errorJson); // Affiche le détail du problème
            }
            return response.IsSuccessStatusCode;
        }

        public async Task<bool> DeleteAsync(string url)
        {
            var response = await _httpClient.DeleteAsync(url);
            if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                return false;
            }
            else if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError)
            {
                throw new Exception("Internal server error");
            }
            else if (!response.IsSuccessStatusCode)
            {
                var errorJson = await response.Content.ReadAsStringAsync();
                Console.WriteLine(errorJson); // Affiche le détail du problème
            }
            return response.IsSuccessStatusCode;
        }

        public async Task<T?> GetAllAsync<T>(string url)
        {
            return await _httpClient.GetFromJsonAsync<T>(url);
        }

        public async Task<T?> GetByIdAsync<T>(string url)
        {
            return await _httpClient.GetFromJsonAsync<T>(url);
        }

        public async Task<T?> GetAllInfoById<T>(string url)
        {
            return await _httpClient.GetFromJsonAsync<T>(url);
        }

        public async Task<T?> EnvoiRequete<T>(string url, SendMessageRequest data)
        {
            var response = await _httpClient.PostAsJsonAsync(url, data);

            if (!response.IsSuccessStatusCode)
                return default;

            return await response.Content.ReadFromJsonAsync<T>();
        }

        public async Task<string> EnvoiRequete<T>(string url, string idConversation, string idWorkForce, string question)
        {
            List<string> lst = new();
            lst.Add(question);
            lst.Add(idConversation);
            lst.Add(idWorkForce);
            var response = await _httpClient.PostAsJsonAsync(url, lst);

            if (!response.IsSuccessStatusCode)
                return $"Erreur : {response.StatusCode}";

            // lire le contenu JSON renvoyé
            var result = await response.Content.ReadAsStringAsync();
            return result;
        }

        public async Task<string> EnvoiRequete<T>(string url, string domain, string idConversation, string idWorkForce, string question)
        {
            List<string> lst = new();
            lst.Add(question);
            lst.Add(domain);
            lst.Add(idConversation);
            lst.Add(idWorkForce);

            var response = await _httpClient.PostAsJsonAsync(url, lst);

            if (!response.IsSuccessStatusCode)
                return $"Erreur : {response.StatusCode}";

            // lire le contenu JSON renvoyé
            var result = await response.Content.ReadAsStringAsync();
            return result;
        }

        public async Task<bool> SauvegardeItem<T>(string url, T data)
        {
            var response = await _httpClient.PostAsJsonAsync(url, data);

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine($"Erreur : {response.StatusCode}");
                return false;// $"Erreur : {response.StatusCode}";

            }

            return response.IsSuccessStatusCode;
        }

        public async Task<bool> EnvoyerMail(string url, MessageToSend data)
        {
            var response = await _httpClient.PostAsJsonAsync(url, data);

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine($"Erreur : {response.StatusCode}");
                return false;// $"Erreur : {response.StatusCode}";

            }

            return response.IsSuccessStatusCode;
        }

        public async Task<string?> EnvoiNewMail<T>(string url, string[] data)
        {
            var response = await _httpClient.PostAsJsonAsync(url, data);

            if (!response.IsSuccessStatusCode)
                return default;

            return await response.Content.ReadFromJsonAsync<string>();
        }


    }

}

