package binanceservice

import (
	"fmt"
	"go-admin/app/admin/models"
	"go-admin/common/helper"
	"go-admin/pkg/utility"
	"go-admin/pkg/utility/snowflakehelper"
	"testing"

	"github.com/bytedance/sonic"

	"github.com/shopspring/decimal"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func TestCancelFutClosePosition(t *testing.T) {
	dsn := "root:root@tcp(192.168.123.216:3306)/gp-bian?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	var apiUserInfo models.LineApiUser
	db.Model(&models.LineApiUser{}).Where("id = 21").Find(&apiUserInfo)
	err := CancelFutClosePosition(apiUserInfo, "ADAUSDT", "BUY", "SHORT")
	if err != nil {
		t.Log("err:", err)
	}
	fmt.Println("成功")
}

func TestDecimal(t *testing.T) {

	fromString, err := decimal.NewFromString("")
	if err != nil {
		fmt.Println("err:", err)
	}
	fmt.Println(fromString)
}

func TestPositionV3(t *testing.T) {
	api := FutRestApi{}
	dsn := "root:root@tcp(192.168.1.12:3306)/gp-bian?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	var apiUserInfo models.LineApiUser
	db.Model(&models.LineApiUser{}).Where("id = 21").Find(&apiUserInfo)
	err := CancelFutClosePosition(apiUserInfo, "ADAUSDT", "BUY", "SHORT")
	if err != nil {
		t.Log("err:", err)
	}

	v3, err := api.GetPositionV3(&apiUserInfo, "DOGEUSDT")
	if err != nil {
		t.Log("err:", err)
	}
	fmt.Println(v3)
}

func TestCancelSpotOrder(t *testing.T) {
	//dsn := "root:root@tcp(192.168.1.12:3306)/gp-bian?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	//db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	//var apiUserInfo models.LineApiUser
	//db.Model(&models.LineApiUser{}).Where("id = 21").Find(&apiUserInfo)
	//api := SpotRestApi{}
	//dto.CancelOpenOrderReq{
	//	ApiId:     21,
	//	Symbol:    "ADAUSDT",
	//	OrderSn:   utility.Int64ToString(snowflakehelper.GetOrderId()),
	//	OrderType: 0,
	//}
	//api.CancelOpenOrders()
}

func TestCancelAllFutOrder(t *testing.T) {
	dsn := "root:root@tcp(192.168.1.12:3306)/gp-bian?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	var apiUserInfo models.LineApiUser
	db.Model(&models.LineApiUser{}).Where("id = 21").Find(&apiUserInfo)
	api := FutRestApi{}
	api.CancelAllFutOrder(apiUserInfo, "TRUMPUSDT")
}

//func TestName(t *testing.T) {
//	api := FutRestApi{}
//	dsn := "root:root@tcp(192.168.1.12:3306)/gp-bian?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
//	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
//	var apiUserInfo models.LineApiUser
//	db.Model(&models.LineApiUser{}).Where("id = 21").Find(&apiUserInfo)
//	api.CancelBatchFutOrder(apiUserInfo, "ADAUSDT",[]{""})
//}

func TestFutOrderPalce(t *testing.T) {
	api := FutRestApi{}
	dsn := "root:root@tcp(192.168.1.12:3306)/gp-bian?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	api.OrderPlace(db, FutOrderPlace{
		ApiId:            21,
		Symbol:           "ADAUSDT",
		Side:             "SELL",
		Quantity:         decimal.NewFromFloat(7),
		Price:            decimal.NewFromFloat(0.9764),
		SideType:         "LIMIT",
		OpenOrder:        0,
		Profit:           decimal.Zero,
		StopPrice:        decimal.Zero,
		OrderType:        "LIMIT",
		NewClientOrderId: "367580922570080256",
	})
}

func TestCancelOpenOrderByOrderSn(t *testing.T) {
	api := SpotRestApi{}
	dsn := "root:root@tcp(192.168.1.12:3306)/go_exchange_single?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	var apiUserInfo models.LineApiUser
	db.Model(&models.LineApiUser{}).Where("id = ?", 50).Find(&apiUserInfo)
	err := api.CancelOpenOrderByOrderSn(apiUserInfo, "TRUMPUSDT", "1698362708")
	if err != nil {
		t.Log("err:", err)
	} else {
		fmt.Println("成功")
	}
}

