using System.Net.Http.Headers;
using System.Text;

namespace IVM.Services
{
    public class webClient
    {
        private HttpRequestMessage webRequest;
        private HttpContent webContent;
        public HttpClient client { get; set; }
        /// <summary>
        /// Sets up a new client to connect with remote.
        /// </summary>
        /// <param name="_token">The bearer either captured from Swagger for development and set in appsettings.Development.json or from the user logon thru Active Directory </param>
        /// <param name="_baseUrl">set in appsettings.Development.json and captured thru Dependency Injection from Program.cs</param>
        public webClient(string _token, string _baseUrl)
        {
            client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {_token}");
            client.BaseAddress = new Uri(_baseUrl);
            client.Timeout = new(0, 2, 30);
        }

        /// <summary>
        /// Dispatches the Request to the server.
        /// </summary>
        /// <returns>HttpResponseMessage</returns>
        internal Task<HttpResponseMessage> GetData(string _endPoint) { return client.GetAsync($"{client.BaseAddress}/{_endPoint}", HttpCompletionOption.ResponseContentRead); }

        /// <summary>
        /// Put - Dispatches content for the server to update
        /// </summary>
        /// <param name="_endPoint">The specific endPoint to update</param>
        /// <returns>async HttpResponseMessage</returns>
        internal Task<HttpResponseMessage> PutData(string _endPoint) { return client.PutAsync($"{client.BaseAddress}{_endPoint}", webContent); }

        internal Task<HttpResponseMessage> PostData(string endPoint)
        {
            return client.PostAsync($"{client.BaseAddress}/{endPoint}", webContent);
        }

        internal Task<HttpResponseMessage> DeleteRequest(string _endPoint) { return client.DeleteAsync($"{client.BaseAddress}/{_endPoint}"); }

        /// <summary>
        /// Initialializes the Message Request to dispatch
        /// </summary>
        /// <param name="_method"></param>
        /// <param name="_endPoint"></param>
        /// <param name="_content"></param>
        /// <returns>Webclient</returns>
        internal webClient SetRequest(string _method, string _endPoint)
        {
          StringContent contextContent = new("{'Data', 'Array'}", Encoding.UTF8, "application/json");

          webRequest = new(new HttpMethod(_method), _endPoint) { Content = contextContent };
          return this;
        }

        /// <summary>
        /// Initialializes the Message Send Request to dispatch
        /// </summary>
        /// <param name="_method"></param>
        /// <param name="_endPoint"></param>
        /// <param name="jsonString"></param>
        /// <returns>Webclient</returns>
        internal webClient SendRequest(string _method, string _endPoint, string jsonString)
        {
            StringContent contextContent = new(jsonString, Encoding.UTF8, "application/json");

            webRequest = new(new HttpMethod(_method), _endPoint) { Content = contextContent };
            return this;
        }

        /// <summary>
        /// Initialializes the Message Content to dispatch
        /// </summary>
        /// <param name="method">PUT</param>
        /// <param name="endpoint">The specific endpoint to update</param>
        /// <param name="jsonString">The captured JSon to dispatch</param>
        /// <returns>Webclient</returns>
        internal webClient SetRequest(string method, string endpoint, string jsonString)
        {
            StringContent contextContent = new(jsonString, Encoding.UTF8, "application/json");
            webContent = contextContent;
            return this;
        }

        internal webClient SendDelete(string method, string endPoint)
        {
            StringContent contextContent = new("{'Data', 'Array'}", Encoding.UTF8, "application/json");

            webRequest = new(new HttpMethod(method), endPoint) { Content = contextContent };
            
            return this;
        }
    }
}
