using Edge.Core.HttpClient;
using HengshanPaymentTerminal.Http.Request;
using Microsoft.Extensions.DependencyInjection;
using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace HengshanPaymentTerminal.Http
{
    public class HttpClientUtils:IHttpClientUtil
    {
        private IHttpClient _httpClientService;

        [ActivatorUtilitiesConstructor]
        public HttpClientUtils(IHttpClient httpClientService)
        {
            _httpClientService = httpClientService;
        }

        public HttpClientUtils()
        {
            ServiceCollection serviceCollection = new ServiceCollection();
            serviceCollection.AddHttpClient();
            ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
            IHttpClientFactory httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
            _httpClientService = new HttpClientService(httpClientFactory);
        }

        public async Task<HttpResponseMessage> CreateTransaction(string requestJson)
        {
            var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
            return await _httpClientService.PostAsync("api/transactions/CreateTransaction", requesStr);
        }

        public async Task<HttpResponseMessage> SendNozzleInfo(string requestJson)
        {
            var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
            return await _httpClientService.PostAsync("api/nozzle/uploadNozzle", requesStr);
        }

        public async Task<HttpResponseMessage> SendNozzleStatu(string requestJson)
        {
            var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
            return await _httpClientService.PostAsync("api/nozzle/updateNozzleStatus", requesStr);
        }
    }
}
