package http

import (
	"FollowUp_Notice/conf"
	"FollowUp_Notice/lib"
	"FollowUp_Notice/logs"
	"crypto/hmac"
	"crypto/sha256"
	"encoding/base64"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/go-resty/resty/v2"
	uuid "github.com/satori/go.uuid"
	"strings"
	"time"
)

var (
	//appKey    = conf.VoiceCall_AppKey    // 语音通知应用的appKey,购买服务时下发,请替换为实际值
	//appSecret = conf.VoiceCall_AppSecret // 语音通知应用的appSecret,购买服务时下发,请替换为实际值

	baseUrl   = "https://rtccall.cn-north-1.myhuaweicloud.cn:443"
	appKey    = "Zy8539Sa61Iv5fo5iGNWLcV24KI1"
	appSecret = "KNZ3OFv8eVF7zO5smbr0160WS4Az"
	template  = "1101dc0ebc154a5a9e7c4e5f11bfdb41"
	phone     = "+8675536362854"
)

func buildAKSKHeader(appKey string, appSecret string) string {
	now := time.Now().UTC().Format("2006-01-02T15:04:05Z")                // Created
	nonce := strings.ReplaceAll(fmt.Sprintf("%s", uuid.NewV1()), "-", "") // Nonce
	digest := hmac.New(sha256.New, []byte(appSecret))
	digest.Write([]byte(nonce + now))
	digestBase64 := base64.StdEncoding.EncodeToString(digest.Sum(nil)) // PasswordDigest
	return fmt.Sprintf(`UsernameToken Username="%s",PasswordDigest="%s",Nonce="%s",Created="%s"`, appKey, digestBase64, nonce, now)
}

type PlayInfo struct {
	TemplateID    string   `json:"templateId"`
	TemplateParas []string `json:"templateParas"`
	CollectInd    int      `json:"collectInd"`
}

type VoiceNotifyRequest struct {
	DisplayNbr   string     `json:"displayNbr"`
	CalleeNbr    string     `json:"calleeNbr"`
	PlayInfoList []PlayInfo `json:"playInfoList"`
}

type VoiceRes struct {
	Resultcode string `json:"resultcode"`
	Resultdesc string `json:"resultdesc"`
	SessionId  string `json:"sessionId"`
}

func VoiceNotifyAPI(displayNbr string, calleeNbr string, playInfoList []PlayInfo) (VoiceRes, error) {
	if len(displayNbr) < 1 || len(calleeNbr) < 1 || playInfoList == nil {
		return VoiceRes{}, errors.New("参数错误")
	}
	apiUri := "/rest/httpsessions/callnotify/v2.0" // v1.0 or v2.0
	//requestUrl := conf.VoiceCall_BaseUrl + apiUri
	requestUrl := baseUrl + apiUri

	jsonData := VoiceNotifyRequest{
		DisplayNbr:   displayNbr,
		CalleeNbr:    calleeNbr,
		PlayInfoList: playInfoList,
	}

	//jsonBytes, _ := json.Marshal(jsonData)
	client := resty.New()
	resp, err := client.R().
		SetHeader("Content-Type", "application/json;charset=UTF-8").
		SetHeader("Authorization", `AKSK realm="SDP",profile="UsernameToken",type="Appkey"`).
		SetHeader("X-AKSK", buildAKSKHeader(appKey, appSecret)).
		SetBody(jsonData).
		Post(requestUrl)

	if err != nil {
		return VoiceRes{}, err
	}

	temp := VoiceRes{}
	if err = json.Unmarshal(resp.Body(), &temp); err != nil {
		logs.Error(lib.FuncName(), err)
		return VoiceRes{}, err
	}
	return temp, nil

}

func GetPlayInfoList(templateId string, templateParas []string) []PlayInfo {
	playInfoList := []PlayInfo{
		{
			TemplateID:    templateId,
			TemplateParas: templateParas,
			CollectInd:    0,
		},
	}
	return playInfoList
}

// 满意度调查
func GetSatisfactionPlayInfoList(templateParas []string) []PlayInfo {
	playInfoList := []PlayInfo{
		{
			TemplateID:    conf.VoiceCall_Satisfaction_Template1,
			TemplateParas: templateParas,
			CollectInd:    1,
		},
		{
			TemplateID:    conf.VoiceCall_Satisfaction_Template2,
			TemplateParas: []string{""},
			CollectInd:    1,
		},
		{
			TemplateID:    conf.VoiceCall_Satisfaction_Template3,
			TemplateParas: []string{""},
			CollectInd:    1,
		},
		{
			TemplateID:    conf.VoiceCall_Satisfaction_Template4,
			TemplateParas: []string{""},
			CollectInd:    1,
		},
		{
			TemplateID:    conf.VoiceCall_Satisfaction_Template5,
			TemplateParas: []string{""},
			CollectInd:    1,
		},
	}
	return playInfoList
}
