using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace CardReaderService
{
    static class DataCenterHelper
    {
        public static void Post(CardDataPost postObject, string url)
        {
            var endpointConfig = ConfigurationManager.AppSettings["DataCenterServiceEndpoint"] ??
                                 "http://localhost:8800";

            using (var client = new HttpClient())
            {
                var jsonObject = JsonConvert.SerializeObject(postObject);
                var content = new StringContent(jsonObject, Encoding.UTF8, "application/json");
                try
                {
                    Console.WriteLine("Writing");
                    var fullUrl = endpointConfig + url;
                    var response = client.PostAsync(fullUrl, content).Result;
                    Console.WriteLine("Written");
                }
                catch (Exception)
                {
                    Console.WriteLine("exceptioning");
                    //ignore
                }
            }
        }

        public static Task PostAsync(CardDataPost postObject, string url)
        {
            return Task.Run(() => Post(postObject, url));
        }
    }

    public class CardDataPost
    {
        public string CardUId { get; set; }
    }
}
