package services

import (
	"fmt"

	asr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/asr/v20190614"
	"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"
)

type TencentRecTaskReq struct {
	SecretId    string
	SecretKey   string
	FileUrl     string
	CallbackUrl string
}

// TencentCreateRecTask 创建录音文件识别
func TencentCreateRecTask(req TencentRecTaskReq) (taskId int, err error) {
	if req.SecretId == "" || req.SecretKey == "" || req.FileUrl == "" {
		err = fmt.Errorf("参数有误, SecretId: %s, SecretKey: %s, FileUrl: %s", req.SecretId, req.SecretKey, req.FileUrl)
		return
	}
	credential := common.NewCredential(
		req.SecretId,
		req.SecretKey,
	)
	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = "asr.tencentcloudapi.com"
	client, e := asr.NewClient(credential, "", cpf)
	if e != nil {
		err = fmt.Errorf("asr NewClient err: %s", e.Error())
		return
	}

	// 实例化一个请求对象, 具体参数看文档https://cloud.tencent.com/document/product/1093/37823
	request := asr.NewCreateRecTaskRequest()
	request.EngineModelType = common.StringPtr("16k_zh")
	request.ChannelNum = common.Uint64Ptr(1)
	request.ResTextFormat = common.Uint64Ptr(2)
	request.SourceType = common.Uint64Ptr(0)
	request.Url = common.StringPtr(req.FileUrl)
	if req.CallbackUrl != "" {
		request.CallbackUrl = common.StringPtr(req.CallbackUrl)
	}
	request.SpeakerDiarization = common.Int64Ptr(1)
	request.SpeakerNumber = common.Int64Ptr(0)
	request.ConvertNumMode = common.Int64Ptr(0)
	request.FilterDirty = common.Int64Ptr(2)
	request.FilterPunc = common.Int64Ptr(0)
	request.FilterModal = common.Int64Ptr(2)

	// 返回的resp是一个CreateRecTaskResponse的实例
	response, e := client.CreateRecTask(request)
	if _, ok := e.(*errors.TencentCloudSDKError); ok {
		err = fmt.Errorf("asr CreateRecTask err: %s", e.Error())
		return
	}
	// 唯一标识, 回调会用到
	taskId = int(*response.Response.Data.TaskId)
	//fmt.Printf("%s", response.ToJsonString())
	return
}

// TencentRecTaskCallback 录音识别回调参数
type TencentRecTaskCallback struct {
	Code         int     `description:"任务状态码，0为成功，其他：失败；详见https://cloud.tencent.com/document/product/1093/52632"`
	Message      string  `description:"失败原因文字描述，成功时此值为空"`
	RequestId    uint64  `json:"requestId" description:"任务唯一标识，与录音识别请求中返回的TaskId一致。数据格式必须设置为Uint64"`
	Appid        uint64  `description:"腾讯云应用ID"`
	ProjectId    int     `json:"projectid" description:"腾讯云项目ID"`
	AudioUrl     string  `json:"audioUrl" description:"语音URL，如创建任务时为上传数据的方式，则不包含该字段"`
	Text         string  `description:"识别出的结果文本"`
	ResultDetail string  `json:"resultDetail" description:"包含详细识别结果，如创建任务时 ResTextFormat 为0，则不包含该字段"`
	AudioTime    float64 `json:"audioTime" description:"语音总时长"`
}

// TencentRecTaskCallbackResp 录音识别回调响应体
type TencentRecTaskCallbackResp struct {
	Code    int    `description:"0为成功, 其他值代表失败"`
	Message string `description:"失败原因说明"`
}

// TencentRecTaskSentenceDetail 录音识别相应结果-单句详情
type TencentRecTaskSentenceDetail struct {
	FinalSentence string `description:"单句最终结果"`
	StartMs       int    `description:"单句开始时间（毫秒）"`
	EndMs         int    `description:"单句结束时间（毫秒）"`
}
