package tencent

import (
	"fmt"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
	ocr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ocr/v20181119"
)

type tencent struct {
	secretId   string
	secretKey  string
	credential *common.Credential
}

var Tencent = tencent{}

func (that *tencent) Init(secretId, secretKey string) {
	// 云市场分配的密钥Id
	//secretId := "xxxx"
	//// 云市场分配的密钥Key
	//secretKey := "xxxx"
	that.secretId = secretId
	that.secretKey = secretKey
	that.credential = common.NewCredential(
		that.secretId,
		that.secretKey,
	)
}

func (that *tencent) OCRCOMPANY(base64Str string) string {

	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = "ocr.tencentcloudapi.com"
	client, _ := ocr.NewClient(that.credential, "ap-guangzhou", cpf)

	request := ocr.NewBizLicenseOCRRequest()

	//request.ImageUrl = common.StringPtr("https://img0.baidu.com/it/u=2041013181,3227632688&fm=26&fmt=auto")
	request.ImageBase64 = common.StringPtr(base64Str)

	response, err := client.BizLicenseOCR(request)
	if _, ok := err.(*errors.TencentCloudSDKError); ok {
		fmt.Println("An API error has returned: %s", err)
		return ""
	}
	if err != nil {
		fmt.Println("An API error has returned: %s", err)
		return ""
	}
	//fmt.Printf("%s", response.ToJsonString())

	return response.ToJsonString()
}

func (that *tencent) OCR(base64Str string) string {

	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = "ocr.tencentcloudapi.com"
	client, _ := ocr.NewClient(that.credential, "ap-guangzhou", cpf)

	request := ocr.NewGeneralAccurateOCRRequest()

	//request.ImageUrl = common.StringPtr("https://img0.baidu.com/it/u=2041013181,3227632688&fm=26&fmt=auto")
	request.ImageBase64 = common.StringPtr(base64Str)

	response, err := client.GeneralAccurateOCR(request)
	if _, ok := err.(*errors.TencentCloudSDKError); ok {
		fmt.Println("An API error has returned: %s", err)
		return ""
	}
	if err != nil {
		fmt.Println("An API error has returned: %s", err)
		return ""
	}
	//fmt.Printf("%s", response.ToJsonString())

	return response.ToJsonString()
}

func (that *tencent) Qrcode(base64Str string) string {

	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = "ocr.tencentcloudapi.com"
	client, _ := ocr.NewClient(that.credential, "ap-guangzhou", cpf)

	request := ocr.NewQrcodeOCRRequest()

	request.ImageBase64 = common.StringPtr(base64Str)

	response, err := client.QrcodeOCR(request)
	if _, ok := err.(*errors.TencentCloudSDKError); ok {
		fmt.Println("An API error has returned: %s", err)
		return ""
	}
	if err != nil {
		fmt.Println("An API error has returned: %s", err)
		return ""
	}
	//fmt.Printf("%s", response.ToJsonString())

	return response.ToJsonString()
}
