from PySide6.QtWidgets import (QApplication, QMainWindow, QWidget, QGridLayout, 
                              QPushButton, QSizePolicy, QSplitter, QToolBar)
from PySide6.QtGui import QFontDatabase
from PySide6.QtCore import Signal, Qt

import os
from info_core.defines import *
from info_core.MyQtClass import ConfigComboBoxGroup, FolderDropWidget, DraggableLine


class ReportGeneratorUI(QMainWindow):
    send_baogao_choose_info = Signal(list[str])
    
    def __init__(self):
        super().__init__()
        # 加载字体
        self.load_font()
        
        # 设置窗口属性
        self.setWindowTitle("报告生成器")
        self.setMinimumSize(WINDOW_MIN_WIDTH, WINDOW_MIN_HEIGHT)
        
        # 主窗口部件
        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)
        
        # 主布局
        self.main_layout = QGridLayout(self.central_widget)
        self.main_layout.setSpacing(MAIN_LAYOUT_SPACING)
        self.main_layout.setContentsMargins(*MAIN_LAYOUT_MARGINS)
        
        # 初始化UI
        self.init_ui()
        
    
    def load_font(self):
        """加载自定义字体"""
        if os.path.exists(FONT_PATH):
            font_id = QFontDatabase.addApplicationFont(FONT_PATH)
            if font_id == -1:
                print("字体加载失败，将使用系统默认字体")
        else:
            print(f"字体文件未找到: {FONT_PATH}，将使用系统默认字体")
        
    def init_ui(self):
        """初始化所有UI组件"""
        # 第一行：项目信息和人员配置
        self.project_group = ConfigComboBoxGroup("项目基本信息")
        self.staff_group = ConfigComboBoxGroup("单次检查配置信息", is_project=False)
        # 第二行：导入图片路径、填写机组信息
        self.picture_group = FolderDropWidget()
        # self.image_analysis = 
        # self.main_layout.addWidget(self.image_analysis, 1, 1)
        # 第三行：生成报告按钮（跨两列）
        self.create_generate_button()
        self.generate_btn.setEnabled(False)
        
        # 创建一个垂直分割器
        self.splitter = QSplitter(Qt.Vertical)
        self.splitter.setStyleSheet(SPLITTER_STYLE)
        # 创建顶部和底部容器
        top_container = QWidget()
        top_container.setLayout(QGridLayout())
        top_container.layout().addWidget(self.project_group, 0, 0)
        top_container.layout().addWidget(self.staff_group, 0, 1)

        middle_container = QWidget()
        middle_container.setLayout(QGridLayout())
        middle_container.layout().addWidget(self.picture_group, 0, 0)

        # 添加部件到分割器
        self.splitter.addWidget(top_container)
        self.splitter.addWidget(middle_container)

        # 设置主布局
        self.main_layout.addWidget(self.splitter, 0, 0, 2, 2)  # 占据前两行两列
        self.main_layout.addWidget(self.generate_btn, 2, 0, 1, 2)

        # 设置分割器初始比例
        self.splitter.setStretchFactor(0, 1)
        self.splitter.setStretchFactor(1, 4)
        
        self.toolbar = QToolBar()
        self.addToolBar(self.toolbar)
        self.toolbar.setMovable(False)
        self.toolbar.setFloatable(False)
        new_action = self.toolbar.addAction("重置布局比例")
        self.toolbar.addSeparator()
        new_action.triggered.connect(self.reset_splitter)
    
    def reset_splitter(self):
        """重置分割器的比例"""
        total_size = sum(self.splitter.sizes())  # 获取当前总大小
        self.splitter.setSizes([
            int(total_size * 0.2),  # 第一部分占 20%（比例 1:4）
            int(total_size * 0.8)   # 第二部分占 80%
        ])

    def on_generate_path_selected(self, path):
        self.generate_btn.setEnabled(True)
    
    # search_file_list = []
    # if self.image_analysis.check_is_waibu:
    #     search_file_list.append("外汇总")
    # if self.image_analysis.check_is_neibu:
    #     search_file_list.append("内汇总")
    # if self.image_analysis.check_is_fanglei:
    #     search_file_list.append("防汇总")
    # self.send_baogao_choose_info.emit(search_file_list)
    
    def create_button(self, text):
        """创建统一风格的按钮"""
        btn = QPushButton(text)
        btn.setStyleSheet(BUTTON_STYLE)
        btn.setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT)
        return btn
    
    def create_generate_button(self):
        """创建生成报告按钮"""
        self.generate_btn = QPushButton("生成报告")
        self.generate_btn.setStyleSheet(PRIMARY_BUTTON_STYLE)
        self.generate_btn.setFixedHeight(50)
        self.generate_btn.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
    

if __name__ == "__main__":
    app = QApplication([])
    window = ReportGeneratorUI()
    window.show()
    app.exec()