using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace isn
{
    public static class UploadFilesToServerUsingHttpClient
    {
        public static PushReport UploadFilesToServer(this HttpClient client, Uri uri, FileInfo fi,
      string apikey)
        {
            return UploadFilesToServerAsync(client, uri, fi, apikey).Result;
        }

        public static async Task<PushReport> UploadFilesToServerAsync(this HttpClient client, Uri uri, FileInfo fi,
       string apikey)
        {

            client.DefaultRequestHeaders.Add("X-NuGet-Client-Version", Constants.ClientVersion);
            client.DefaultRequestHeaders.Add("X-NuGet-ApiKey", apikey);

            using (var multipartFormDataContent = new MultipartFormDataContent())
            {
                /* var values = new[]
                 {
                     new KeyValuePair<string, string>("Id", Guid.NewGuid().ToString()),
                     new KeyValuePair<string, string>("Key", "awesome"),
                     new KeyValuePair<string, string>("From", "khalid@home.com")
                      //other values
                 };foreach (var keyValuePair in values)
                 {
                     multipartFormDataContent.Add(new StringContent(keyValuePair.Value), 
                         String.Format("\"{0}\"", keyValuePair.Key));
                 } */
                multipartFormDataContent.Add(new ByteArrayContent(File.ReadAllBytes(fi.FullName)),
                    '"' + "File" + '"',
                    '"' + fi.Name + '"');

                var result = await client.PutAsync(uri, multipartFormDataContent);
                
                if (result.IsSuccessStatusCode)
                {
                    string report = await result.Content.ReadAsStringAsync();
                    Console.WriteLine(report);

                }
                else
                {
                    string ereport = await result.Content.ReadAsStringAsync();
                    Console.WriteLine(ereport);
                }
                return new PushReport();
            }
        }
    }
}