func TestCancelOpenOrderBySymbol(t *testing.T) {
	// api := SpotRestApi{}
	dsn := "root:root@tcp(192.168.1.12:3306)/gp-bian?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	var apiUserInfo models.LineApiUser
	db.Model(&models.LineApiUser{}).Where("id = ?", 10).Find(&apiUserInfo)
	err := CancelSpotOrder("ADAUSDT", &apiUserInfo, "SELL")
	if err != nil {
		t.Log("err:", err)
	} else {
		fmt.Println("成功")
	}
}

func TestDelRedisKeys(t *testing.T) {
	//初始redis 链接
	helper.InitDefaultRedis("192.168.1.12:6379", "", 0)
	helper.InitLockRedisConn("192.168.1.12:6379", "", "0")
	prefixs := []string{
		"api_user_hold",
		"spot_trigger_lock",
		"fut_trigger_lock",
		"fut_trigger_stop_lock",
		"spot_trigger_stop_lock",
		"spot_addposition_trigger",
		"fut_addposition_trigger",
		"spot_hedge_close_position",
		"futures_hedge_close_position",
		"spot_callback",
		"fut_callback",
		"holde_a",
		"holde_b",
	}
	helper.DefaultRedis.DeleteKeysByPrefix(prefixs...)
}

func TestOpenOrders(t *testing.T) {
	dsn := "root:root@tcp(192.168.1.12:3306)/gp-bian?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	var apiUserInfo models.LineApiUser
	db.Model(&models.LineApiUser{}).Where("id = ?", 21).Find(&apiUserInfo)
	client := GetClient(&apiUserInfo)
	auth, _, err := client.SendSpotAuth("/api/v3/order", "GET", map[string]string{"symbol": "ADAUSDT", "orderId": "6001232151"})
	if err != nil {
		fmt.Println("err:", err)
	}
	m := make(map[string]interface{}, 0)
	sonic.Unmarshal(auth, &m)
	fmt.Println("m:", m)
}

func TestClosePosition(t *testing.T) {
	endpoint := "/fapi/v1/order"
	params := map[string]string{
		"symbol":           "ADAUSDT",
		"type":             "LIMIT",
		"quantity":         "5",
		"newClientOrderId": utility.Int64ToString(snowflakehelper.GetOrderId()),
		"positionSide":     "SHORT",
	}
	params["side"] = "BUY"
	params["price"] = "0.98"
	params["timeInForce"] = "GTC"
	dsn := "root:root@tcp(192.168.1.12:3306)/gp-bian?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	var apiUserInfo models.LineApiUser
	db.Model(&models.LineApiUser{}).Where("id = ?", 21).Find(&apiUserInfo)

	client := GetClient(&apiUserInfo)
	resp, _, err := client.SendFuturesRequestAuth(endpoint, "POST", params)
	fmt.Println("resp:", string(resp))
	fmt.Println("err:", err)

}

func TestFutOrderPlace(t *testing.T) {
	futureApi := FutRestApi{}
	dsn := "root:root@tcp(192.168.1.12:3306)/go_exchange_single?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	err := futureApi.OrderPlace(db, FutOrderPlace{
		ApiId:            50,
		Symbol:           "ADAUSDT",
		Side:             "SELL",
		Quantity:         decimal.NewFromFloat(8),
		Price:            decimal.NewFromFloat(0.7012),
		SideType:         "LIMIT",
		OpenOrder:        0,
		Profit:           decimal.NewFromFloat(0.7017),
		StopPrice:        decimal.NewFromFloat(0.7017),
		OrderType:        "STOP",
		NewClientOrderId: "5465465784222554477",
	})
	fmt.Println("err:", err)

}

func TestFutureQueryOrder(t *testing.T) {
	futureApi := FutRestApi{}
	// dsn := "root:root@tcp(192.168.1.12:3306)/go_exchange_single?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	// db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})

	helper.InitDefaultRedis("192.168.1.12:6379", "", 2)
	apiUserInfo, _ := GetApiInfo(49)
	order, err := futureApi.GetOrderByOrderSnLoop("TRUMPUSDT", "380211842506555392", apiUserInfo, 3)

	if err != nil {
		fmt.Println("err:", err)
	}

	fmt.Println("future order:", order)
}

func TestSpotQueryOrder(t *testing.T) {
	spotApi := SpotRestApi{}
	// dsn := "root:root@tcp(192.168.1.12:3306)/go_exchange_single?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	// db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})

	helper.InitDefaultRedis("192.168.1.12:6379", "", 2)
	apiUserInfo, _ := GetApiInfo(49)
	order, err := spotApi.GetOrderByOrderSnLoop("ADAUSDT", "380577197825458177", apiUserInfo, 3)

	if err != nil {
		fmt.Println("err:", err)
	}

	fmt.Println("spot order:", order)
}
