using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace Baosight.HBase
{
    class Request4Json
    {
        public static string Get(string username, string token, string url)
        {
            //Uri uri = new Uri(url);
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.ContentType = "application/json";

            string userNamePassword = username + ":" + token;
            request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(userNamePassword)));

            string responseString = null;
            try
            {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    //Console.WriteLine("***" + response.StatusCode.ToString());
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    responseString = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (WebException ex)
            {
                responseString = ex.Message;
            }

            return responseString;
        }

        public static byte[] GetBytes(string username, string token, string url)
        {
            //Uri uri = new Uri(url);
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.ContentType = "application/json";

            string userNamePassword = username + ":" + token;
            request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(userNamePassword)));

            byte[] responseBytes = null;
            try
            {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    //Console.WriteLine("***" + response.StatusCode.ToString());
                    Stream stream = response.GetResponseStream();
                    long contentLength = response.ContentLength;
                    responseBytes = new byte[contentLength];
                    responseBytes = ReadStreamFully(stream);
                    stream.Close();
                }
            }
            catch (WebException ex)
            {
                string responseString = ex.Message;
                Console.WriteLine("***" + responseString);
            }

            return responseBytes;
        }

        public static string PostBytes(string username, string token, string url, byte[] data)
        {
           // Uri uri = new Uri(url);
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/octet-stream";

            string userNamePassword = username + ":" + token;
            request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(userNamePassword)));

            request.ContentLength = data.Length;
            Stream reqStream = request.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
            reqStream.Close();

            string responseString = null;
            try
            {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    Console.WriteLine("***" + response.StatusCode.ToString());
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    responseString = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (WebException ex)
            {
                responseString = ex.Message;
            }
            return responseString;
        }

        public static string Post(string username, string token, string url, string data)
        {
            //Uri uri = new Uri(url);
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/json";

            string userNamePassword = username + ":" + token;
            request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(userNamePassword)));

            byte[] bs = Encoding.UTF8.GetBytes(data);
            Stream reqStream = request.GetRequestStream();
            reqStream.Write(bs, 0, bs.Length);
            reqStream.Close();

            string responseString = null;
            try
            {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    Console.WriteLine("***" + response.StatusCode.ToString());
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    responseString = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (WebException ex)
            {
                responseString = ex.Message;
            }
            return responseString;
        }

        public static string Put(string username, string token, string url, string data)
        {
           // Uri uri = new Uri(url);
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "PUT";
            request.ContentType = "application/json";

            string userNamePassword = username + ":" + token;
            request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(userNamePassword)));

            byte[] bs = Encoding.UTF8.GetBytes(data);
            Stream reqStream = request.GetRequestStream();
            reqStream.Write(bs, 0, bs.Length);
            reqStream.Close();

            string responseString = null;
            try
            {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    //Console.WriteLine("***" + response.StatusCode.ToString());
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    responseString = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (WebException ex)
            {
                responseString = ex.Message;
            }
            return responseString;
        }

        public static string Delete(string username, string token, string url)
        {
           // Uri uri = new Uri(url);
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "DELETE";
            request.ContentType = "application/json";

            string userNamePassword = username + ":" + token;
            request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(userNamePassword)));

            string responseString = null;
            try
            {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    //Console.WriteLine("***" + response.StatusCode.ToString());
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    responseString = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (WebException ex)
            {
                responseString = ex.Message;
            }
            return responseString;
        }

        static byte[] ReadStreamFully(Stream stream)
        {
            byte[] buffer = new byte[128];
            using (MemoryStream ms = new MemoryStream())
            {
                while (true)
                {
                    int read = stream.Read(buffer, 0, buffer.Length);
                    if (read <= 0)
                        return ms.ToArray();
                    ms.Write(buffer, 0, read);
                }
            }
        }

        public static byte[] File2Bytes(string path)
        {
            if (!File.Exists(path))
            {
                return new byte[0];
            }

            FileInfo fi = new FileInfo(path);
            byte[] buff = new byte[fi.Length];

            FileStream fs = fi.OpenRead();
            fs.Read(buff, 0, Convert.ToInt32(fs.Length));
            fs.Close();

            return buff;
        }

        public static void Bytes2File(byte[] buff, string savepath)
        {
            if (buff == null)
            {
                return;
            }
            if (File.Exists(savepath))
            {
                File.Delete(savepath);
            }

            FileStream fs = new FileStream(savepath, FileMode.CreateNew);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(buff, 0, buff.Length);
            bw.Close();
            fs.Close();
        }
    }
}
