using IdentityServer.PublishedLanguage.Dto;
using IdentityServer.Wrapper.Constants;
using IdentityServer.Wrapper.Models;
using NDB.Extensions.Http;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace IdentityServer.Wrapper.Services
{
    internal class IdentityService : IIdentityService
    {
        private const string _contentType = "application/json";
        private readonly HttpClient _httpClient;

        public IdentityService(HttpClient httpClient, ServiceConfiguration configuration)
        {
            httpClient.BaseAddress = new Uri(configuration.BaseAddress);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_contentType));

            _httpClient = httpClient;
        }

        public async Task<Token> Authenticate(string userName, string password)
        {
            var route = string.Format(ApiRoutes.Authentication, userName, password);
            var result = await _httpClient.ExecutePostRequest<Token>(route);
            return result;
        }

        public async Task<User> Authorize(string token)
        {
            var route = string.Format(ApiRoutes.Authorization, token);
            var result = await _httpClient.ExecutePostRequest<User>(route);
            return result;
        }
    }
}