package excservice

import (
	"go-admin/pkg/utility"

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

/*
Bitget用户订单订阅处理
  - @msg 消息内容
  - @listenType 订阅类型 0-现货 1-合约
*/
func ReceiveBitgetListen(msg []byte, listenType int, apiKey string) (reconnect bool, err error) {
	var dataMap map[string]interface{}
	err = sonic.Unmarshal(msg, &dataMap)

	if err != nil {
		log.Error("接收Bitget ws 反序列化失败:", err)
		return
	}

	// 检查是否是操作响应
	if op, exists := dataMap["op"]; exists {
		switch op {
		case "login":
			return handleBitgetLogin(dataMap, apiKey)
		case "subscribe":
			return handleBitgetSubscribe(dataMap, apiKey)
		case "unsubscribe":
			return handleBitgetUnsubscribe(dataMap, apiKey)
		}
	}

	// 检查是否是数据推送
	if arg, exists := dataMap["arg"]; exists {
		return handleBitgetDataPush(dataMap, arg, listenType, apiKey)
	}

	log.Info("未知Bitget事件 内容:", string(msg))
	return false, nil
}

// handleBitgetLogin 处理登录响应
func handleBitgetLogin(dataMap map[string]interface{}, apiKey string) (bool, error) {
	if code, exists := dataMap["code"]; exists {
		if code == "0" || code == 0 {
			log.Info("Bitget WebSocket登录成功 key:%s", apiKey)
		} else {
			log.Error("Bitget WebSocket登录失败 key:%s code:%v", apiKey, code)
			return true, nil // 需要重连
		}
	}
	return false, nil
}

// handleBitgetSubscribe 处理订阅响应
func handleBitgetSubscribe(dataMap map[string]interface{}, apiKey string) (bool, error) {
	if code, exists := dataMap["code"]; exists {
		if code == "0" || code == 0 {
			log.Info("Bitget WebSocket订阅成功 key:%s", apiKey)
		} else {
			log.Error("Bitget WebSocket订阅失败 key:%s code:%v", apiKey, code)
		}
	}
	return false, nil
}

// handleBitgetUnsubscribe 处理取消订阅响应
func handleBitgetUnsubscribe(dataMap map[string]interface{}, apiKey string) (bool, error) {
	if code, exists := dataMap["code"]; exists {
		if code == "0" || code == 0 {
			log.Info("Bitget WebSocket取消订阅成功 key:%s", apiKey)
		} else {
			log.Error("Bitget WebSocket取消订阅失败 key:%s code:%v", apiKey, code)
		}
	}
	return false, nil
}

// handleBitgetDataPush 处理数据推送
func handleBitgetDataPush(dataMap map[string]interface{}, arg interface{}, listenType int, apiKey string) (bool, error) {
	argMap, ok := arg.(map[string]interface{})
	if !ok {
		log.Error("Bitget数据推送参数格式错误 key:%s", apiKey)
		return false, nil
	}

	channel, exists := argMap["channel"]
	if !exists {
		log.Error("Bitget数据推送缺少channel参数 key:%s", apiKey)
		return false, nil
	}

	switch channel {
	case "account":
		return handleBitgetAccount(dataMap, listenType, apiKey)
	case "orders":
		return handleBitgetOrders(dataMap, listenType, apiKey)
	case "positions":
		return handleBitgetPositions(dataMap, listenType, apiKey)
	case "balance":
		return handleBitgetBalance(dataMap, listenType, apiKey)
	default:
		log.Info("收到Bitget未处理的频道数据 channel:%v key:%s", channel, apiKey)
	}

	return false, nil
}

// handleBitgetAccount 处理账户更新
func handleBitgetAccount(dataMap map[string]interface{}, listenType int, apiKey string) (bool, error) {
	log.Info("收到Bitget账户更新 listenType:%d key:%s", listenType, apiKey)

	data, exists := dataMap["data"]
	if !exists {
		return false, nil
	}

	// 这里可以根据需要实现具体的账户更新处理逻辑
	// 类似于binanceservice.ChangeSpotOrder或binanceservice.ChangeFutureOrder

	utility.SafeGo(func() {
		handleBitgetAccountUpdate(data, listenType, apiKey)
	})

	return false, nil
}

// handleBitgetOrders 处理订单更新
func handleBitgetOrders(dataMap map[string]interface{}, listenType int, apiKey string) (bool, error) {
	log.Info("收到Bitget订单更新 listenType:%d key:%s", listenType, apiKey)

	data, exists := dataMap["data"]
	if !exists {
		return false, nil
	}

	utility.SafeGo(func() {
		handleBitgetOrderUpdate(data, listenType, apiKey)
	})

	return false, nil
}

// handleBitgetPositions 处理持仓更新
func handleBitgetPositions(dataMap map[string]interface{}, listenType int, apiKey string) (bool, error) {
	log.Info("收到Bitget持仓更新 listenType:%d key:%s", listenType, apiKey)

	data, exists := dataMap["data"]
	if !exists {
		return false, nil
	}

	utility.SafeGo(func() {
		handleBitgetPositionUpdate(data, listenType, apiKey)
	})

	return false, nil
}

// handleBitgetBalance 处理余额更新
func handleBitgetBalance(dataMap map[string]interface{}, listenType int, apiKey string) (bool, error) {
	log.Info("收到Bitget余额更新 listenType:%d key:%s", listenType, apiKey)

	data, exists := dataMap["data"]
	if !exists {
		return false, nil
	}

	utility.SafeGo(func() {
		handleBitgetBalanceUpdate(data, listenType, apiKey)
	})

	return false, nil
}

// 具体的处理函数，可以根据业务需求实现

// handleBitgetAccountUpdate 处理账户更新的具体逻辑
func handleBitgetAccountUpdate(data interface{}, listenType int, apiKey string) {
	// TODO: 实现具体的账户更新处理逻辑
	log.Info("处理Bitget账户更新 listenType:%d key:%s data:%v", listenType, apiKey, data)
}

// handleBitgetOrderUpdate 处理订单更新的具体逻辑
func handleBitgetOrderUpdate(data interface{}, listenType int, apiKey string) {
	// TODO: 实现具体的订单更新处理逻辑
	log.Info("处理Bitget订单更新 listenType:%d key:%s data:%v", listenType, apiKey, data)

	// 这里可以参考binanceservice中的订单处理逻辑
	// 例如：更新数据库中的订单状态、触发回调等
}

// handleBitgetPositionUpdate 处理持仓更新的具体逻辑
func handleBitgetPositionUpdate(data interface{}, listenType int, apiKey string) {
	// TODO: 实现具体的持仓更新处理逻辑
	log.Info("处理Bitget持仓更新 listenType:%d key:%s data:%v", listenType, apiKey, data)
}

// handleBitgetBalanceUpdate 处理余额更新的具体逻辑
func handleBitgetBalanceUpdate(data interface{}, listenType int, apiKey string) {
	// TODO: 实现具体的余额更新处理逻辑
	log.Info("处理Bitget余额更新 listenType:%d key:%s data:%v", listenType, apiKey, data)
}

// getBitgetWsTypeName 获取WebSocket类型名称
func getBitgetWsTypeName(listenType int) string {
	switch listenType {
	case 0:
		return "现货"
	case 1:
		return "合约"
	default:
		return "未知"
	}
}
