#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：MicroscopeFrame.py
@Description：
@Author：XSH
@Time：2025/11/18 9:57
@Department：研发部
@Copyright：©2025 乐辰生物
"""
import sys
import os
from PyQt5                                              import Qt
from AppSettings.Settings                               import *
from AppInterfaces.utils                                import *
from AppCore.Levels                                     import *
from AppUtils.UiUtils                                   import UiUtils
from AppInterfaces.UiInterface                          import UiInterface, AppUiProperty, AppUtils, AppConvert
from AppBackEnd.AppBusinessInterface                    import AppBusinessInterface
from AppHeadEnd.ControllerViews.MicroscopeController    import MicroscopeController     #
from AppHeadEnd.ControllerFrames.ControlFrameInterface  import ControlFrameInterface    #

class MicroscopeFrame(Qt.QWidget, ControlFrameInterface):
    Controller: MicroscopeController
    def __init__(self, business: AppBusinessInterface, parent: Qt.QWidget = None):
        super(MicroscopeFrame, self).__init__(parent=parent)
        self.Business = business if isinstance(business, AppBusinessInterface) else None
        self.Initialization('MicroscopeFrame', self, parent)
        self.initWindowIcon()
        self.initWidgetStyle()



    def initValue(self):
        super(MicroscopeFrame, self).initValue()
        self.initController()

    def initConnect(self):
        super(MicroscopeFrame, self).initConnect()
        TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
        self.Controller.ReadObjective   .clicked.connect(slot=self.SlotReadObjective   , type=TYPE)
        self.Controller.SendObjective   .clicked.connect(slot=self.SlotSendObjective   , type=TYPE)
        self.Controller.ReadPosition    .clicked.connect(slot=self.SlotReadPosition    , type=TYPE)
        self.Controller.SendPosition    .clicked.connect(slot=self.SlotSendPosition    , type=TYPE)
        self.Controller.ReadLAMP        .clicked.connect(slot=self.SlotReadLAMP        , type=TYPE)
        self.Controller.SendLAMP        .clicked.connect(slot=self.SlotSendLAMP        , type=TYPE)
        self.Controller.ReadAPBL        .clicked.connect(slot=self.SlotReadAPBL        , type=TYPE)
        self.Controller.SendAPBL        .clicked.connect(slot=self.SlotSendAPBL        , type=TYPE)
        self.Controller.InitButton      .clicked.connect(slot=self.SlotInitButton      , type=TYPE)
        self.Controller.UnInitButton    .clicked.connect(slot=self.SlotUnInitButton    , type=TYPE)
        self.Controller.OriginButton    .clicked.connect(slot=self.SlotOriginButton    , type=TYPE)

    def SlotReadObjective   (self):
        if not self.isAvailable():
            return
        if not self.hasMicroscope:
            return
        value: float
        isWrong: int
        isWrong, value = self.microscope.get_objective()
        if RECODE_SUCCEED == isWrong and isinstance(value, (int, float)):
            self.Controller.EditObjective.setValue(int(value))


    def SlotSendObjective   (self):
        if not self.isAvailable():
            return
        if not self.hasMicroscope:
            return

        value: int = self.Controller.EditObjective.value()
        if isinstance(value, (int, float)):
            self.microscope.set_objective(value)


    def SlotReadPosition    (self):
        if not self.isAvailable():
            return
        if not self.hasMicroscope:
            return
        value: float
        isWrong: int
        isWrong, value = self.microscope.get_position()
        if RECODE_SUCCEED == isWrong and isinstance(value, (int, float)):
            self.Controller.EditPosition.setValue(value)

    def SlotSendPosition    (self):
        if not self.isAvailable():
            return
        if not self.hasMicroscope:
            return
        value: float = self.Controller.EditPosition.value()
        if isinstance(value, (int, float)):
            self.microscope.waitmove(value)

    def SlotReadLAMP        (self):
        if not self.isAvailable():
            return
        if not self.hasMicroscope:
            return
        value: float
        isWrong: int
        isWrong, value = self.microscope.get_lamp()
        if isinstance(value, (int, float)):
            self.Controller.EditLAMP.setValue(value)

    def SlotSendLAMP        (self):
        if not self.isAvailable():
            return
        if not self.hasMicroscope:
            return
        value: float = self.Controller.EditLAMP.value()
        self.microscope.set_lamp(value)


    def SlotReadAPBL        (self):
        if not self.isAvailable():
            return
        if not self.hasMicroscope:
            return
        value: float
        isWrong: int
        isWrong, value = self.microscope.get_apbl()
        if isinstance(value, (int, float)):
            self.Controller.EditAPBL.setValue(value)

    def SlotSendAPBL        (self):
        if not self.isAvailable():
            return
        if not self.hasMicroscope:
            return
        value: float = self.Controller.EditLFBL.value()
        self.microscope.set_lfbl(value)


    def SlotInitButton      (self):
        if not self.isAvailable():
            return
        if not self.hasMicroscope:
            return
        self.microscope.initManager()
        self.microscope.ReOpen()

    def SlotUnInitButton    (self):
        if not self.isAvailable():
            return
        if not self.hasMicroscope:
            return
        self.microscope.deleManager()

    def SlotOriginButton    (self):
        if not self.isAvailable():
            return
        if not self.hasMicroscope:
            return
        self.microscope.waithome()

    def initProperty(self):
        super(MicroscopeFrame, self).initProperty()


    def initLayouts(self):
        super(MicroscopeFrame, self).initLayouts()
        if self.hasWindowLayouts:
            return
        m_layout: Qt.QGridLayout = Qt.QGridLayout()
        m_layout.setSpacing(1)
        m_layout.setContentsMargins(0, 0, 0, 0)

        m_layout.addWidget(self.Controller)
        self.setLayout(m_layout)


    @property
    def hasController(self) -> bool:
        return hasattr(self, 'Controller') and isinstance(self.Controller, MicroscopeController)

    def initController(self):
        if not self.hasController:
            self.Controller = MicroscopeController(self)


