/*
Copyright 2018 Huawei Technologies Co.,Ltd.

sdk reference linking：https://support.huaweicloud.com/sdkreference-ocr/ocr_04_0016.html
**/

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();

            //AutoClassificationTokenRequst();
        }

        # 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 = "cn-north-4"; //cn-north-1,cn-east-2 etc.
            string api = "/v1.0/ocr/id-card";
            //the fullpath of the image to the recognized or web image url
            string imgPath = "./../../data/id-card-demo.jpg";

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

            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.cn-north-4.myhuaweicloud.com";  //http endpoint information
            string api = "/v1.0/ocr/id-card";
            //the fullpath of the image to the recognized or web image url
            string imgPath = "./../../data/id-card-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("side", "front");

            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

        static void AutoClassificationTokenRequst()
        {
            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 = "cn-north-4"; //cn-north-1,cn-east-2 etc.
            string api = "/v1.0/ocr/auto-classification";
            //the fullpath of the image to the recognized or web image url
            string imgPath = "./../../data/vat-invoice-demo.jpg";

            Dictionary<string, Object> param = new Dictionary<string, Object>();
            string[] list_type = { "vat_invoice", "id_card_portrait_side" };
            param.Add("type_list", list_type);

            string body = "";
            try
            {
                OcrClientToken res = new OcrClientToken(userName, passWord, domainName, regionName);
                string StatusCode = res.OcrServiceBase64(api, imgPath, param, ref body);
                
                if ("OK" == StatusCode || "200" == StatusCode)
                {
                    decodeRespose(body);
                } else
                {
                    Console.WriteLine("response = " + body);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught: {0}", ex);
            }
        }

        static void decodeRespose(string respose)
        {
            JObject responseDict = (JObject)JsonConvert.DeserializeObject(respose);
            Console.WriteLine("resposeDict = {0}", responseDict);

            JArray resultArr = responseDict.Value<JArray>("result");
            foreach (var item in resultArr)
            {
                JObject contentDict = item.Value<JObject>("content");
                string type = item.Value<string>("type");
                if("id_card_portrait_side" == type)
                {
                    string name = contentDict.Value<string>("name");
                    string sex = contentDict.Value<string>("sex");
                    string ethnicity = contentDict.Value<string>("ethnicity");
                    string birth = contentDict.Value<string>("birth");
                    string address = contentDict.Value<string>("address");
                    string number = contentDict.Value<string>("number");

                    Console.WriteLine("name = " + name + "\nsex = " + sex + "\nethnicity = " + ethnicity);
                }
                else if ("vat_invoice" == type)
                {
                    string itemtype = contentDict.Value<string>("type");
                    string seller_address = contentDict.Value<string>("seller_address");
                    string attribution = contentDict.Value<string>("attribution");
                    string code = contentDict.Value<string>("code");
                    string check_code = contentDict.Value<string>("check_code");
                    string machine_number = contentDict.Value<string>("machine_number");
                    string print_number = contentDict.Value<string>("print_number");
                    string number = contentDict.Value<string>("number");
                    string issue_date = contentDict.Value<string>("issue_date");
                    string encryption_block = contentDict.Value<string>("encryption_block");
                    string buyer_name = contentDict.Value<string>("buyer_name");
                    string buyer_id = contentDict.Value<string>("buyer_id");
                    string buyer_address = contentDict.Value<string>("buyer_address");
                    string buyer_bank = contentDict.Value<string>("buyer_bank");
                    string seller_name = contentDict.Value<string>("seller_name");
                    string seller_id = contentDict.Value<string>("seller_id");
                    string subtotal_amount = contentDict.Value<string>("subtotal_amount");
                    string seller_bank = contentDict.Value<string>("seller_bank");
                    string subtotal_tax = contentDict.Value<string>("subtotal_tax");
                    string total = contentDict.Value<string>("total");
                    string total_in_words = contentDict.Value<string>("total_in_words");
                    string remarks = contentDict.Value<string>("remarks");
                    string receiver = contentDict.Value<string>("receiver");
                    string issuer = contentDict.Value<string>("issuer");

                    JArray supervision_seal = contentDict.Value<JArray>("supervision_seal");
                    JArray seller_seal = contentDict.Value<JArray>("seller_seal");
                    JArray item_list = contentDict.Value<JArray>("item_list");

                    Console.WriteLine("itemtype = " + itemtype + "\nseller_address = " + seller_address);
                }
            }
        }
    }
}
