#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：SlideOperation.py
@Description：
@Author：XSH
@Time：2025/12/23 15:34
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from abc                                    import abstractmethod                       #
from PyQt5                                  import Qt                                   #
from AppInterfaces.UiInterface              import UiInterface                          #
from AppInterfaces.globalSlot               import globalSlot, AppSlot                  #
from AppCore.Contexts                       import *                                    #
from AppCore.DBModels                       import *                                    #
from AppCore.Levels                         import *                                    #
from AppUtils.utils                         import *                                    #
from AppSettings.Settings                   import *                                    #
from AppHeadEnd.Commons.OperationViews.SlideFrame import SlideFrame
class SlideOperation(Qt.QWidget, UiInterface):
    Number: int
    Slides: list[SlideFrame]
    clicked: Qt.pyqtSignal = Qt.pyqtSignal(object)

    def __init__(self, parent: Qt.QWidget = None):
        super(SlideOperation, self).__init__(parent)
        self.Initialization('SlideOperation', self, parent)


    def initValue(self):
        super(SlideOperation, self).initValue()
        self.initNumber()
        self.initSlides()

    def initProperty(self):
        super(SlideOperation, self).initProperty()
        self.setFixedSize(250 * 3, 750 * 0.6)

    def initLayouts(self):
        if self.hasWindowLayouts:
            return
        main_layout: Qt.QHBoxLayout = Qt.QHBoxLayout()
        main_layout.setSpacing(40)
        main_layout.setContentsMargins(40, 40, 40, 40)
        for slide in self.Slides:
            main_layout.addWidget(slide)
        self.setLayout(main_layout)

    @property
    def hasSlides(self) -> bool:
        return self.has_field('Slides', list) and len(self.Slides) == 5

    @property
    def hasNumber(self) -> bool:
        return self.has_field('Number', int)


    @property
    def number(self) -> int:
        self.initNumber()
        return self.Number

    def slide(self, number: int) -> SlideFrame:
        if isinstance(number, int) and number in TrayLevel.all():
            return self.Slides[number - 1]
        return None


    def initSlides(self):
        if not self.hasSlides:
            self.Slides = []
            for tray_number in TrayLevel.all():
                frame: SlideFrame = SlideFrame(number=tray_number, parent=self)
                frame.clicked.connect(self.SlotSlideClicked, Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
                self.Slides .append(frame)
    def SlotSlideClicked(self, number: int):
        if isinstance(number, int) and number in TrayLevel.all():
            self.Number = number
            self.clicked.emit(number)

    def initNumber(self):
        if not self.hasNumber:
            self.Number = TrayLevel.TraySlide1

