import os
from import_qt_mode import *
from UI.manual_image_matching.manual_image_matching_main import Ui_Form
from module.view_control.manual_image_matching.m_image_matching_data import MatchingData
from module.view_control.manual_image_matching.goods_art_image_list import GoodsArtImageList
from module.view_control.MineQWidget import MineQWidget, LoadingMask


class MainMatchingWindow(MineQWidget, QWidget):
    mask_load_sign = Signal(bool)

    def __init__(self, root_path=None):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        # 安装事件过滤器
        self.installEventFilter(self)
        self.ui.splitter.setSizes([900, 450])

        # 移除 Qt.WindowStaysOnTopHint 标志以取消置顶
        self.setWindowFlags(self.windowFlags() & ~Qt.WindowStaysOnTopHint)
        # 默认最大化显示窗口
        self.showMaximized()

        self.matching_data = MatchingData()
        if not root_path:
            # root_path = r"D:\phpstudy_pro\WWW\auto_photo\output\2024-11-18"
            root_path = r"D:\MyDocuments\PythonCode\MyPython\red_dragonfly\deal_pics\auto_capture_V2\auto_photo\output\2024-12-20-2 - 副本"

        self.root_path = root_path
        print(self.size())
        # 加载图片
        self.state = 0
        self.__mask = LoadingMask(self)
        self.mask_load_sign.connect(self.mask_load)
        self.installEventFilter(self.__mask)
        self.__mask.is_end_sign.connect(self.hide_mask)

        QTimer.singleShot(1000, self.load_images)
        self.ui.select_path.clicked.connect(self.selectPath)
        self.show()

    def mask_load(self, flag):
        print("mask_load")
        if flag:
            self.__mask.show()
        else:
            self.__mask.close()

    def load_images(self):
        self.show_all_goods_list(self.root_path)

    def show_all_goods_list(self, root_path):
        # for i in self.ui.action_widget_list.findChildren(GoodsArtImageList):
        #     i.deleteLater()



        self.mask_load_sign.emit(True)
        for data in self.matching_data.get_all_folder(root_path):
            # folder_name = data["folder_name"]
            # original_path = data["original_path"]
            # cut_out_path = data["cut_out_path"]
            goods_art_image_list = GoodsArtImageList(goods_art_no_folder_path=data["goods_art_no_folder_path"],
                                                     parent=self.ui.action_widget_list,
                                                     windows=self,
                                                     label_color = data["label_color"],
                                                     )
            self.ui.verticalLayout_4.insertWidget(self.ui.verticalLayout_4.count() - 1, goods_art_image_list)
            goods_art_image_list.select_clearn_all_sign.connect(self.clearn_all_label_select)

        self.mask_load_sign.emit(False)

    # 清除所有已选
    def clearn_all_label_select(self, original_path):
        for goods_art_image_list_item in self.ui.action_widget_list.findChildren(GoodsArtImageList):
            goods_art_image_list_item: GoodsArtImageList
            if goods_art_image_list_item.original_path != original_path:
                if goods_art_image_list_item.has_selected:
                    goods_art_image_list_item.clearn_all_label_select()
                    goods_art_image_list_item.has_selected = False

        for goods_art_image_list_item in self.ui.action_widget_list_2.findChildren(GoodsArtImageList):
            goods_art_image_list_item: GoodsArtImageList
            if goods_art_image_list_item.original_path != original_path:
                if goods_art_image_list_item.has_selected:
                    goods_art_image_list_item.clearn_all_label_select()
                    goods_art_image_list_item.has_selected = False

    def selectPath(self):
        # a = QMessageBox.question(
        #     self,
        #     "继续",
        #     "如选择的文件夹图片很多，则此处会非常耗时",
        #     QMessageBox.Yes | QMessageBox.No,
        # )
        # if a != QMessageBox.Yes:
        #     return

        path = QFileDialog.getExistingDirectory(self, "选择文件夹", "")
        if not path:
            return
        if os.path.isfile(path):
            return

        _, folder_name = os.path.split(path)

        for widget in self.ui.action_widget_list_2.findChildren(GoodsArtImageList):
            widget.deleteLater()
        self.mask_load_sign.emit(True)
        goods_art_image_list = GoodsArtImageList(goods_art_no_folder_path=path,
                                                 parent=self.ui.action_widget_list_2,
                                                 is_goods_art_no_folder=False,
                                                 )
        goods_art_image_list.select_clearn_all_sign.connect(self.clearn_all_label_select)
        self.ui.verticalLayout_5.insertWidget(self.ui.verticalLayout_5.count() - 1, goods_art_image_list)
        self.mask_load_sign.emit(False)

    def show_big_image(self, icon):
        icon = icon.scaled(self.ui.show_big_image.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
        self.ui.show_big_image.setPixmap(icon)

    def eventFilter(self, obj, event):
        if obj == self and event.type() == QEvent.WindowStateChange:
            if self.isMaximized():
                print("检测到窗口最大化")
                QTimer.singleShot(1000, self.show_again)
        return super().eventFilter(obj, event)

    def show_again(self, *args):
        print("检测到窗口最大化2")
        print(self.size())
        self.updateGeometry()
        # self.resizeEvent(QResizeEvent(self.size(), QSize()))
        # self.adjustSize()
        self.ui.action_widget_list.updateGeometry()
        print(self.size())

        for i in self.ui.action_widget_list.findChildren(GoodsArtImageList):
            i.repaint()
        for i in self.ui.action_widget_list_2.findChildren(GoodsArtImageList):
            i.repaint()

    def resizeEvent(self, event):
        super().resizeEvent(event)


if __name__ == '__main__':
    app = QApplication([])
    window = MainMatchingWindow()
    window.resize(1000, 800)
    window.show()
    app.exec()
