using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;

namespace LHDN.CRM.Test
{
    [TestClass]
    public class ContactCentreTest
    {
        [TestMethod]
        public void TestLanguageOut()
        {

            //string URL = "http://172.20.229.26:8090/api/CallingCentre/TaxPayer/Language?phoneno=0122446181";
            string URL = "http://localhost:9088/api/CallingCentre/TaxPayer/Language?phoneno=0138220635";
            //string DATA = JsonConvert.SerializeObject(model);

            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(URL);

            //Generate Api Key
            string strReqtimestamp = DateTime.Now.ToString();
            string secretKey = string.Empty;
            string sH = string.Empty;
            secretKey = "doremifaso" + strReqtimestamp;
            sH = SHA1(secretKey);


            client.DefaultRequestHeaders.Add("_apikey", sH);
            client.DefaultRequestHeaders.Add("_reqtimestamp", strReqtimestamp);

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // System.Net.Http.HttpContent content = new StringContent(DATA, UTF8Encoding.UTF8, "application/json");
            //HttpResponseMessage messge = client.GetAsync(URL, content).Result;
            HttpResponseMessage messge = client.GetAsync(URL).Result;
            string description = string.Empty;
            if (messge.IsSuccessStatusCode)
            {
                string result = messge.Content.ReadAsStringAsync().Result;
                description = result;
            }
            else
            {
                if (messge.StatusCode == System.Net.HttpStatusCode.BadRequest) //No Record Found or Duplicate Record
                {
                    var ErrMsg = JsonConvert.DeserializeObject<dynamic>(messge.Content.ReadAsStringAsync().Result);
                    Console.WriteLine(ErrMsg.Message);

                }
                else //Invalid API Key
                {
                    Console.WriteLine(messge.Content.ReadAsStringAsync().Result);
                }

                Console.ReadLine();
            }

            client.Dispose();
        }


        private string SHA1(string sIn)
        {
            System.Security.Cryptography.SHA1CryptoServiceProvider sha1Obj = new System.Security.Cryptography.SHA1CryptoServiceProvider();
            Byte[] bytesToHash = System.Text.Encoding.ASCII.GetBytes(sIn);
            string strResult = string.Empty;
            bytesToHash = sha1Obj.ComputeHash(bytesToHash);

            foreach (byte b in bytesToHash)
            {
                strResult += b.ToString("x2");
            }
            return strResult;
        }
    }
}
