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(Uri uri, FileInfo fi,
       string apikey)
        {
            using (var client = new HttpClient())
            {
                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 = client.PutAsync(uri, multipartFormDataContent).Result;
                    result.EnsureSuccessStatusCode();
                    if (result.IsSuccessStatusCode)
                    {
                        Task.Run(async ()=>
                        {
                            string report = await result.Content.ReadAsStringAsync();
                            Console.WriteLine(report);
                        }).Wait();

                    }
                    return new PushReport();
                }
            }
        }
    }
}