package jobs

import (
	"fmt"
	"go-admin/app/admin/models"
	"go-admin/common/helper"
	"go-admin/services/binanceservice"
	"testing"

	"github.com/go-admin-team/go-admin-core/sdk/config"
	"github.com/shopspring/decimal"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

// 测试现货限价转市价订单
func TestSpotLimitTransferJob(t *testing.T) {
	dsn := "root:123456@tcp(127.0.0.1:3306)/go_exchange_single?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})

	helper.InitDefaultRedis("127.0.0.1:6379", "", 2)
	helper.InitLockRedisConn("127.0.0.1:6379", "", "3")

	job := LimitOrderTimeoutDuration{}
	orders := make([]models.LinePreOrder, 0)
	err := db.Model(&models.LinePreOrder{}).
		Where("order_sn =?", "393609596205268992").
		Preload("Childs").
		Find(&orders).Error
	// job.Exec([]string{})

	config.ApplicationConfig.Mode = "dev"

	if err != nil {
		fmt.Printf("获取订单失败 %v", err)
	}

	for _, order := range orders {
		apiInfo, err := binanceservice.GetApiInfo(49)

		if err != nil {
			fmt.Printf("获取api信息失败 %v", err)
			return
		}
		spotApi := binanceservice.SpotRestApi{}
		err = job.ReSpotOrderPlace(db, order, apiInfo, spotApi)

		if err != nil {
			fmt.Printf("下单失败 %v", err)
		}
	}

	select {}
}

// 测试限价转市价订单
func TestLimitTransferJob(t *testing.T) {
	dsn := "root:123456@tcp(127.0.0.1:3306)/go_exchange_single?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})

	helper.InitDefaultRedis("127.0.0.1:6379", "", 2)
	helper.InitLockRedisConn("127.0.0.1:6379", "", "3")

	job := LimitOrderTimeoutDuration{}
	orders := make([]models.LinePreOrder, 0)
	err := db.Model(&models.LinePreOrder{}).
		Where("order_sn =?", "393573282378416128").
		Preload("Childs").
		Find(&orders).Error
	// job.Exec([]string{})

	config.ApplicationConfig.Mode = "dev"

	if err != nil {
		fmt.Printf("获取订单失败 %v", err)
	}

	for _, order := range orders {
		apiInfo, err := binanceservice.GetApiInfo(49)

		if err != nil {
			fmt.Printf("获取api信息失败 %v", err)
			return
		}
		futApi := binanceservice.FutRestApi{}
		err = job.ReFutOrderPlace(db, order, apiInfo, futApi)

		if err != nil {
			fmt.Printf("下单失败 %v", err)
		}
	}
	select {}
}

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

	helper.InitDefaultRedis("127.0.0.1:6379", "", 2)
	helper.InitLockRedisConn("127.0.0.1:6379", "", "3")

	futApi := binanceservice.FutRestApi{}
	// params := binanceservice.FutOrderPlace{
	// 	Symbol: "ADAUSDT",
	// 	ApiId: 49,
	// 	Side: ,
	// }
	apiInfo, err := binanceservice.GetApiInfo(49)

	if err != nil {
		fmt.Printf("获取api信息失败 %v", err)
		return
	}

	err = futApi.ClosePositionLoop("ADAUSDT", "393573282378416128", decimal.NewFromInt(21), "SELL", "LONG", apiInfo, "LIMIT", "0", decimal.NewFromFloat(0.76), 3)

	fmt.Printf("报错 %v", err)
}

// 测试现货下单
func TestReduceSpot(t *testing.T) {
	dsn := "root:123456@tcp(127.0.0.1:3306)/go_exchange_single?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})

	helper.InitDefaultRedis("127.0.0.1:6379", "", 2)
	helper.InitLockRedisConn("127.0.0.1:6379", "", "3")

	spotApi := binanceservice.SpotRestApi{}
	// params := binanceservice.FutOrderPlace{
	// 	Symbol: "ADAUSDT",
	// 	ApiId: 49,
	// 	Side: ,
	// }
	apiInfo, err := binanceservice.GetApiInfo(49)
	fmt.Sprintf("%v", apiInfo)

	// if err != nil {
	// 	fmt.Printf("获取api信息失败 %v", err)
	// 	return
	// }

	err = spotApi.CancelOpenOrderByOrderSn(apiInfo, "ADAUSDT", "393609596188491776")

	fmt.Printf("取消报错 %v", err)

	params := binanceservice.OrderPlacementService{
		ApiId:            49,
		Side:             "SELL",
		Type:             "LIMIT",
		TimeInForce:      "GTC",
		Symbol:           "ADAUSDT",
		Price:            decimal.NewFromFloat(0.76),
		Quantity:         decimal.NewFromInt(21),
		NewClientOrderId: "393609596205268992",
	}
	err = spotApi.OrderPlaceLoop(db, params, 3)

	fmt.Printf("报错 %v", err)
}
