package oss

import (
	"fmt"
	"log"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

const (
	ENDPOINT      = "oss-cn-beijing.aliyuncs.com"
	ACCESS_KEY    = "LTAI5tBUvFtfWU4H3AikcmwF"
	ACCESS_SECRET = "W7Yceh0A0RODc6bELpcML1xHZOQ32q"
	BUCKET        = "infish-oss"
)

func CreateClient() {
	_, err := oss.New(ENDPOINT, ACCESS_KEY, ACCESS_SECRET)
	if err != nil {
		panic(err)
	}

	return

}

type UpladImagesRes struct {
	Model string
	Shoes []string
}

func UpladImages(model string, shoes []string) (UpladImagesRes, error) {

	cli, err := oss.New(ENDPOINT, ACCESS_KEY, ACCESS_SECRET)
	if err != nil {
		log.Fatalf("Error: %v", err)
		return UpladImagesRes{}, err
	}

	// 获取存储空间
	bucket, err := cli.Bucket(BUCKET)
	if err != nil {
		return UpladImagesRes{}, err
	}
	res := UpladImagesRes{}
	localFileName := model
	objectName := fmt.Sprintf("tryon/%s", model)
	err = bucket.PutObjectFromFile(objectName, localFileName)
	if err != nil {
		return UpladImagesRes{}, err
	}
	res.Model = fmt.Sprintf("https://infish-oss.oss-cn-beijing.aliyuncs.com/%s", objectName)

	for _, shoe := range shoes {
		localFileName = shoe
		objectName = fmt.Sprintf("tryon/%s", shoe)
		err = bucket.PutObjectFromFile(objectName, localFileName)
		if err != nil {
			fmt.Println(err)
			continue
		}
		res.Shoes = append(res.Shoes, fmt.Sprintf("https://infish-oss.oss-cn-beijing.aliyuncs.com/%s", objectName))
	}

	return res, nil
}
