package services

import (
	"errors"
	"fmt"
	alimt20181012 "github.com/alibabacloud-go/alimt-20181012/v2/client"
	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
)

/**
* 使用STS鉴权方式初始化账号Client，推荐此方式。本示例默认使用AK&SK方式。
* @param accessKeyId
* @param accessKeySecret
* @param securityToken
* @return Client
* @throws Exception
 */
func CreateClientWithSTS (accessKeyId *string, accessKeySecret *string, securityToken *string) (_result *alimt20181012.Client, _err error) {
	config := &openapi.Config{
		// 必填，您的 AccessKey ID
		AccessKeyId: accessKeyId,
		// 必填，您的 AccessKey Secret
		AccessKeySecret: accessKeySecret,
		// 必填，您的 Security Token
		SecurityToken: securityToken,
		// 必填，表明使用 STS 方式
		Type: tea.String("sts"),
	}
	// 访问的域名
	config.Endpoint = tea.String("mt.cn-hangzhou.aliyuncs.com")
	_result = &alimt20181012.Client{}
	_result, _err = alimt20181012.NewClient(config)
	return _result, _err
}

func handler (accessKeyId, accessKeySecret, stsToken *string, content string) (resp *alimt20181012.TranslateGeneralResponseBody, _err error) {
	// 初始化 Client，采用 AK&SK 鉴权访问的方式，此方式可能会存在泄漏风险，建议使用 STS 方式。鉴权访问方式请参考：https://help.aliyun.com/document_detail/378661.html
	//client, _err := CreateClient(accessKeyId, accessKeySecret)
	client, _err := CreateClientWithSTS(accessKeyId, accessKeySecret, stsToken)
	if _err != nil {
		_err = errors.New(fmt.Sprintf("创建翻译客户端失败 err: %v", _err))
		return
	}

	translateGeneralRequest := &alimt20181012.TranslateGeneralRequest{
		FormatType: tea.String("text"),
		SourceLanguage: tea.String("zh"),
		TargetLanguage: tea.String("en"),
		SourceText: tea.String(content),
		Scene: tea.String("general"),
	}
	runtime := &util.RuntimeOptions{}
	ret, tryErr := func()(ret *alimt20181012.TranslateGeneralResponse, _e error) {
		defer func() {
			if r := tea.Recover(recover()); r != nil {
				_e = r
				return
			}
		}()
		// 复制代码运行请自行打印 API 的返回值
		ret, _e = client.TranslateGeneralWithOptions(translateGeneralRequest, runtime)
		if _e != nil {
			return
		}
		return
	}()


	if tryErr != nil {
		var e = &tea.SDKError{}
		if _t, ok := tryErr.(*tea.SDKError); ok {
			e = _t
		} else {
			e.Message = tea.String(tryErr.Error())
		}
		// 如有需要，请打印 e
		_, _err = util.AssertAsString(e.Message)
		if _err != nil {
			_err =  errors.New(fmt.Sprintf("翻译失败 err: %v", _err))
			return
		}
		_err =  errors.New(fmt.Sprintf("翻译失败 err: %v", _err))
		return
	}
	if *ret.StatusCode != 200 {
		_err = errors.New(fmt.Sprintf(" %v", ret.StatusCode))
		return
	}
	resp = ret.Body
	return
}


func AliTranslate(content string) (contentEn string, err error) {
	stsToken, err := GetOssSTSToken()
	if err != nil {
		err = errors.New(fmt.Sprintf("阿里云机器翻译失败 err %v", err))
		return
	}
	fmt.Println(stsToken)
	resp, err := handler(&stsToken.AccessKeyId, &stsToken.AccessKeySecret, &stsToken.SecurityToken, content)
	if err != nil {
		err = errors.New(fmt.Sprintf("阿里云机器翻译失败 err %v", err))
		return
	}
	if *resp.Code != 200 {
		err = errors.New(fmt.Sprintf("阿里云机器翻译失败 code: %v; msg: %v",*resp.Code, *resp.Message))
		return
	}

	contentEn = *resp.Data.Translated
	return
}