package tiktok_api

import (
	"fmt"
	"git.rosy.net.cn/baseapi"
	"git.rosy.net.cn/baseapi/utils"
	"go.uber.org/zap"
	"math"
	"strings"
	"testing"
)

var (
	api         *API
	sugarLogger *zap.SugaredLogger
)

func init() {

	logger, _ := zap.NewDevelopment()
	sugarLogger = logger.Sugar()
	baseapi.Init(sugarLogger)

}

func TestCreateFolad(t *testing.T) {
	data, err := a.CreateFolder("美好菜市图片文件")
	fmt.Println("err=====", err)
	fmt.Println("LogId====", data.LogId)              //2022092811422001020812109607601B77
	fmt.Println("data====", data.Data.Name)           // jxcs_folder
	fmt.Println("data====", data.Data.Type)           // 0
	fmt.Println("data====", data.Data.FolderId)       // 71482708738494630701960 /71482723234545339291960(两个文件夹的id)
	fmt.Println("data====", data.Data.ParentFolderId) // 0
}

func TestUploadImg(t *testing.T) {
	//param := []Imgs{
	//	{Name: "测试图片1", Url: "http://image.jxc4.com/efbfbde919e41435144485bb72bc8bd4.png"},
	//	{Name: "测试图片2", Url: "https://image.jxc4.com/image/94859ee145dd3038f040800dd347e7b7.jpg"},
	//	{Name: "测试图片3", Url: "https://image.jxc4.com/image/7a71696ddc7c030e31f9edc6cb211ab8.jpg"},
	//	{Name: "测试图片detail", Url: "http://image.jxc4.com/image/f22e2e290087f6806ad48d279a7166d0.jpg"},
	//}
	//data, err := a.BatchUploadImages(param)
	//fmt.Println(err)
	//fmt.Println(data)
	//// logid 202209281425150101351501360B6DB460
	detail := "https://image.jxc4.com/image/af51596be83bee4ca9dd71d0a6380b38.jpg"
	ImgOrigin := "https://image.jxc4.com/image/f7b0b4c6e13eadbf7852110dee602f39.png"
	img1, img2, err := GetTiktokImgList(a, "668619", "", detail, ImgOrigin)
	fmt.Println(img1)
	fmt.Println(img2)
	fmt.Println(err)
}

// GetTiktokImgList 获取抖音图片链接
func GetTiktokImgList(api *API, storeId, appOrgCode string, detailImg string, img ...string) (string, string, error) {
	imgs := make([]Imgs, 0, 0)
	for _, v := range img {
		if v != "" {
			imgs = append(imgs, Imgs{
				Name: storeId + "_" + v[21:54],
				Url:  v,
			})
		}
	}
	if detailImg != "" {
		imgs = append(imgs, Imgs{
			Name: "detail_" + storeId + "_" + detailImg[21:54],
			Url:  detailImg,
		})
	}
	tiktokImgList, err := api.BatchUploadImages(imgs)
	if err != nil {
		return "", "", err
	}

	detailTiktok := ""
	var tiktokImg []string
	for k, v := range tiktokImgList {
		if strings.Contains(k, "detail_") {
			detailTiktok = v.ByteUrl
			continue
		}
		tiktokImg = append(tiktokImg, v.ByteUrl)
	}
	if detailTiktok == "" {
		detailTiktok = tiktokImg[0]
	}

	return strings.Join(tiktokImg, "|"), detailTiktok, nil
}

func TestRefundToken(t *testing.T) {
	a.RefreshToken()

}

func TestComposeSkuNameOriginal(t *testing.T) {
	i := 0
	for {
		if i >= 30 {
			break
		}
		name1 := ComposeSkuNameOriginal("", "【新鲜】凤尾 莴笋尖 莴笋叶", "", "份", 500, "g", 0)
		//fmt.Println("name1 == ", name1)

		name2 := ComposeSkuNameOriginal("", name1, "", "份", 500, "g", 0)
		//fmt.Println("name2 == ", name2)

		name11 := LimitUTF8StringLen(name1, 60)
		name22 := LimitUTF8StringLen(name2, 60)
		fmt.Println("name11 = ", name11)
		fmt.Println("name22 = ", name22)
		i++
	}

}
func ComposeSkuNameOriginal(prefix, name, comment, unit string, spec_quality float32, spec_unit string, maxLen int) (skuName string) {
	strBuilder := &strings.Builder{}
	if prefix != "" {
		strBuilder.WriteString("[")
		strBuilder.WriteString(prefix)
		strBuilder.WriteString("]")
	}
	skuName += name
	strBuilder.WriteString(name)
	if unit == "份" {
		strBuilder.WriteString("约")
	}
	if unit != "" {
		strBuilder.WriteString(ComposeSkuSpec(spec_quality, spec_unit))
		strBuilder.WriteString("/")
		strBuilder.WriteString(unit)
	}
	if comment != "" {
		strBuilder.WriteString("(")
		strBuilder.WriteString(comment)
		strBuilder.WriteString(")")
	}
	skuName = strBuilder.String()
	if maxLen > 0 {
		skuName = utils.LimitUTF8StringLen(skuName, maxLen)
	}
	return skuName
}

func ComposeSkuSpec(spec_quality float32, spec_unit string) (spec string) {
	if spec_unit != "" {
		if math.Round(float64(spec_quality)) == float64(spec_quality) || (spec_unit != "L" && spec_unit != "kg") {
			spec = fmt.Sprintf("%d", int(spec_quality))
		} else {
			spec = strings.TrimRight(fmt.Sprintf("%.2f", spec_quality), "0.")
		}
		spec += spec_unit
	}
	return spec
}
func LimitUTF8StringLen(str string, maxRuneCount int) (limitedStr string) {
	if maxRuneCount > 0 {
		if len(str) > maxRuneCount {
			runeList := []rune(str)
			if len(runeList) > maxRuneCount {
				str = string(runeList[:maxRuneCount])
			}
		}
	}
	return str
}

func Test1111(t *testing.T) {
	fmt.Println(len("【新鲜】凤尾 莴笋尖 莴笋叶约500g/份"))
	fmt.Println(len("【新鲜】凤尾 莴笋尖 莴笋叶约500g/份约500g/                份"))
	fmt.Println(len("【新鲜】凤尾 莴笋尖 莴笋叶约500g/份约500g/份"))
}

func Test22222(t *testing.T) {
	fmt.Println(len("【新鲜】凤尾 莴笋尖 莴笋叶"))
	fmt.Println(len("凤尾 莴笋尖 莴笋叶 约350g"))
}
