package services

import (
	"encoding/json"
	"errors"
	"eta/eta_mobile/services/alarm_msg"
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"time"

	"eta/eta_mobile/utils"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/sts"
)

/*
上传demo
func init() {
	fmt.Println("start")
	randStr := utils.GetRandStringNoSpecialChar(28)
	fileName :=  randStr + ".jpg"
	fmt.Println("fileName:",fileName)
	fpath:="./1.png"
	resourceUrl,err:=UploadAliyun(fileName,fpath)
	if err!=nil {
		fmt.Println("UploadAliyun Err:",err.Error())
		return
	}
	fmt.Println("resourceUrl:",resourceUrl)
	fmt.Println("end")
}
*/

// 图片上传到阿里云
//func UploadAliyun(filename, filepath string) (string, error) {
//	client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
//	if err != nil {
//		return "1", err
//	}
//	bucket, err := client.Bucket(utils.Bucketname)
//	if err != nil {
//		return "2", err
//	}
//	path := utils.UploadDir + time.Now().Format("200601/20060102/")
//	path += filename
//	err = bucket.PutObjectFromFile(path, filepath)
//	if err != nil {
//		return "3", err
//	}
//	path = utils.Imghost + path
//	return path, err
//}

// UploadAliyunV2 图片上传到阿里云
//func UploadAliyunV2(filename, filepath string) (string, error) {
//	if utils.AccessKeyId == `` {
//		return "0", errors.New("阿里云信息未配置")
//	}
//	client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
//	if err != nil {
//		return "1", err
//	}
//	bucket, err := client.Bucket(utils.Bucketname)
//	if err != nil {
//		return "2", err
//	}
//	path := utils.UploadDir + time.Now().Format("200601/20060102/")
//	path += filename
//	err = bucket.PutObjectFromFile(path, filepath)
//	if err != nil {
//		return "3", err
//	}
//	path = utils.Imghost + path
//	return path, err
//}

// UploadAudioAliyun 音频上传到阿里云
//func UploadAudioAliyun(filename, filepath string) (string, error) {
//	if utils.AccessKeyId == `` {
//		return "0", errors.New("阿里云信息未配置")
//	}
//	client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
//	if err != nil {
//		return "1", err
//	}
//	bucket, err := client.Bucket(utils.Bucketname)
//	if err != nil {
//		return "2", err
//	}
//	path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
//	path += filename
//	err = bucket.PutObjectFromFile(path, filepath)
//	if err != nil {
//		return "3", err
//	}
//	path = utils.Imghost + path
//	return path, err
//}

// UploadVideoAliyun 视频上传到阿里云
//func UploadVideoAliyun(filename, filepath, savePath string) error {
//	if utils.AccessKeyId == `` {
//		return errors.New("阿里云信息未配置")
//	}
//	defer func() {
//		os.Remove(filepath)
//	}()
//	client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
//	if err != nil {
//		return err
//	}
//	bucket, err := client.Bucket(utils.Bucketname)
//	if err != nil {
//		return err
//	}
//	//path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
//	//path += filename
//	err = bucket.PutObjectFromFile(savePath, filepath)
//	if err != nil {
//		return err
//	}
//	//path = utils.Imghost + path
//	//return path,err
//	return err
//}

// UploadFileToAliyun 上传文件到阿里云
//func UploadFileToAliyun(filename, filepath, savePath string) error {
//	if utils.AccessKeyId == `` {
//		return errors.New("阿里云信息未配置")
//	}
//	defer func() {
//		os.Remove(filepath)
//	}()
//	client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
//	if err != nil {
//		return err
//	}
//	bucket, err := client.Bucket(utils.Bucketname)
//	if err != nil {
//		return err
//	}
//	//path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
//	//path += filename
//	err = bucket.PutObjectFromFile(savePath, filepath)
//	if err != nil {
//		return err
//	}
//	//path = utils.Imghost + path
//	//return path,err
//	return err
//}

type STSToken struct {
	AccessKeyId     string
	AccessKeySecret string
	SecurityToken   string
	ExpiredTime     string
	RegionId        string
	Bucketname      string
	Endpoint        string
	Imghost         string
}

