/*
Copyright 2018 Huawei Technologies Co.,Ltd.
**/

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


namespace CSHarp_SDK
{
    class OcrDemo
    {
        static void Main()
        {
            TokenRequst();

            //AkskRequst();
        }

        # region Token demo code
        static void TokenRequst()
        {
            string domainName = "xxx";  //if the user isn't IAM user, domain_name is the same with username
            string userName = "xxx";
            string passWord = "xxx";
            string regionName = "ap-southeast-1"; //cn-north-1,cn-east-2 etc.
            string api = "/v1.0/ocr/passport";
            //the fullpath of the image to the recognized or web image url
            string imgPath = "./../../data/passport-demo.jpg";

            Dictionary<string, Object> param = new Dictionary<string, Object>();
            //param.Add("country_code", "GENERAL");

            string body = "";
            try
            {
                OcrClientToken res = new OcrClientToken(userName, passWord, domainName, regionName);
                string StatusCode = res.OcrServiceBase64(api, imgPath, param, ref body);
                Console.WriteLine("token_result: " + body);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught: {0}", ex);
            }
        }
        # endregion

        # region AKSK demo code
        static void AkskRequst()
        {
            string ak = "xxx";  //AK from authentication
            string sk = "xxx";  //SK from authentication
            string endPoint = "ocr.ap-southeast-1.myhuaweicloud.com";  //http endpoint information
            string api = "/v1.0/ocr/passport";
            //the fullpath of the image to the recognized or web image url
            string imgPath = "./../../data/passport-demo.jpg";

            HWOcrClientAKSK ocrClient = new HWOcrClientAKSK(ak, sk, endPoint);  //initialize ocr_client from ak,sk and endpoint information
            Dictionary<string, Object> option = new Dictionary<string, Object>();
            //option.Add("country_code", "GENERAL");

            try
            {
                //call OCR interface to recognize picture
                HttpWebResponse response = ocrClient.RequestOcrAkSkService(api, imgPath, option);
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                Console.WriteLine("aksk_result: " + reader.ReadToEnd());
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught: {0}", ex);
            }
        }
        #endregion
    }
}
