#include <iostream>
#include <string>
#include <curl/curl.h>

#include "HWOcrClientToken.h"	// Token��֤
#include "HWOcrClientAKSK.h"	// AK/SK��֤

#define MAX_SIZE 100

void tokenRequest();
void akskRequest();
void autoClassificationTokenRequest();
void DecodeResponse(std::string resData);

int main()
{
	// Token auth for ocr service
	tokenRequest();

	// aksk auth for ocr service
	akskRequest();

	// Token auth for auto-classification service
	autoClassificationTokenRequest();

	system("pause");
	return 0;
}

void tokenRequest() {
	// Token for request type
	std::string username = "xxx";
	std::string password = "xxx";
	std::string domainname = "xxx"; //if the user isn't iam user, domain_name is the same with username
	std::string region = "cn-north-4";   //cn - north - 1, cn - east - 2 etc.
	std::string httpUri = "/v1.0/ocr/id-card";
	std::string imgPath = "./data/id-card-demo.jpg"; //the file path of the image or url of image to be recognized

	HWOcrClientToken ocrclient;
	ocrclient.init(domainname, username, password, region);
	Json::Value jsonOption;
	std::string strRespData1 = ocrclient.ocr_post(imgPath, httpUri, jsonOption);
	std::cout << strRespData1 << std::endl;
}

void akskRequest() {
	// AK/SK for request type
	std::string strak = "xxx"; //ak from authentication
	std::string strsk = "xxx"; //sk from authentication
	std::string struri = "/v1.0/ocr/id-card"; //the uri for the http request to be called
	std::string imgPath = "./data/id-card-demo.jpg"; //the file path of the image or url of image to be recognized
	std::string region = "cn-north-4"; //the region selection

	HWOcrClientAKSK client_aksk;
	client_aksk.init(strak, strsk, struri, region); //initialize ocr_client from //ak,sk,uri,region and endpoint information
	std::string strrespdata2 = client_aksk.request_ocr_service_base64(struri, imgPath);
	std::cout << strrespdata2 << std::endl;
}

void autoClassificationTokenRequest() {
	// Token for request type
	std::string username = "xxx";
	std::string password = "xxx";
	std::string domainname = "xxx"; //if the user isn't iam user, domain_name is the same with username
	std::string region = "cn-north-4";   //cn - north - 1, cn - east - 2 etc.
	std::string httpUri = "/v1.0/ocr/auto-classification";
	std::string imgPath = "./data/vat-invoice-demo.jpg"; //the file path of the image or url of image to be recognized

	HWOcrClientToken ocrclient;
	ocrclient.init(domainname, username, password, region);
	Json::Value jsonOption;
	Json::Value list;
	list.append("vat_invoice");
	list.append("id_card_portrait_side");
	jsonOption["type_list"] = Json::Value(list);

	std::string resData = ocrclient.ocr_post(imgPath, httpUri, jsonOption);
	std::cout << resData << std::endl;

	DecodeResponse(resData);
}

void DecodeResponse(std::string resData) {
	// data analysis
	Json::CharReaderBuilder builder;
	Json::CharReader* reader(builder.newCharReader());
	Json::Value jsonData;
	JSONCPP_STRING err;
	bool isSuccess = reader->parse(resData.data(), resData.data() + resData.size(), &jsonData, &err);
	if (isSuccess) {
		long status = jsonData["status"].asInt();
		if (status == 200) {
			Json::Value jsonBody = jsonData["response body"];
			Json::Value jsonResultArr = jsonBody["result"];
			for (int i = 0; i < jsonResultArr.size(); i++)
			{
				Json::Value resultItem = jsonResultArr[i];
				Json::Value content = resultItem["content"];
				std::string type = resultItem["type"].asString();
				std::cout << "resultItem = " << resultItem << std::endl;
				std::cout << "content = " << content << std::endl;

				if ("id_card_portrait_side" == type) {
					std::string name = content["name"].asString();
					std::string sex = content["sex"].asString();
					std::string ethnicity = content["ethnicity"].asString();
					std::string birth = content["birth"].asString();
					std::string address = content["address"].asString();
					std::string number = content["number"].asString();
				}
				else if ("vat_invoice" == type) {
					std::string contentType = content["type"].asString();
					std::string serial_number = content["serial_number"].asString();
					std::string attribution = content["attribution"].asString();
					std::string code = content["code"].asString();
					std::string check_code = content["check_code"].asString();
					std::string machine_number = content["machine_number"].asString();
					std::string print_number = content["print_number"].asString();
					std::string number = content["number"].asString();
					std::string issue_date = content["issue_date"].asString();
					std::string encryption_block = content["encryption_block"].asString();
					std::string buyer_name = content["buyer_name"].asString();
					std::string buyer_id = content["buyer_id"].asString();
					std::string buyer_address = content["buyer_address"].asString();
					std::string buyer_bank = content["buyer_bank"].asString();
					std::string seller_name = content["seller_name"].asString();
					std::string seller_id = content["seller_id"].asString();
					std::string seller_address = content["seller_address"].asString();
					std::string seller_bank = content["seller_bank"].asString();
					std::string subtotal_amount = content["subtotal_amount"].asString();
					std::string subtotal_tax = content["subtotal_tax"].asString();
					std::string total = content["total"].asString();
					std::string total_in_words = content["total_in_words"].asString();
					std::string remarks = content["remarks"].asString();
					std::string receiver = content["receiver"].asString();
					std::string issuer = content["issuer"].asString();

					std::cout << "contentType = " << contentType << std::endl;
					std::cout << "serial_number = " << serial_number << std::endl;

					Json::Value supervision_seal = content["supervision_seal"];
					Json::Value seller_seal = content["seller_seal"];
					Json::Value item_list = content["item_list"];
					for (int i = 0; i < item_list.size(); i++) {
						Json::Value listItem = item_list[i];
						std::cout << "listItem = " << listItem << std::endl;

						std::string name = listItem["name"].asString();
						std::string specification = listItem["specification"].asString();
						std::string unit = listItem["unit"].asString();
						std::string quantity = listItem["quantity"].asString();
						std::string license_plate_number = listItem["license_plate_number"].asString();
						std::string vehicle_type = listItem["vehicle_type"].asString();
						std::string start_date = listItem["start_date"].asString();
						std::string end_date = listItem["end_date"].asString();
						std::string amount = listItem["end_date"].asString();
						std::string tax_rate = listItem["tax_rate"].asString();
						std::string tax = listItem["tax"].asString();

						std::cout << "name = " << name << std::endl;
						std::cout << "unit = " << unit << std::endl;
					}
				}
			}
		}
	}
	else {
		std::cout << "json parse error" << std::endl;
	}
}