//go:build integration

package test

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"mime/multipart"
	"net/http"
	"os"
	"testing"
)

type PushData struct {
	uploaderUrl string
	fileName    string
	bucket      string
	ossName     string
	purpose     string
}

// 华为
var ossData = PushData{
	uploaderUrl: "http://127.0.0.1:12214/fts/Transfer/Uploader",
	fileName:    "hello.txt",
	bucket:      "makw-cdn-file",
	ossName:     "huawei",
	purpose:     "uploader", //文件夹名称（用途）
}

// minio
//var ossData = PushData{
//	uploaderUrl: "http://127.0.0.1:12214/fts/Transfer/Uploader",
//	fileName:    "hello.txt",
//	bucket:      "uploader",
//	ossName:     "minio",
//	purpose:     "uploader",
//}

var token = os.Getenv("BSM_FTS_TOKEN")

//var token = ""

func postFile(ossName, filename, bucket, key string) (string, *bytes.Buffer) {
	body := &bytes.Buffer{}
	writer := multipart.NewWriter(body)
	_ = writer.WriteField("provider", ossName)
	_ = writer.WriteField("bucket", bucket)
	_ = writer.WriteField("purpose", key)
	uploadWriter, _ := writer.CreateFormFile("file", filename)
	fileData, _ := os.Open(filename)
	defer fileData.Close()
	io.Copy(uploadWriter, fileData)
	_ = writer.Close()
	return writer.FormDataContentType(), body
}

// 测试上传请求
func TestUploader(t *testing.T) {
	// 组装请求内容
	contextType, body := postFile(ossData.ossName, ossData.fileName, ossData.bucket, ossData.purpose)
	//fmt.Println(body.String())

	req, err := http.NewRequest("POST", ossData.uploaderUrl, body)
	if err != nil {
		panic(err)
	}
	req.Header.Set("Content-Type", contextType)
	req.Header.Set("Authorization", token)
	// 发起请求
	client := &http.Client{}
	resp, err := client.Do(req)
	//resp, err := http.Post(ossData.uploaderUrl, context, body)
	if err != nil {
		panic(err)
	}

	content, err := io.ReadAll(resp.Body)
	defer resp.Body.Close()
	fmt.Printf("%s", content)
}

// allows 获取传输配置信息
func TestGetConfig(t *testing.T) {
	resp, err := http.Get("http://127.0.0.1:12214/fts/Get/Config")
	if err != nil {
		panic(err)
	}
	content, err := io.ReadAll(resp.Body)
	defer resp.Body.Close()
	fmt.Printf("%s", content)
}

// 闪传
func TestPrepare(t *testing.T) {
	var data = map[string]string{
		"hash":      "04d6af2bff8ae74b7ac7d42b56a5ca795f56a4d00f4f4229fdae3561d46b912c",
		"file_name": "xx.png",
	}
	jsonData, _ := json.Marshal(data)
	var body = bytes.NewBuffer([]byte(jsonData))
	fmt.Println(body)
	resp, err := http.Post("http://127.0.0.1:12214/fts/Transfer/Prepare", "application/json", body)
	if err != nil {
		panic(err)
	}
	content, err := io.ReadAll(resp.Body)
	fmt.Printf("%s", content)

}

// 列表
func TestList(t *testing.T) {
	// 组装请求内容
	var data = map[string]int{
		"size": 2,
		//"page": 33,
	}
	jsonData, _ := json.Marshal(data)
	var body = bytes.NewBuffer(jsonData)
	fmt.Println(body)
	req, err := http.NewRequest("POST", "http://127.0.0.1:12214/fts/Transfer/FileList", body)
	if err != nil {
		panic(err)
	}
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", token)
	// 发起请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}

	content, err := io.ReadAll(resp.Body)
	defer resp.Body.Close()
	fmt.Printf("%s", content)
}

// 详情
func TestDetails(t *testing.T) {
	// 组装请求内容
	var data = map[string]string{
		"identity": "5fc04e7f-1b82-4069-9ea7-c29414722e98",
		//"file_name": "hello.txt",
	}
	jsonData, _ := json.Marshal(data)
	var body = bytes.NewBuffer([]byte(jsonData))
	req, err := http.NewRequest("POST", "http://127.0.0.1:12214/fts/Transfer/Details", body)
	if err != nil {
		panic(err)
	}
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", token)
	// 发起请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}

	content, err := io.ReadAll(resp.Body)
	defer resp.Body.Close()
	fmt.Printf("%s", content)
}
