using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace CommonLibrary
{
    public static class ApiClient
    {
        /// <summary>
        /// Thực hiện Post lên TModel
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Url">Host name</param>
        /// <param name="Uri">Api ứng với Controller</param>
        /// <param name="SmatModel">Thực thể đầu vào</param>
        /// <returns></returns>
        public static Task<string> PostAsJsonAsync<T>(string Url, string Uri, T SmatModel)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(Url);
            Task<HttpResponseMessage> result = client.PostAsJsonAsync(Uri, SmatModel);
            if (result.Result.IsSuccessStatusCode)
            {
                return result.Result.Content.ReadAsStringAsync();
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// Thực hiện Post lên TModel với Token
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Url">Host name</param>
        /// <param name="Uri">Api ứng với Controller</param>
        /// <param name="SmatModel">Thực thể đầu vào</param>
        /// <param name="token">Token</param>
        /// <returns></returns>
        public static Task<string> PostAsJsonAsync<T>(string Url, string Uri, T SmatModel, string token)
        {
            try
            {
                if (string.IsNullOrEmpty(token) || string.IsNullOrWhiteSpace(token))
                {
                    HttpClient client = new HttpClient();
                    client.BaseAddress = new Uri(Url);
                    Task<HttpResponseMessage> result = client.PostAsJsonAsync(Uri, SmatModel);
                    if (result.Result.IsSuccessStatusCode)
                    {
                        return result.Result.Content.ReadAsStringAsync();
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    HttpClient client = new HttpClient();
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                    client.BaseAddress = new Uri(Url);
                    Task<HttpResponseMessage> result = client.PostAsJsonAsync(Uri, SmatModel);
                    if (result.Result.IsSuccessStatusCode)
                    {
                        return result.Result.Content.ReadAsStringAsync();
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }


        }

        /// <summary>
        /// Thực hện lấy ra tất cả bản ghi
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="Uri"></param>
        /// <returns></returns>
        public static Task<string> GetAsync(string Url, string Uri)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(Url);
            Task<HttpResponseMessage> result = client.GetAsync(Uri);
            if (result.Result.IsSuccessStatusCode)
            {
                return result.Result.Content.ReadAsStringAsync();
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Thực hện lấy ra tất cả bản ghi với Token
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="Uri"></param>
        /// <param name="token">Token</param>
        /// <returns></returns>
        public static Task<string> GetAsync(string Url, string Uri, string token)
        {
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            client.BaseAddress = new Uri(Url);
            Task<HttpResponseMessage> result = client.GetAsync(Uri)
                .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode());
            return result.Result.Content.ReadAsStringAsync();
        }

        public static Task<string> DeleteAsync(string Url, string Uri)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(Url);
            Task<HttpResponseMessage> rs = client.DeleteAsync(Uri)
                .ContinueWith((task) => task.Result.EnsureSuccessStatusCode());
            return rs.Result.Content.ReadAsStringAsync();
        }

        public static Task<string> DeleteAsync(string Url, string Uri, string token)
        {
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            client.BaseAddress = new Uri(Url);
            Task<HttpResponseMessage> rs = client.DeleteAsync(Uri)
                .ContinueWith((task) => task.Result.EnsureSuccessStatusCode());
            return rs.Result.Content.ReadAsStringAsync();
        }
    }
}