// GetOssSTSToken 获取STSToken
func GetOssSTSToken() (item *STSToken, err error) {
	defer func() {
		if err != nil {
			utils.FileLog.Info(err.Error())
			go alarm_msg.SendAlarmMsg("获取STSToken失败, ErrMsg: "+err.Error(), 3)
		}
	}()
	item = new(STSToken)
	// 获取缓存中的Token
	recent, _ := utils.Rc.RedisString(utils.STSTokenCacheKey)
	if recent != "" {
		lastToken := new(STSToken)
		if e := json.Unmarshal([]byte(recent), &lastToken); e != nil {
			err = errors.New("GetOssSTSToken lastToken Unmarshal Err: " + e.Error())
			return
		}
		// 未防止正在上传大文件时Token过期, 将判定的过期时间提前10分钟
		afterTime := time.Now().Local().Add(10 * time.Minute)
		expired, e := time.ParseInLocation(utils.FormatDateTime, lastToken.ExpiredTime, time.Local)
		if e != nil {
			err = errors.New("GetOssSTSToken expiredTime Parse Err: " + e.Error())
			return
		}
		if expired.After(afterTime) {
			item.AccessKeyId = lastToken.AccessKeyId
			item.AccessKeySecret = lastToken.AccessKeySecret
			item.SecurityToken = lastToken.SecurityToken
			item.ExpiredTime = lastToken.ExpiredTime
			item.RegionId = utils.RegionId
			item.Bucketname = utils.Bucketname
			item.Endpoint = utils.Imghost
			item.Imghost = utils.Imghost
			return
		}
	}
	// 已过期则获取新的token
	newToken, e := NewSTSToken()
	if e != nil {
		err = errors.New("GetOssSTSToken NewSTSToken Err: " + e.Error())
		return
	}
	newTokenJson, e := json.Marshal(newToken)
	if e != nil {
		err = errors.New("GetOssSTSToken NewToken JSON Err: " + e.Error())
		return
	}
	// 覆盖缓存
	if e := utils.Rc.Put(utils.STSTokenCacheKey, newTokenJson, time.Hour); e != nil {
		err = errors.New("GetOssSTSToken SetRedis Err: " + e.Error())
		return
	}
	item = newToken
	return
}

// NewSTSToken 获取一个新的STSToken
func NewSTSToken() (item *STSToken, err error) {
	defer func() {
		if err != nil {
			utils.FileLog.Info(err.Error())
		}
	}()
	item = new(STSToken)
	client, e := sts.NewClientWithAccessKey("cn-shanghai", utils.RAMAccessKeyId, utils.RAMAccessKeySecret)
	if e != nil {
		err = errors.New("NewSTSToken NewClient Err: " + e.Error())
		return
	}
	request := sts.CreateAssumeRoleRequest()
	request.Scheme = utils.AliStsScheme
	request.RegionId = utils.RegionId
	request.RoleArn = utils.RoleArn
	now := time.Now().Format(utils.FormatDateTimeUnSpace)
	request.RoleSessionName = utils.RoleSessionName + now
	request.DurationSeconds = "3600"
	request.ConnectTimeout = 300 * time.Second
	request.ReadTimeout = 300 * time.Second

	response, e := client.AssumeRole(request)
	if e != nil {
		err = errors.New("NewSTSToken AssumeRole Err: " + e.Error())
		return
	}
	if response != nil {
		item.AccessKeyId = response.Credentials.AccessKeyId
		item.AccessKeySecret = response.Credentials.AccessKeySecret
		item.SecurityToken = response.Credentials.SecurityToken
		t, _ := time.Parse(time.RFC3339, response.Credentials.Expiration)
		expiration := t.In(time.Local)
		item.ExpiredTime = expiration.Format(utils.FormatDateTime)
		item.RegionId = utils.RegionId
		item.Bucketname = utils.Bucketname
		item.Endpoint = utils.Imghost
		item.Imghost = utils.Imghost
	}
	return
}

// UploadAliyunToDir 上传至hzchart
//func UploadAliyunToDir(filename, filepath, uploadDir, fileDir string) (string, error) {
//	if utils.AccessKeyId == `` {
//		return "0", errors.New("阿里云信息未配置")
//	}
//	client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
//	if err != nil {
//		return "1", err
//	}
//	bucket, err := client.Bucket(utils.Bucketname)
//	if err != nil {
//		return "2", err
//	}
//	if uploadDir == "" {
//		uploadDir = utils.UploadDir
//	}
//	if fileDir == "" {
//		fileDir = time.Now().Format("200601/20060102/")
//	}
//	path := uploadDir + fileDir
//	path += filename
//	err = bucket.PutObjectFromFile(path, filepath)
//	if err != nil {
//		return "3", err
//	}
//	path = utils.Imghost + path
//	return path, err
//}

type AliOss struct{}

// UploadFile 上传文件
func (m *AliOss) UploadFile(fileName, filePath, savePath string) (string, error) {
	if utils.AccessKeyId == `` {
		return "0", errors.New("阿里云信息未配置")
	}
	client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
	if err != nil {
		return "1", err
	}
	bucket, err := client.Bucket(utils.Bucketname)
	if err != nil {
		return "2", err
	}

	path := savePath
	if savePath == "" {
		path = utils.UploadDir + time.Now().Format("200601/20060102/") + fileName
	}
	err = bucket.PutObjectFromFile(path, filePath)
	if err != nil {
		return "3", err
	}
	resourceUrl := utils.Imghost + path
	return resourceUrl, err
}

func (m *AliOss) GetUploadToken() (token OssToken, err error) {
	stsToken, e := GetOssSTSToken()
	if e != nil {
		err = fmt.Errorf("GetOssSTSToken err: %s", e.Error())
		return
	}
	token.AccessKeyId = stsToken.AccessKeyId
	token.AccessKeySecret = stsToken.AccessKeySecret
	token.SecurityToken = stsToken.SecurityToken
	token.ExpiredTime = stsToken.ExpiredTime
	token.RegionId = stsToken.RegionId
	token.Bucketname = stsToken.Bucketname
	token.Endpoint = stsToken.Endpoint
	token.Imghost = stsToken.Imghost
	return
}
