﻿using System.Net.Http.Headers;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Hedy.Core.Administration.Infrastructure
{
    public partial class ClientRest
    {
        public async Task<HttpMessage<T>> GetAsync<T>(int id, string methodName)
        {
            HttpMessage<T> httpMessage = new HttpMessage<T>();
            HttpResponseMessage httpResponseMessage = new HttpResponseMessage();

            var url = string.Concat(this.resourceName, "/", methodName, "/", id);

            using (var handler = new HttpClientHandler())
            using (var client = new HttpClient(handler))
            {
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", TokenClient.GetAcessToken());
                client.BaseAddress = new Uri(this.baseUri);
                httpResponseMessage = await client.GetAsync(url);
            }

            httpMessage.StatusCode = httpResponseMessage.StatusCode;
            var responseContent = httpResponseMessage.Content.ReadAsStringAsync();
            var sdtasfsf = JsonConvert.DeserializeObject<dynamic>(responseContent.Result);

            return httpMessage;
        }
    }
}