<reponame>skancoder/DSA_Prepusing System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Configuration;

namespace ServerlessApp
{
    public class CreateOrderFunction
    {
        public const string Url = "Url";
        public const string UserName = "UserName";
        public const string Password = "Password";

        private readonly ITaskHelper _taskHelper;
        private readonly IConfiguration _configuration;
        private readonly HttpClient _httpClient;
        private readonly IMapper _mapper;

        public CreateOrderFunction(ITaskHelper taskHelper, IConfiguration configuration, HttpClient httpClient,IMapper mapper)
        {
            _taskHelper = taskHelper ?? throw new ArgumentNullException(nameof(taskHelper));
            _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
            _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
            _mapper = mapper?? throw new ArgumentNullException(nameof(mapper));
        }

        [FunctionName("CreateOrderFunction")]
        public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req)
        {
            // Console.WriteLine("Sample debug output");

            /* TODO: Implement Azure Function logic here */
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var functionRequest = _taskHelper.Deserialize<FunctionRequest>(requestBody);
            var legacySystemRequest=_mapper.Map<LegacySystemRequest>(functionRequest);
            var httpRequestMessage=GetLegacySystemHttpRequest(legacySystemRequest);
            var response = await _httpClient.SendAsync(httpRequestMessage);
            response.EnsureSuccessStatusCode();
            var content = await response.Content.ReadAsStringAsync();
            var legacySystemResponse = _taskHelper.Deserialize<LegacySystemResponse>(content);

        var functionResponse=_mapper.Map<FunctionResponse>(legacySystemResponse);
             return new OkObjectResult(functionResponse);
        }

        public HttpRequestMessage GetLegacySystemHttpRequest(LegacySystemRequest legacySystemRequest)
        {
            /* TODO: Implement getting HttpRequestMessage, that is sent to Legacy System here */    
            string reqUrl=_configuration.GetValue<string>(Url); 
            string userName=_configuration.GetValue<string>(UserName); 
            string password=_configuration.GetValue<string>(Password); 
            var token=_taskHelper.GetBase64EncodedString(userName+":"+password);

            string body=_taskHelper.Serialize(legacySystemRequest);
             var request = new HttpRequestMessage(HttpMethod.Post, reqUrl);
    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    request.Content = new StringContent(body, Encoding.UTF8);
    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

    request.Headers.Authorization = new AuthenticationHeaderValue("Basic", token);
            return request;
        }
    }
}

/*
This interface is already defined in the project within 'ServerlessApp' namespace. Please do not uncomment it!

Code snippets:

public interface ITaskHelper
{
    T Deserialize<T>(string json);
    string Serialize(object data);
    string GetBase64EncodedString(string text);
}
*/
