using collAnon.Pub.Shared;
using drinkMe.Client.Services.Interfaces;
using drinkMe.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading.Tasks;

namespace drinkMe.Client.Services
{
	public class PaymentService : IPaymentService
	{
		readonly HttpClient HttpClient;
		public PaymentService(HttpClient httpClient)
		{
			HttpClient = httpClient;
		}

		public async Task<WebResult> Pay(PurchaseCart purchaseCart)
		{
			var result = new WebResult();
			var httpResponse = await HttpClient.PostAsync($"api/Drinks/{nameof(Pay)}", JsonContent.Create(purchaseCart));

			if (!httpResponse.IsSuccessStatusCode)
				return result.Invalidate(JsonSerializer.Deserialize<WebResult>(await httpResponse.Content.ReadAsStringAsync(), new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }));

			return result;
		}
	}
}
