package fileConfig

import (
	fileAsw "epur-pay/pkg/fileserver/asw"
	fileCos "epur-pay/pkg/fileserver/cos"
	fileLocal "epur-pay/pkg/fileserver/local"
	fileOss "epur-pay/pkg/fileserver/oss"
	"epur-pay/pkg/logger"
	"epur-pay/pkg/utils"
)

var Cf *config

type config struct {
	Store  *Store //当前使用的桶
	Stores []*Store
}

/*
存储类型

	oss  阿里云oss
	cos  腾讯云
	asw  亚马逊
	local 本地local
*/
type Store struct {
	StoreType string
	Api       api
}

type api interface {
	Put(fileName string, contentType string, data interface{}) string
	ReadUrl(filePath string) string
	Token(key string, contentType string) (map[string]interface{}, error)
}

type FileStoreRequest struct {
	AccessKeyId, AccessKeySecret, BucketName, EndPoint, Access, RegionId string
	Private                                                              bool
	StoreType                                                            string
}

func New(f *FileStoreRequest) {

	Cf = &config{
		Stores: []*Store{},
	}

	if f.StoreType == "oss" {
		store, err := fileOss.New(f.AccessKeyId, f.AccessKeySecret, f.BucketName, f.EndPoint, f.Access, f.RegionId, f.Private)
		utils.Error(err)
		currStore := &Store{StoreType: f.StoreType, Api: store}
		Cf.Stores = append(Cf.Stores, currStore)
		Cf.Store = currStore

		logger.AccessLogger.Infof("---> StoreType: %s Access: %s", f.StoreType, store.Access)
	} else if f.StoreType == "cos" {
		store, err := fileCos.New(f.AccessKeyId, f.AccessKeySecret, f.BucketName, f.EndPoint, f.Access, f.RegionId)
		utils.Error(err)
		currStore := &Store{StoreType: f.StoreType, Api: store}
		Cf.Stores = append(Cf.Stores, currStore)
		Cf.Store = currStore

		logger.AccessLogger.Infof("---> StoreType: %s Access: %s", f.StoreType, store.Access)
	} else if f.StoreType == "asw" {
		store, err := fileAsw.New(f.AccessKeyId, f.AccessKeySecret, f.BucketName, f.EndPoint, f.Access, f.RegionId)
		utils.Error(err)
		currStore := &Store{StoreType: f.StoreType, Api: store}
		Cf.Stores = append(Cf.Stores, currStore)
		Cf.Store = currStore

		logger.AccessLogger.Infof("---> StoreType: %s Access: %s", f.StoreType, store.Access)
	} else if f.StoreType == "local" {
		store, err := fileLocal.New(f.AccessKeyId, f.AccessKeySecret, f.BucketName, f.EndPoint, f.Access, f.RegionId)
		utils.Error(err)
		currStore := &Store{StoreType: f.StoreType, Api: store}
		Cf.Stores = append(Cf.Stores, currStore)
		Cf.Store = currStore

		logger.AccessLogger.Infof("---> StoreType: %s Access: %s", f.StoreType, store.Access)
	}
}