import os
from PySide6.QtGui import QAction,QDragEnterEvent, QDropEvent
from PySide6.QtWidgets import QMainWindow,QSplitter
from gui.widgets.media_viewr import MediaViewer
from gui.widgets.right_panel import RightPanel


from lib.custom_logger import get_logger
logger = get_logger()



class MainWindow(QMainWindow):
    """アプリメインウィンドウ"""
    def __init__(self):
        super().__init__()
        self.setWindowTitle("CV Studio (Prototype)")
        self.resize(1200, 700)
        logger.info("MainWindow initialized")

        # メニューを構築
        self._build_menu()


        # 半々に分割
        self.viewer = MediaViewer()
        self.right = RightPanel()

        splitter = QSplitter()        
        splitter.addWidget(self.viewer)
        splitter.addWidget(self.right)
        splitter.setSizes([self.width()//2, self.width()//2])
        self.setCentralWidget(splitter)



    def _build_menu(self):
        # File 
        menu_file = self.menuBar().addMenu("File")
        open_act = QAction("Open...", self)
        # open_act.triggered.connect(self.viewer.open_file)
        menu_file.addAction(open_act)
        # hr
        menu_file.addSeparator()

        # exit
        exit_act = QAction("Exit", self)
        exit_act.setShortcut("Ctrl+Q")
        exit_act.triggered.connect(self.on_exit)
        menu_file.addAction(exit_act)
        logger.info("Main Window Menu built.")


    # events----
    def on_exit(self):
        """アプリ終了アクション"""
        logger.info("Exit action triggered")
        self.close()   # QMainWindow.close() を呼ぶとアプリ終了






    # ---- D&D ----
    # ルートウィンドウにもD&D(フォルダから直接落とせるように)
    def dragEnterEvent(self, e: QDragEnterEvent):
        if e.mimeData().hasUrls():
            e.acceptProposedAction()

    def dropEvent(self, e: QDropEvent):
        urls = e.mimeData().urls()
        if urls:
            path = urls[0].toLocalFile()
            if os.path.isfile(path):
                self.viewer.load(path)
