package util

import (
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"io/ioutil"
	"os"
	qu "qfw/util"
)

type Task struct {
	UserId  string
	GiveNum int
}

//oss
var (
	ossEndpoint        = "oss-cn-beijing.aliyuncs.com" //正式环境用：oss-cn-beijing-internal.aliyuncs.com 测试：oss-cn-beijing.aliyuncs.com
	ossAccessKeyId     = "LTAI4G5x9aoZx8dDamQ7vfZi"
	ossAccessKeySecret = "Bk98FsbPYXcJe72n1bG3Ssf73acuNh"
	ossBucketName      = "topjy"
	ossclient          *oss.Client
)

func InitOss() {
	client, err := oss.New(ossEndpoint, ossAccessKeyId, ossAccessKeySecret)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	ossclient = client
}

func OssGetObject(objectName string) string {
	defer qu.Catch()
	// 获取存储空间。
	bucket, err := ossclient.Bucket(ossBucketName)
	if err != nil {
		fmt.Println("Error:", err)
		return ""
	}

	// 下载文件到流。
	body, err := bucket.GetObject(objectName)
	if err != nil {
		fmt.Println("Error:", err)
		return ""
	}
	defer body.Close()
	data, err := ioutil.ReadAll(body)
	if err != nil {
		fmt.Println("Error:", err)
		return ""
	}
	return string(data)
}

func GetFileText(tmp map[string]interface{}) (filetext string) {
	fn_url := map[string]string{}
	if projectinfo, ok := tmp["projectinfo"].(map[string]interface{}); ok && projectinfo != nil {
		if attachments, ok := projectinfo["attachments"].(map[string]interface{}); ok && attachments != nil {
			for _, info := range attachments {
				if infoMap, ok := info.(map[string]interface{}); ok && infoMap != nil {
					filename := qu.ObjToString(infoMap["filename"])
					org_url := qu.ObjToString(infoMap["org_url"])
					fn_url[filename] = org_url
				}
			}
		}
	}

	if len(fn_url) > 0 {
		index := 1
		if attchMap, ok := tmp["attach_text"].(map[string]interface{}); attchMap != nil && ok {
			for fn, url := range fn_url {
				href := fmt.Sprint(index) + ".<a href=" + url + " target='_blank'>" + fn + "</a>\n<br/>"
				onetext := ""
				for _, tmpData1 := range attchMap {
					if tmpData2, ok := tmpData1.(map[string]interface{}); tmpData2 != nil && ok {
						for _, result := range tmpData2 {
							if resultMap, ok := result.(map[string]interface{}); resultMap != nil && ok {
								file_name := qu.ObjToString(resultMap["file_name"])
								if fn == file_name { //找到对应的附件
									if attach_url := qu.ObjToString(resultMap["attach_url"]); attach_url != "" {
										onetext = OssGetObject(attach_url) + "\n<br/>" //oss读数据
										goto L
									}
								}
							}
						}
					}
				}
			L:
				filetext += href + onetext
				index++
			}
		}
	}
	/**
	if attchMap, ok := tmp["attach_text"].(map[string]interface{}); attchMap != nil && ok {
		for _, tmpData1 := range attchMap {
			if tmpData2, ok := tmpData1.(map[string]interface{}); tmpData2 != nil && ok {
				for _, result := range tmpData2 {
					if resultMap, ok := result.(map[string]interface{}); resultMap != nil && ok {
						if attach_url := util.ObjToString(resultMap["attach_url"]); attach_url != "" {
							bs := OssGetObject(attach_url) //oss读数据
							if utf8.RuneCountInString(filetext+bs) < 50000 {
								filetext += bs + "\n"
							} else {
								break
							}
						}
					}
				}
			}
		}
	}
	*/
	return
}
