from core import util
from core.logger import LogLevel, PrintLog
from core.qmt import qmtv
from core.sfgrid import bus_events
from core.sfgrid.bus_events import EventTradeTargetUpdate
import core.sfgrid.model as model
from core.eventbus import event_bus
from core.constants import OrderTypeBuy, OrderTypeSell, OrderTypeInit
from core.util import is_trading_time

from xtquant import xtconstant
from xtquant.xttype import XtOrderResponse, XtTrade
import threading
import core.eventbus as eBus


class SFGridStrategy:

    def __init__(self, tradeTarget: model.SFGridTradeTarget):
        self.tradeTarget:model.SFGridTradeTarget = tradeTarget
        event_bus.subscribe(eBus.MarketOrderCreated, self.onOrderCreateAsync)
        event_bus.subscribe(eBus.MarketOrderTraded, self.onOrderTrade)
        self.todayUpStopPrice=qmtv.dailyUpStop(tradeTarget.stock_code) # type: ignore
        self.todayDownStopPrice=qmtv.dailyDownStop(tradeTarget.stock_code) # type: ignore
        PrintLog(LogLevel.INFO, f'|- 标的{tradeTarget.targetName()}初始化: 停涨价 {self.todayUpStopPrice}, 停跌价 {self.todayDownStopPrice}')
        self.orderGrid = {} # grid index, order_seq | order_id
        self.loadExistOrders()
        self.enabledTrading(tradeTarget.enabled)  # type: ignore
        self.dataUpdateLock = threading.Lock()
    
    def loadExistOrders(self):
        orders = qmtv.queryPendingOrder(self.tradeTarget.stock_code, self.getName()) # type: ignore
        for order in orders:
            if order.strategy_name != self.getName():
                continue
            gridIdx = int(order.order_remark.split(',')[1])
            self.orderGrid[gridIdx] = order.order_id
            PrintLog(LogLevel.INFO, f'|- 标的[{self.tradeTarget.targetName()}] 初始化: 加载现有订单, grid-{gridIdx} order_id:{self.orderGrid[gridIdx]}')
    
    def printPendingOrder(self):
        for idx, order_id in self.orderGrid.items():
            PrintLog(LogLevel.DEBUG, f" {idx} : {order_id}")

    def onMarketActiveSwitch(self, isActive: bool):
        if isActive and self.tradeTarget.enabled:
            self.refreshGridOrder()
    
    def refreshGridOrder(self): # 下网格单
        if not qmtv.isMarketActive or not self.tradeTarget.enabled:
            PrintLog(LogLevel.INFO, f'|- 市场 {qmtv.isMarketActive}, 策略 {self.getName()} {self.tradeTarget.enabled}, 不下单')
            return

        currentIdx:int = 0
        
        orders = qmtv.queryPendingOrder(self.tradeTarget.stock_code, self.getName()) # type: ignore

        if self.tradeTarget.status == 0 and len([order for order in orders if order.order_remark == f'{OrderTypeInit},1,{self.tradeTarget.stock_code}']) == 0: # status == 0 表示已配置好交易参数，且不存在执行中的建仓单
            price = self.tradeTarget.getPriceGrid()[0]
            remark = f'{OrderTypeInit},1,{self.tradeTarget.stock_code}'
            tmpOrderSeq = qmtv.orderAsync(
                str(self.tradeTarget.stock_code), 
                self.tradeTarget.grid_volume,
                xtconstant.STOCK_BUY, 
                price,
                xtconstant.FIX_PRICE,
                remark, # remark  # type: ignore
                self.getName(), # strategy_name
            )
            self.orderGrid[1] = tmpOrderSeq # seq
            PrintLog(LogLevel.INFO, f'|- 标的[{self.tradeTarget.targetName()}] 初始化: 建仓单，建仓价: {price:.3f}')
        elif self.tradeTarget.status == 1: # 下网格单
            currentIdx = self.tradeTarget.grid_index # type: ignore
            orders = qmtv.queryPendingOrder(self.tradeTarget.stock_code, self.getName()) # type: ignore

            # 向上下一单，向下下一单
            if currentIdx > 0: # 可以下空单
                sellIdx = currentIdx - 1
                sellPrice = self.tradeTarget.getPriceGrid()[sellIdx]
                remark = f'{OrderTypeSell},{sellIdx},{self.tradeTarget.stock_code}'
                if len([order for order in orders if order.order_remark == remark]) == 0: # 网格节点没有卖单，下单
                    # 不存在策略内同价位订单，下单
                    tmpOrderSeq = qmtv.orderAsync(
                                str(self.tradeTarget.stock_code), 
                                self.tradeTarget.grid_volume,
                                xtconstant.STOCK_SELL, 
                                sellPrice,
                                xtconstant.FIX_PRICE,
                                remark, # remark  # type: ignore
                                self.getName(), # strategy_name
                                )
                    self.orderGrid[sellIdx] = tmpOrderSeq # seq
                    PrintLog(LogLevel.INFO, f'|- 标的[{self.tradeTarget.targetName()}] 网格策略: 下空单，价格: {sellPrice:.3f}')
                else:
                    PrintLog(LogLevel.INFO, f'|- 标的[{self.tradeTarget.targetName()}] 网格策略: 已存在同价位空单，跳过下单')
            if currentIdx < len(self.tradeTarget.getPriceGrid()) - 1: # 可以下多单
                print(f'length: {len(self.tradeTarget.getPriceGrid())}, currentIdx = {currentIdx}')
                buyIdx = currentIdx + 1
                buyPrice = self.tradeTarget.getPriceGrid()[buyIdx]
                remark = f'{OrderTypeInit},{buyIdx},{self.tradeTarget.stock_code}'
                if len([order for order in orders if order.order_type == xtconstant.STOCK_BUY and order.price == buyPrice]) == 0:
                    tmpOrderSeq = qmtv.orderAsync(
                            str(self.tradeTarget.stock_code), 
                            self.tradeTarget.grid_volume,
                            xtconstant.STOCK_BUY, 
                            buyPrice,
                            xtconstant.FIX_PRICE,
                            remark, # remark  # type: ignore
                            self.getName(), # strategy_name
                            )
                    self.orderGrid[buyIdx] = tmpOrderSeq # seq
                    PrintLog(LogLevel.INFO, f'|- 标的[{self.tradeTarget.targetName()}] 网格策略: 下多单，价格: {buyPrice:.3f}')
                else:
                    PrintLog(LogLevel.INFO, f'|- 标的[{self.tradeTarget.targetName()}] 网格策略: 已存在同价位多单，跳过下单')
            else:
                PrintLog(LogLevel.INFO, f'|- 标的[{self.tradeTarget.targetName()}] 网格策略: 已过下边界，停止多单交易')
    
    def deleteTradeTarget(self, tradeTarget:model.SFGridTradeTarget):
        PrintLog(LogLevel.INFO, f'|- 标的{tradeTarget.targetName()}信息删除: START')
        self.dataUpdateLock.acquire()
        try:
            tradeTarget.delete_instance()
            event_bus.publish(bus_events.EventTradeTargetDeleted, tradeTarget)
            PrintLog(LogLevel.INFO, f'|- 标的{tradeTarget.targetName()}信息删除: END')
        finally:
            self.dataUpdateLock.release()

    def enabledTrading(self, enabled: bool) -> model.SFGridTradeTarget:
        self.tradeTarget.enabled = enabled  # type: ignore

        if enabled:
            PrintLog(LogLevel.INFO, f"  |- 标的{self.tradeTarget.targetName()}交易启动, 持仓量：{self.tradeTarget.current_position}")
            if self.tradeTarget.status == 0: # 未建仓
                PrintLog(LogLevel.INFO, f"  |- 标的{self.tradeTarget.targetName()}初始状态, 设置网格序号 1,")
                self.tradeTarget.grid_index = 1  # pyright: ignore[reportAttributeAccessIssue]
            else: # 已建仓
                # 交易阶段，检查仓位，检查现有订单
                PrintLog(LogLevel.INFO, f"  |- 标的{self.tradeTarget.targetName()}已有仓位或非初始状态 无需建初始仓 当前仓位: {self.tradeTarget.current_position} 状态: {self.tradeTarget.status}")
                minRequirePosition:int = self.tradeTarget.grid_volume * int(self.tradeTarget.grid_index)  # type: ignore
                if minRequirePosition <= int(self.tradeTarget.current_position):  # type: ignore
                    PrintLog(LogLevel.INFO, f'  |- 仓位检查: 持仓需求充足, (gridVolume*gridIndex)={minRequirePosition}, 当前持仓：{self.tradeTarget.current_position}')
                else:
                    PrintLog(LogLevel.INFO, f'  |- 仓位检查: 持仓需求不足, (gridVolume*gridIndex)={minRequirePosition}, 当前持仓：{self.tradeTarget.current_position}, 交易启动失败')
                    self.tradeTarget.enabled = False  # type: ignore
            self.refreshGridOrder()
        else:
            orders = qmtv.queryPendingOrder(self.tradeTarget.stock_code, self.getName()) # type: ignore
            for order in orders:
                qmtv.xttrader.cancel_order_stock_async(qmtv.account, order.order_id)
            print(f'  |- 取消未成交订单 {len(orders)}')
            print(f"  |- 标的{self.tradeTarget.targetName()}交易监控暂停")
        
        self.saveProxy()
        return self.tradeTarget

    def isEnabled(self) -> bool:
        print(f'|- 检查交易状态[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}] - {self.tradeTarget.enabled}')
        return bool(self.tradeTarget.enabled)  # 修复返回类型问题

    def onOrderCreateAsync(self, response:XtOrderResponse): # 下单成功回调，更新orderID到 self.orderGrid
        remark = response.order_remark.split(',')
        stockCode = remark[2] # 从remark中获取stockCode
        if response.strategy_name != self.getName() or len(remark) < 3 or self.tradeTarget.stock_code != stockCode:
            return
        self.dataUpdateLock.acquire()
        try:
            gridIdx = remark[1] # 从remark中获取gridIdx
            PrintLog(LogLevel.INFO, f"委托创建通知 onOrderCreateAsync[{self.tradeTarget.targetName()}]: {response.order_id}")
            self.orderGrid[gridIdx] = response.order_id
            PrintLog(LogLevel.INFO, f"委托创建通知 onOrderCreateAsync 更新 grid-{gridIdx} seq:{response.seq} -> order_id:{response.order_id}")
        except Exception as e:
            PrintLog(LogLevel.ERROR, f"|- 委托创建通知 onOrderCreateAsync[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}]: {response.order_id} - {str(e)}")
        finally:
            self.dataUpdateLock.release()

    def onOrderTrade(self, trade:XtTrade): # TODO 委托成交通知,处理成交后网格切换
        remark = trade.order_remark.split(',')
        if trade.strategy_name != self.getName() or len(remark) < 3 or self.tradeTarget.stock_code != trade.stock_code:
            return
        PrintLog(LogLevel.INFO, f'|- 委托成交通知[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}-{trade.order_id}] : {trade.order_id}')

        self.dataUpdateLock.acquire()
        try:
            orderType = trade.order_remark.split(',')[0]
            gridIdx = trade.order_remark.split(',')[1] # 从remark中获取gridIdx
            type:str = ""
            if orderType == OrderTypeInit:
                PrintLog(LogLevel.INFO, f'|- 委托成交通知[{self.tradeTarget.targetName()}-{trade.order_id}] - 建仓单成交')
                self.tradeTarget.status = 1  # type: ignore
                self.tradeTarget.init_price = trade.traded_price  # type: ignore
                self.tradeTarget.grid_index = 1  # type: ignore
                type = "建仓单"
            else:
                PrintLog(LogLevel.INFO, f'|- 委托成交通知[{self.tradeTarget.targetName()}-{trade.order_id}] - 网格单成交')
                oriIdx = self.tradeTarget.grid_index
                if gridIdx > self.tradeTarget.grid_index:
                    type = "下移一格"
                    self.tradeTarget.grid_index +=1
                elif gridIdx < self.tradeTarget.grid_index:
                    type = "上移一格"
                    self.tradeTarget.grid_match_count += 1
                    self.tradeTarget.grid_total_profit += self.tradeTarget.grid_size * trade.traded_volume
                    self.tradeTarget.grid_index -= 1
                else:
                    type = "保持格, 理论上不应该输出"
                PrintLog(LogLevel.INFO, f'|- 委托成交通知[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} - 原网格位置 {oriIdx}, 现网格位置 {self.tradeTarget.grid_index}')
            
            self.saveProxy()
            del self.orderGrid[gridIdx]
            PrintLog(LogLevel.INFO, f"|- 成交报告[{self.tradeTarget.targetName()}] : ====================================")
            PrintLog(LogLevel.INFO, f"|- 标的[{self.tradeTarget.targetName()}] {type}-单号{trade.order_id}已成交 ")
            PrintLog(LogLevel.INFO, f'   成交价: {trade.traded_price} 成交量: {trade.traded_volume}')
            PrintLog(LogLevel.INFO, f'   手续费  : {trade.commission:.3f}')
            self.refreshGridOrder() # 更新网格订单
        finally:
            self.dataUpdateLock.release()


    def getName(self):
        return "SFGRID"
    
    def saveProxy(self):
        rc = self.tradeTarget.save()
        event_bus.publish(EventTradeTargetUpdate, self.tradeTarget)
        return rc