package v1

import (
	"encoding/json"
	"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
	"opp/controllers"
	"opp/internal/aliyun"
	"opp/protocol"
	"opp/services/upload"
)

type VodController struct {
	controllers.BaseController
}

//创建视频上传凭证 CreateUploadVideo
// @router /createUploadVideo [post]
func (this *VodController) CreateUploadVideo() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *aliyun.CreateUploadVideoRequest
	if err := json.Unmarshal(this.ByteBody, &request); err != nil {
		log.Error(err)
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	if len(request.FileName) == 0 {
		request.FileName = aliyun.DefaultVideoFileName
	}
	header := controllers.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(upload.CreateUploadVideo(header, request))
}

//刷新视频上传凭证 RefreshUploadVideo
// @router /refreshUploadVideo [post]
func (this *VodController) RefreshUploadVideo() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *aliyun.RefreshUploadVideoRequest
	if err := json.Unmarshal(this.ByteBody, &request); err != nil {
		log.Error(err)
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	header := controllers.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(upload.RefreshUploadVideo(header, request))
}

//创建图片上传凭证 CreateUploadImage
// @router /createUploadImage [post]
func (this *VodController) CreateUploadImage() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *aliyun.CreateUploadImageRequest
	if err := json.Unmarshal(this.ByteBody, &request); err != nil {
		log.Error(err)
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	header := controllers.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(upload.CreateUploadImage(header, request))
}

//创建图片上传凭证 CreateUploadImages
// @router /createUploadImages [post]
func (this *VodController) CreateUploadImages() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var (
		request  *aliyun.CreateUploadImagesRequest
		response *aliyun.CreateUploadImagesResponse = &aliyun.CreateUploadImagesResponse{
			List: make([]*aliyun.CreateUploadImageResponse, 0),
		}
	)
	if err := json.Unmarshal(this.ByteBody, &request); err != nil {
		log.Error(err)
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	var err error
	if len(request.Items) == 0 {
		msg = protocol.BadRequestParam(2)
	}
	header := controllers.GetRequestHeader(this.Ctx)
	for i := range request.Items {
		r := request.Items[i]
		if rsp, e := upload.CreateUploadImage(header, r); e != nil {
			err = e
			break
		} else {
			response.List = append(response.List, rsp)
		}
	}
	msg = protocol.NewReturnResponse(response, err)
}

//获取视频播放地址 GetPlayInfo
// @router /getPlayInfo [post]
func (this *VodController) GetPlayInfo() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *aliyun.GetPlayInfoRequest
	if err := json.Unmarshal(this.ByteBody, &request); err != nil {
		log.Error(err)
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	header := controllers.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(upload.GetPlayInfo(header, request))
}

//获取视频播放凭证 GetVideoPlayAuth
// @router /getVideoPlayAuth [post]
func (this *VodController) GetVideoPlayAuth() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *aliyun.GetVideoPlayAuthRequest
	if err := json.Unmarshal(this.ByteBody, &request); err != nil {
		log.Error(err)
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	header := controllers.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(upload.GetVideoPlayAuth(header, request))
}