package svc

import (
	"fmt"
	"strings"

	"applet/app/e"
	"applet/app/lib/qiniu"
	"applet/app/md"
	"applet/app/utils"
	"applet/app/utils/logx"

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

// 请求文件上传
func ImgReqUpload(c *gin.Context, uid, dirName, fname, callbackUrl string, fsize int64) (interface{}, error) {
	ext := utils.FileExt(fname)
	if err := initStg(c, fsize, ext); err != nil {
		return nil, err
	}
	// logx.Warn(uid)
	newName := dirName + "_" + fmt.Sprintf("%010s", uid)

	// if dirName == md.FILE_DIR_FEEDBACK || dirName == md.FILE_DIR_STYLE {
	// 	newName += "_" + utils.FormatNanoUnix() + utils.RandString(4, "0123456789")
	// }
	// 默认都加时间戳
	newName += "_" + utils.FormatNanoUnix() + utils.RandString(4, "0123456789")
	newName += "." + ext

	f := &md.FileCallback{
		Uid:      uid,
		DirId:    md.FileUserDir[dirName],
		FileName: newName,
	}
	// logx.Warn(f.Uid)
	return qiniu.ReqImgUpload(f, callbackUrl), nil
}

func initStg(c *gin.Context, fsize int64, ext string) error {
	// 获取上传配置
	stgInfo := SysCfgFind(
		c,
		md.KEY_CFG_FILE_BUCKET,
		md.KEY_CFG_FILE_HOST,
		md.KEY_CFG_FILE_AK,
		md.KEY_CFG_FILE_SK,
		md.KEY_CFG_FILE_PVD,
		md.KEY_CFG_FILE_REGION,
		md.KEY_CFG_FILE_MAX_SIZE,
		md.KEY_CFG_FILE_EXT,
		md.KEY_CFG_FILE_SCHEME,
		md.KEY_CFG_FILE_AVATAR_THUMBNAIL,
	)
	//?imageView2/1/w/120/h/120/format/webp/interlace/1
	if stgInfo == nil {
		return e.NewErrCode(e.ERR_CFG)
	}
	// todo 目前仅支持七牛
	if v, ok := stgInfo[md.KEY_CFG_FILE_PVD]; !ok || v != "qiniu" {
		return e.NewErrCode(e.ERR_CFG)
	}
	if v, ok := stgInfo[md.KEY_CFG_FILE_REGION]; !ok || v == "" {
		return e.NewErrCode(e.ERR_CFG)
	}
	if v, ok := stgInfo[md.KEY_CFG_FILE_AK]; !ok || v == "" {
		return e.NewErrCode(e.ERR_CFG)
	}
	if v, ok := stgInfo[md.KEY_CFG_FILE_SK]; !ok || v == "" {
		return e.NewErrCode(e.ERR_CFG)
	}
	if v, ok := stgInfo[md.KEY_CFG_FILE_BUCKET]; !ok || v == "" {
		return e.NewErrCode(e.ERR_CFG)
	}
	if v, ok := stgInfo[md.KEY_CFG_FILE_SCHEME]; !ok || v == "" {
		stgInfo[md.KEY_CFG_FILE_SCHEME] = "http"
		SysCfgSet(c, md.KEY_CFG_FILE_SCHEME, stgInfo[md.KEY_CFG_FILE_SCHEME], "文件域名HTTP协议")
	}
	qiniu.Init(stgInfo[md.KEY_CFG_FILE_AK], stgInfo[md.KEY_CFG_FILE_SK], stgInfo[md.KEY_CFG_FILE_BUCKET], stgInfo[md.KEY_CFG_FILE_REGION], stgInfo[md.KEY_CFG_FILE_SCHEME])
	if v, ok := stgInfo[md.KEY_CFG_FILE_HOST]; !ok || v == "" {
		var err error
		stgInfo[md.KEY_CFG_FILE_HOST], err = qiniu.BucketGetDomain(stgInfo[md.KEY_CFG_FILE_BUCKET])
		if err != nil {
			logx.Error(err)
			return e.NewErrCode(e.ERR_CFG)
		}
		SysCfgSet(c, md.KEY_CFG_FILE_HOST, stgInfo[md.KEY_CFG_FILE_HOST], "文件域名地址")
	}
	// 头像缩略图参数
	if v, ok := stgInfo[md.KEY_CFG_FILE_AVATAR_THUMBNAIL]; !ok || v == "" {
		SysCfgSet(c, md.KEY_CFG_FILE_AVATAR_THUMBNAIL, "?imageView2/1/w/200/h/200/format/png", "文件用户头像缩略图参数")
	}

	// 检查文件大小限制
	if v, ok := stgInfo[md.KEY_CFG_FILE_MAX_SIZE]; ok && v != "" && utils.StrToInt64(v) < fsize {
		return e.NewErrCode(e.ERR_FILE_MAX_SIZE)
	}
	// 检查文件后缀
	if v, ok := stgInfo[md.KEY_CFG_FILE_EXT]; ok && v != "" && !strings.Contains(v, ext) {
		return e.NewErrCode(e.ERR_FILE_EXT)
	}
	return nil
}
