import sys
import asyncio
from PySide6.QtWidgets import QApplication, QMainWindow, QLabel, QWidget, QVBoxLayout, QTabWidget
from PySide6.QtCore import Qt, QTimer, QThread, Signal

from .dashboard import DashboardPanel
from .strategy_panel import StrategyPanel
from .positions_panel import PositionsPanel
from .orders_panel import OrdersPanel
from .logs_panel import LogsPanel


class AsyncWorker(QThread):
    def __init__(self, loop: asyncio.AbstractEventLoop, target_coro, kwargs):
        super().__init__()
        self.loop = loop
        self.target_coro = target_coro
        self.kwargs = kwargs

    def run(self):
        asyncio.set_event_loop(self.loop)
        self.loop.run_until_complete(self.target_coro(**self.kwargs))
        self.loop.close()


class MainWindow(QMainWindow):
    def __init__(
        self, broker, data_feed, db_session, order_manager, risk_manager, strategy_engine, settings, backend_runner
    ):
        super().__init__()
        self.broker = broker
        self.data_feed = data_feed
        self.db_session = db_session
        self.order_manager = order_manager
        self.risk_manager = risk_manager
        self.strategy_engine = strategy_engine
        self.settings = settings
        self.backend_runner = backend_runner

        self.setWindowTitle("Algo Trader")
        self.setGeometry(100, 100, 1600, 900)

        # Create main tab widget
        self.tabs = QTabWidget()
        self.tabs.setTabPosition(QTabWidget.North)

        # Create panels
        self.dashboard_panel = DashboardPanel()
        self.strategy_panel = StrategyPanel()
        self.positions_panel = PositionsPanel()
        self.orders_panel = OrdersPanel()
        self.logs_panel = LogsPanel()

        # Add panels to tabs
        self.tabs.addTab(self.dashboard_panel, "Dashboard")
        self.tabs.addTab(self.strategy_panel, "Strategies")
        self.tabs.addTab(self.positions_panel, "Positions")
        self.tabs.addTab(self.orders_panel, "Orders")
        self.tabs.addTab(self.logs_panel, "Logs")

        self.setCentralWidget(self.tabs)

        # --- Asyncio Integration ---
        self.loop = asyncio.get_event_loop()
        if self.loop.is_running():
            self.loop = asyncio.new_event_loop()

        self.backend_worker = AsyncWorker(
            loop=self.loop,
            target_coro=self.backend_runner,
            kwargs={
                "broker": self.broker,
                "data_feed": self.data_feed,
                "db_session": self.db_session,
                "order_manager": self.order_manager,
                "risk_manager": self.risk_manager,
                "strategy_engine": self.strategy_engine,
                "settings": self.settings,
            },
        )
        self.backend_worker.start()

    def closeEvent(self, event):
        if self.data_feed:
            asyncio.run_coroutine_threadsafe(self.data_feed.close(), self.loop)
        if self.db_session:
            self.db_session.close()
        self.loop.call_soon_threadsafe(self.loop.stop)
        self.backend_worker.wait() # Wait for the backend thread to finish
        super().closeEvent(event)
