package mediafiller

import (
	"service/api/consts"
	"service/dbstruct"
	"service/library/logger"

	"github.com/gin-gonic/gin"
)

// todo
func getImageMapByIds(ctx *gin.Context, ids []int64) (map[int64]*dbstruct.Image, error) {
	if len(ids) == 0 {
		return nil, nil
	}
	list, err := defaultMediaFiller.getImageByIds(ctx, ids)
	if err != nil {
		logger.Error("defaultMediaFiller getImageByIds failed : %v", err)
		return nil, err
	}
	imageMap := make(map[int64]*dbstruct.Image, len(list))
	for _, image := range list {
		imageMap[image.Id] = image
	}
	return imageMap, err
}

// todo
func getVideoMapByIds(ctx *gin.Context, ids []int64) (map[int64]*dbstruct.Video, error) {
	if len(ids) == 0 {
		return nil, nil
	}
	list, err := defaultMediaFiller.getVideoByIds(ctx, ids)
	if err != nil {
		logger.Error("defaultMediaFiller getVideoByIds failed : %v", err)
		return nil, err
	}
	videoMap := make(map[int64]*dbstruct.Video, len(list))
	for _, video := range list {
		videoMap[video.Id] = video
	}
	return videoMap, err
}

// todo
func transToCImage(image *dbstruct.Image, isAMember int64) *dbstruct.ToCImage {
	if image == nil {
		return nil
	}

	// 选择内存最小的图片
	imgSrcId := image.SelectMinSizeOssId()

	// 会员可以查看原图
	if isAMember == consts.PermanentMember {
		imgSrcId = ""
	}

	return &dbstruct.ToCImage{
		Id:   image.Id,
		W:    image.W,
		H:    image.H,
		Fmt:  image.Fmt,
		Urls: []string{defaultMediaFiller.GetFileServerDomain() + imgSrcId},
	}
}

// todo
func transToCVideo(video *dbstruct.Video, coverImg *dbstruct.Image) *dbstruct.ToCVideo {
	if video == nil {
		return nil
	}
	ret := &dbstruct.ToCVideo{
		Id:        video.Id,
		Dur:       video.Dur,
		CoverUrls: []string{},
		Urls:      []string{defaultMediaFiller.GetFileServerDomain() + video.SelectMinSizeOssId()},
	}
	if coverImg != nil {
		ret.CoverW = coverImg.W
		ret.CoverH = coverImg.H
		ret.CoverFmt = coverImg.Fmt
		imgSrcId := coverImg.SelectMinSizeOssId()
		ret.CoverUrls = []string{defaultMediaFiller.GetFileServerDomain() + imgSrcId}
	}
	return ret
}

func transToCVideoTest(video *dbstruct.Video, coverImg *dbstruct.Image) *dbstruct.ToCVideo {
	if video == nil {
		return nil
	}
	ret := &dbstruct.ToCVideo{
		Id:        video.Id,
		Dur:       video.Dur,
		CoverUrls: []string{},
		Urls:      []string{defaultMediaFiller.GetFileServerDomain() + video.MustSelectMinSizeOssId()},
	}
	if coverImg != nil {
		ret.CoverW = coverImg.W
		ret.CoverH = coverImg.H
		ret.CoverFmt = coverImg.Fmt
		imgSrcId := coverImg.SelectMinSizeOssId()
		ret.CoverUrls = []string{defaultMediaFiller.GetFileServerDomain() + imgSrcId}
	}
	return ret
}

// todo
func transToCImageInternal(image *dbstruct.Image) *dbstruct.ToCImage {
	if image == nil {
		return nil
	}

	// 选择内存最小的图片
	imgSrcId := image.SelectMinSizeOssId()

	return &dbstruct.ToCImage{
		Id:   image.Id,
		W:    image.W,
		H:    image.H,
		Fmt:  image.Fmt,
		Urls: []string{defaultMediaFiller.GetInternalFileServerDomain() + imgSrcId},
	}
}

// todo
func transToCVideoInternal(video *dbstruct.Video, coverImg *dbstruct.Image) *dbstruct.ToCVideo {
	if video == nil {
		return nil
	}
	ret := &dbstruct.ToCVideo{
		Id:        video.Id,
		Dur:       video.Dur,
		CoverUrls: []string{},
		Urls:      []string{defaultMediaFiller.GetInternalFileServerDomain() + video.SelectMinSizeOssId()},
	}
	if coverImg != nil {
		ret.CoverW = coverImg.W
		ret.CoverH = coverImg.H
		ret.CoverFmt = coverImg.Fmt
		imgSrcId := coverImg.SelectMinSizeOssId()
		ret.CoverUrls = []string{defaultMediaFiller.GetInternalFileServerDomain() + imgSrcId}
	}
	return ret
}