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

namespace Baosight.HBase
{
    class Token
    {
        public string token { get; set; }
        public string errcode { get; set; }
    }
    class OperationType
    {
        public static string token = "token";    //权限认证
        public static string table = "table";    //表操作
        public static string record = "record";  //记录操作
        public static string index = "index";    //索引操作
        public static string metrics = "metrics";//监控操作
    }

    public class HbaseUnit
    {
        private string username = "";
        private string password = "";
        private string urlhead = "";

        private Token token;


        public HbaseUnit(string username, string password, string urlhead)
        {
            ReInit(username, password, urlhead);
        }
        public void ReInit(string username, string password, string urlhead)
        {
            Console.WriteLine("******************************init******************************");
            this.username = username;
            this.password = password;
            this.urlhead = urlhead;
            ReGetToken();
        }

        private string ReGetToken()
        {
            string retString = Request4Json.Get(username, password, urlhead + OperationType.token);
            if (retString == null)
            {
                Console.WriteLine("can't get token");
                return null;
            }
            Console.WriteLine("token>get token string{0}\n", retString);

            JsonSerializer serializer = new JsonSerializer();
            StringReader sr = new StringReader(retString);
            token = (Token)serializer.Deserialize(new JsonTextReader(sr), typeof(Token));
            if (token == null)
            {
                Console.WriteLine("token is null");
                return null;
            }
            Console.WriteLine("token>{0}\n", token.token);
            return token.token;
        }

        public string Get(string operationUrl)
        {
            if (this.token.token == null || this.token.token == "")
            {
                if (ReGetToken() == null)
                {
                    return "can't get token";
                }
            }
            return Request4Json.Get(username, this.token.token, this.urlhead + operationUrl);
        }

        public byte[] GetBytes(string operationUrl)
        {
            if (this.token.token == null || this.token.token == "")
            {
                if (ReGetToken() == null)
                {
                    return null;
                }
            }
            return Request4Json.GetBytes(username, this.token.token, this.urlhead + operationUrl);
        }



        public string PostBytes(string operationUrl, byte[] data)
        {
            if (this.token.token == null || this.token.token == "")
            {
                if (ReGetToken() == null)
                {
                    return "can't get token";
                }
            }
            return Request4Json.PostBytes(this.username, this.token.token, this.urlhead + operationUrl, data);
        }

        public string Post(string operationUrl, string data)
        {
            if (this.token.token == null || this.token.token == "")
            {
                if (ReGetToken() == null)
                {
                    return "can't get token";
                }
            }
            return Request4Json.Post(this.username, this.token.token, this.urlhead + operationUrl, data);
        }

        public string Put(string operationUrl, string data)
        {
            if (this.token.token == null || this.token.token == "")
            {
                if (ReGetToken() == null)
                {
                    return "can't get token";
                }
            }
            return Request4Json.Put(this.username, this.token.token, this.urlhead + operationUrl, data); ;
        }

        public string Delete(string operationUrl)
        {
            if (this.token.token == null || this.token.token == "")
            {
                if (ReGetToken() == null)
                {
                    return "can't get token";
                }
            }
            return Request4Json.Delete(this.username, this.token.token, this.urlhead + operationUrl);
        }

        public byte[] File2Bytes(string path)
        {
            return Request4Json.File2Bytes(path);
        }

        public void Bytes2File(byte[] buff, string path)
        {
            Request4Json.Bytes2File(buff, path);
        }
    }
}
