package bitgetservice

import (
	"go-admin/config"

	"github.com/go-admin-team/go-admin-core/logger"
)

type BitgetMarketFutureClient struct {
}

func (c *BitgetMarketFutureClient) Subscribe(symbols []string) error {
	// 1. 创建Bitget客户端（不需要API密钥用于公开数据）
	client := &BitgetClient{}

	// 初始化公开行情客户端
	marketConfig := DefaultMarketConfig()
	client.marketClient = NewBitgetMarketClient(marketConfig)
	if config.ExtConfig.ProxyUrl != "" {
		client.SetMarketProxy("", config.ExtConfig.ProxyUrl)
	}

	// 3. 设置消息监听器
	msgListener := func(message string) {
		logger.Info("收到市场数据: %s", message)
		// 在这里处理市场数据
	}

	errorListener := func(message string) {
		logger.Error("收到错误消息: %s", message)
	}

	client.SetMarketListeners(msgListener, errorListener)

	// 4. 连接WebSocket
	if err := client.ConnectMarketData(); err != nil {
		logger.Error("连接市场数据失败: %v", err)
		return err
	}

	// 5. 订阅合约行情
	tickerListener := func(message string) {
		logger.Info("收到BTC-USDT合约行情: %s", message)
	}

	params := []SubscribeReq{}

	for _, v := range symbols {
		params = append(params, SubscribeReq{
			InstType: "USDT-FUTURES",
			Channel:  "ticker",
			InstId:   v,
		})
	}

	if err := client.SubscribeMarketData(params, tickerListener); err != nil {
		logger.Error("订阅合约行情失败: %v", err)
		return err
	}
	return nil
}
