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 import constants
from core.eventbus import event_bus
from core.constants import OrderTypeBuy, OrderTypeInit, OrderTypeSell
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
        self.enabledTrading(tradeTarget.enabled)  # type: ignore
        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
        self.refreshPlanPrice()
        self.dataUpdateLock = threading.Lock()
    
    def updateTradeTarget(self, inTradeTarget:model.SFGridTradeTarget):
        # PrintLog(LogLevel.INFO, f'|- 标的{self.tradeTarget.targetName()}信息更新: START')
        # self.dataUpdateLock.acquire()
        # PrintLog(LogLevel.INFO ,f'|- 标的{self.tradeTarget.targetName()}信息更新: LOCKED')
        # try:
        self.tradeTarget = inTradeTarget
        self.refreshPlanPrice()
        # finally:
            # PrintLog(LogLevel.INFO ,f'|- 标的{self.tradeTarget.targetName()}信息更新: UNLOCKED')
            # self.dataUpdateLock.release()
            # self.refreshPlanPrice()
            # PrintLog(LogLevel.INFO ,f'|- 标的{self.tradeTarget.targetName()}信息更新: END')
    
    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:
            print(f"  |- 标的{self.tradeTarget.targetName()}交易启动, 持仓量：{self.tradeTarget.current_position}")
            if self.tradeTarget.status == 0: # 未建仓
                print(f"  |- 标的{self.tradeTarget.targetName()}初始状态, 设置网格序号 1,")
                self.tradeTarget.grid_index = 1  # pyright: ignore[reportAttributeAccessIssue]
                self.refreshPlanPrice()
            else: # 已建仓
                # 交易阶段，检查仓位，检查现有订单
                print(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
                    print(f'  |- 仓位检查: 持仓需求充足, (gridVolume*gridIndex)={minRequirePosition}, 当前持仓：{self.tradeTarget.current_position}')
                else:
                    print(f'  |- 仓位检查: 持仓需求不足, (gridVolume*gridIndex)={minRequirePosition}, 当前持仓：{self.tradeTarget.current_position}, 交易启动失败')
                    self.tradeTarget.enabled = False  # type: ignore
        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 onDataUpdate(self, inTradeTarget:model.SFGridTradeTarget):

        if not is_trading_time():
            return

        if not self.tradeTarget.enabled: # 策略中止，自动交易暂停
            return
        
        self.dataUpdateLock.acquire()
        try:
            lastPrice = inTradeTarget.market_price
            orderPrice:float = -1
            orderType = -1
            index: int =  self.tradeTarget.grid_index  # pyright: ignore[reportAssignmentType]
            orderRemark= ""
            status = "未建初始仓" if self.tradeTarget.status == 0 else "已建初始仓"
            PrintLog(LogLevel.INFO, f'|- 市价更新[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}] - 当前持仓   : \t{self.tradeTarget.current_position}')
            PrintLog(LogLevel.INFO, f'|- 市价更新[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}] - 当前价格   : \t{lastPrice}')
            PrintLog(LogLevel.INFO, f'|- 市价更新[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}] - 计划卖出价 : \t{self.tradeTarget.plan_sell_price:.3f}')
            PrintLog(LogLevel.INFO, f'|- 市价更新[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}] - 网格价格({index}): \t{self.tradeTarget.getPriceGrid()[index]:.3f}')
            PrintLog(LogLevel.INFO, f'|- 市价更新[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}] - 计划买入价 : \t{self.tradeTarget.plan_buy_price:.3f}')
            if self.tradeTarget.enabled and self.tradeTarget.status == 0 and lastPrice <= inTradeTarget.getPriceGrid()[1]: # 已启用，未建仓，准备建仓单信息
                PrintLog(LogLevel.INFO, f'|- 市价更新[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}] - 准备建仓单信息')
                orderPrice =  inTradeTarget.getPriceGrid()[1]
                orderType = xtconstant.STOCK_BUY
                orderRemark = OrderTypeInit
            elif self.tradeTarget.enabled and self.tradeTarget.status == 1 and self.tradeTarget.plan_buy_price > 0 and lastPrice <= self.tradeTarget.plan_buy_price: 
                # 已启用，已建仓，准备网格多单信息
                PrintLog(LogLevel.INFO, f'|- 市价更新[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}] - 准备网格多单信息')
                orderPrice = self.tradeTarget.plan_buy_price # type: ignore
                orderType = xtconstant.STOCK_BUY
                orderRemark = OrderTypeBuy
            elif self.tradeTarget.enabled and self.tradeTarget.status == 1 and self.tradeTarget.plan_buy_price > 0 and lastPrice >= self.tradeTarget.plan_sell_price:
                # 已启用，已建仓，准备网格空单信息
                PrintLog(LogLevel.INFO, f'|- 市价更新[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}] - 准备网格空单信息')
                orderPrice = self.tradeTarget.plan_sell_price # type: ignore
                orderType = xtconstant.STOCK_SELL
                orderRemark = OrderTypeSell

            if orderType != -1:
                orders = qmtv.queryPendingOrder(str(self.tradeTarget.stock_code), self.getName())
                if len([order for order in orders if order.order_type == orderType and order.price == orderPrice]) > 0:
                    # 已存在未交易的多单
                    PrintLog(LogLevel.INFO, f'|- 市价更新[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}] - 已存在未交易的{"多单" if orderType == xtconstant.STOCK_BUY else "空单"}，不重复下单')
                else:
                    PrintLog(LogLevel.INFO, f'|- 市价更新[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}] - 下网格{"多单" if orderType == xtconstant.STOCK_BUY else "空单"}')
                    self.tradeTarget.current_order_no = qmtv.orderAsync(
                            str(self.tradeTarget.stock_code), 
                            self.tradeTarget.grid_volume,
                            orderType, 
                            orderPrice,
                            xtconstant.FIX_PRICE,
                            orderRemark, # remark  # type: ignore
                            self.getName(), # strategy_name
                            )
                orderTypeName = ""
                if orderRemark == OrderTypeBuy:
                    orderTypeName = "多单"
                elif orderRemark == OrderTypeSell:
                    orderTypeName = "空单"
                elif orderRemark == OrderTypeInit:
                    orderTypeName = "建仓单"
                gridBasePrice  =  -1 if index>=len(inTradeTarget.getPriceGrid()) or index < 0 else inTradeTarget.getPriceGrid()[int(index)]  # pyright: ignore[reportArgumentType]
                PrintLog(LogLevel.INFO, f'|- {orderTypeName}委托[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}] - 单号 {self.tradeTarget.current_order_no}, 网格基准价 {gridBasePrice:.3f}, 下单价 {orderPrice:.3f}, 下单量 {self.tradeTarget.grid_volume}')
        finally:
            self.saveProxy()
            self.dataUpdateLock.release()

    def onOrderCreateAsync(self, response:XtOrderResponse):
        self.dataUpdateLock.acquire()
        try:
            if response.strategy_name == self.getName() and response.seq == self.tradeTarget.current_order_no:
                print(f"委托创建通知 onOrderCreateAsync[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}]: {response.order_id}")
                self.tradeTarget.current_order_no = response.order_id
                self.saveProxy()
        finally:
            self.dataUpdateLock.release() 

    def onOrderTrade(self, trade:XtTrade):
        if trade.strategy_name != self.getName():
            PrintLog(LogLevel.WARNING, f"|- 委托回调 投资备注 {trade.stock_code}-{trade.instrument_name} {trade.strategy_name} 不匹配 {trade.order_remark}")
            return
        PrintLog(LogLevel.INFO, f'|- 委托成交通知[{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}-{trade.order_id}]:START, {trade.order_id}')

        self.dataUpdateLock.acquire()
        try:
            type:str = ""
            if self.tradeTarget.status == 0 and trade.order_id == self.tradeTarget.current_order_no and trade.order_remark == constants.OrderTypeInit:
                # 此时为建仓成交
                self.tradeTarget.current_position = int(self.tradeTarget.current_position) + trade.traded_volume # 当前持仓数，账户原有持仓不在策略范围内  # type: ignore
                self.tradeTarget.last_trade_price = float(trade.traded_price)  # type: ignore
                self.tradeTarget.grid_index = 1  # type: ignore
                self.tradeTarget.status = 1  # type: ignore
                type = "建初始仓"
            elif trade.order_id == self.tradeTarget.current_order_no and self.tradeTarget.status == 1 and trade.order_type == xtconstant.STOCK_SELL:  # type: ignore
                # 上涨一格：此时空单成交
                self.tradeTarget.current_position = int(self.tradeTarget.current_position) - trade.traded_volume  # type: ignore
                self.tradeTarget.last_trade_price = float(trade.traded_price)  # type: ignore
                self.tradeTarget.grid_index = int(self.tradeTarget.grid_index) - 1  # type: ignore
                type = "上涨一格"
            elif trade.order_id == self.tradeTarget.current_order_no and self.tradeTarget.status == 1 and trade.order_type == xtconstant.STOCK_BUY:  # type: ignore
                # 下跌一格：此时多单成交
                self.tradeTarget.current_position = int(self.tradeTarget.current_position) + trade.traded_volume  # type: ignore
                self.tradeTarget.last_trade_price = float(trade.traded_price)  # type: ignore
                self.tradeTarget.grid_index = int(self.tradeTarget.grid_index) + 1  # type: ignore
                type = "下跌一格"
            else:
                type = "其他异常状态"
            
            self.refreshPlanPrice()
            self.printTradeReport(trade, type)
        finally:
            self.dataUpdateLock.release()
    
    def printTradeReport(self, trade:XtTrade, type:str):
        PrintLog(LogLevel.INFO, f"|- 成交报告[{self.tradeTarget.targetName()}] : ====================================")
        PrintLog(LogLevel.INFO, f"|- 标的[{self.tradeTarget.targetName()}] {type}-单号{self.tradeTarget.current_order_no}已成交 ")
        PrintLog(LogLevel.INFO, f'   成交价: {trade.traded_price} 成交量: {trade.traded_volume}')
        PrintLog(LogLevel.INFO, f'   手续费  : {trade.commission:.3f}')


    def refreshPlanPrice(self):
        buyIdx = 0
        sellIdx= 0
        if self.tradeTarget.status == 0: # 未建仓
            self.tradeTarget.grid_index = 0 # type: ignore
            buyIdx = 1
            sellIdx = 0
        else:
            buyIdx: int = self.tradeTarget.grid_index + 1  # pyright: ignore[reportAssignmentType]
            sellIdx: int = self.tradeTarget.grid_index - 1

        if self.tradeTarget.grid_index > 0: # 可以下空单
            self.tradeTarget.plan_sell_price = float(self.tradeTarget.getPriceGrid()[sellIdx])  # pyright: ignore[reportAttributeAccessIssue]
        else:
            self.tradeTarget.plan_sell_price = -1.0  # type: ignore

        if self.tradeTarget.grid_index < len(self.tradeTarget.getPriceGrid()) - 1:
            self.tradeTarget.plan_buy_price = float(self.tradeTarget.getPriceGrid()[buyIdx])  # pyright: ignore[reportAttributeAccessIssue]
        else:
            self.tradeTarget.plan_buy_price = -1.0  # pyright: ignore[reportAttributeAccessIssue]
        event_bus.publish(EventTradeTargetUpdate, self.tradeTarget)
    
    def getName(self):
        return "SFGRID"
    
    def saveProxy(self):
        rc = self.tradeTarget.save()
        event_bus.publish(EventTradeTargetUpdate, self.tradeTarget)
        return rc