package service

import (
	"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
	"openapi/pkg/constant"
	protocol "openapi/pkg/domain"
	"openapi/pkg/infrastructure/aliyun"
	"openapi/pkg/infrastructure/utils"
)

//创建视频上传凭证
func CreateUploadVideo(header *protocol.RequestHeader, request *aliyun.CreateUploadVideoRequest) (rsp *aliyun.CreateUploadVideoResponse, err error) {
	var (
		up       aliyun.UploadAddress
		filePath string
		fileName = request.FileName
	)
	defer func() {
		err = ErrorHandler(err)
	}()
	client, e := aliyun.DefaultVodClient()
	if e != nil {
		log.Error(e)
		err = e
		return
	}
	if aliyun.ValidFileExt(aliyun.VideoFileExt, request.FileName) {
		filePath = utils.GetFileName(header.AppProject, aliyun.FileVideo, request.FileName)
	} else if aliyun.ValidFileExt(aliyun.VoiceFileExt, request.FileName) {
		filePath = utils.GetFileName(header.AppProject, aliyun.FileVoice, request.FileName)
	} else {
		err = aliyun.OssErrFileFormat
		return
	}
	request.FileName = filePath
	request.Title = filePath
	rsp, err = aliyun.CreateUploadVideo(client, request)
	if err != nil {
		log.Error(err)
		return
	}
	if up, err = aliyun.ParseUploadAddress(rsp.UploadAddress); err != nil {
		log.Error(err)
		return
	}
	rsp.FileName = fileName
	rsp.FileURL = up.GetFileUrl(constant.CName)
	return
}

//创建视频上传凭证
func RefreshUploadVideo(header *protocol.RequestHeader, request *aliyun.RefreshUploadVideoRequest) (rsp *aliyun.RefreshUploadVideoResponse, err error) {
	var ()
	client, e := aliyun.DefaultVodClient()
	if e != nil {
		log.Error(e)
		err = e
		return
	}
	rsp, err = aliyun.RefreshUploadVideo(client, request)
	if err != nil {
		log.Error(err)
	}
	return
}

//创建图片上传凭证
func CreateUploadImage(header *protocol.RequestHeader, request *aliyun.CreateUploadImageRequest) (rsp *aliyun.CreateUploadImageResponse, err error) {
	var ()
	client, e := aliyun.DefaultVodClient()
	if e != nil {
		log.Error(e)
		err = e
		return
	}
	if len(request.FileName) == 0 {
		request.FileName = aliyun.DefaultImageFileName
	}
	if !aliyun.ValidFileExt(aliyun.ImageFileExt, request.FileName) {
		return nil, protocol.NewCustomMessage(1, aliyun.OssErrFileFormat.Error())
	}
	request.Title = utils.GetFileName(header.AppProject, aliyun.FileImage, request.FileName)
	rsp, err = aliyun.CreateUploadImage(client, request)
	if err != nil {
		log.Error(err)
		return
	}
	rsp.FileURL = rsp.ImageURL
	rsp.FileName = request.FileName
	return
}

//获取视频播放地址
func GetPlayInfo(header *protocol.RequestHeader, request *aliyun.GetPlayInfoRequest) (rsp interface{}, err error) {
	var ()
	client, e := aliyun.DefaultVodClient()
	if e != nil {
		log.Error(e)
		err = e
		return
	}
	rsp, err = aliyun.GetPlayInfo(client, request)
	if err != nil {
		log.Error(err)
	}
	return
}

//获取视频播放凭证
func GetVideoPlayAuth(header *protocol.RequestHeader, request *aliyun.GetVideoPlayAuthRequest) (rsp interface{}, err error) {
	var ()
	client, e := aliyun.DefaultVodClient()
	if e != nil {
		log.Error(e)
		err = e
		return
	}
	rsp, err = aliyun.GetGetVideoPlayAuth(client, request)
	if err != nil {
		log.Error(err)
	}
	return
}