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

namespace ZionVault.WebService
{
    class HttpClient
    {
        System.Net.Http.HttpClient client;

        public HttpClient(string server)
        {
            SetUp(server);
        }

        private void SetUp(string url)
        {
            client = new System.Net.Http.HttpClient();
            client.BaseAddress = new Uri(url);
            client.DefaultRequestHeaders.Accept.Clear();
        }
        public void Authorize(string token)
        {
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
                "Basic", token
                );
        }

        public void Authorize(string user, string pass)
        {
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
                "Basic", System.Convert.ToBase64String(
                        System.Text.Encoding.GetEncoding("UTF-8").GetBytes(user + ":" + pass)));
        }

        private async Task<string> Get(string endpoint)
        {
            System.Net.Http.HttpResponseMessage response = await client.GetAsync(endpoint);
            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsStringAsync();
            }
            throw new Exception(await response.RequestMessage.Content.ReadAsStringAsync());
        }

        private async Task<string> Put(string endpoint, string data)
        {
            StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
            System.Net.Http.HttpResponseMessage response = await client.PutAsync(endpoint, content);
            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsStringAsync();
            }
            throw new Exception(await response.RequestMessage.Content.ReadAsStringAsync());
        }

        private async Task<string> Post(string endpoint, string data)
        {
            StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
            System.Net.Http.HttpResponseMessage response = await client.PostAsync(endpoint, content);
            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsStringAsync();
            }
            throw new Exception(await response.RequestMessage.Content.ReadAsStringAsync());
        }

        private async Task<string> Delete(string endpoint)
        {
            System.Net.Http.HttpResponseMessage response = await client.DeleteAsync(endpoint);
            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsStringAsync();
            }
            throw new Exception(await response.RequestMessage.Content.ReadAsStringAsync());
        }
    }
}
