#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：DrawPositionView.py
@Description：
@Author：XSH
@Time：2025/12/18 14:06
@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 *                                  #

class DrawPositionView(Qt.QWidget, UiInterface):
    PositionChangedSignal: Qt.pyqtSignal = Qt.pyqtSignal(object, object, object, object)
    PositionRefreshSignal: Qt.pyqtSignal = Qt.pyqtSignal()
    InfoObjective: Qt.QPushButton                           #
    InfoPositionX: Qt.QPushButton                           #
    InfoPositionY: Qt.QPushButton                           #
    InfoPositionZ: Qt.QPushButton                           #
    EditObjective: Qt.QSpinBox                              #
    EditPositionX: Qt.QDoubleSpinBox                        #
    EditPositionY: Qt.QDoubleSpinBox                        #
    EditPositionZ: Qt.QDoubleSpinBox                        #
    PositionGroup: Qt.QGroupBox                             #
    InfoMoveStep : Qt.QPushButton                           #
    EditMoveStep : Qt.QDoubleSpinBox                        #
    InfoMicStep  : Qt.QPushButton                           #
    EditMicStep  : Qt.QSpinBox                              #
    UPMicMove    : Qt.QPushButton                           #
    EditMicMove  : Qt.QDoubleSpinBox                        #
    DownMicMove  : Qt.QPushButton                           #
    def __init__(self, parent: Qt.QWidget = None):
        super(DrawPositionView, self).__init__(parent)
        self.Initialization("", self, parent)

    def mousePressEvent(self, ev: Qt.QMouseEvent) -> None:
        self.PositionRefreshSignal.emit()
        return super(DrawPositionView, self).mousePressEvent(ev)

    def initValue(self):
        super(DrawPositionView, self).initValue()
        self.initInfoObjective()
        self.initInfoPositionX()
        self.initInfoPositionY()
        self.initInfoPositionZ()
        self.initEditObjective()
        self.initEditPositionX()
        self.initEditPositionY()
        self.initEditPositionZ()
        self.initInfoMoveStep ()
        self.initEditMoveStep ()
        self.initInfoMicStep  ()
        self.initEditMicStep  ()
        self.initPositionGroup()
        self.initUPMicMove    ()
        self.initEditMicMove  ()
        self.initDownMicMove  ()


    def getMicMoveStep(self) -> int:
        return int(LEICA_MM_STEP * self.EditMicMove.value())

    def initProperty(self):
        super(DrawPositionView, self).initProperty()
        self.InfoObjective.setText('当前物镜')
        self.InfoPositionX.setText('Ｘ轴位置')
        self.InfoPositionY.setText('Ｙ轴位置')
        self.InfoPositionZ.setText('Ｚ轴位置')
        self.InfoMoveStep .setText('移动步长')
        self.UPMicMove    .setText('上升')
        self.DownMicMove  .setText('下降')
        self.InfoObjective.setFlat(True)
        self.InfoPositionX.setFlat(True)
        self.InfoPositionY.setFlat(True)
        self.InfoPositionZ.setFlat(True)
        self.InfoMicStep  .setFlat(True)
        self.InfoMoveStep .setFlat(True)

        self.EditPositionX.setRange(0.0, 147.0)
        self.EditPositionY.setRange(0.0, 60.0)
        self.EditPositionZ.setRange(0, 8800000)
        self.EditMicMove  .setRange(0.0, 8800000.0)

        self.EditMicMove.setSuffix('mm')
        self.EditMicMove.setDecimals(4)
        self.EditMicMove.setSingleStep(0.1)
        self.EditMoveStep .setRange(0.0, 45.0)
        self.EditMoveStep .setDecimals(6)
        self.EditMoveStep .setSuffix('mm')
        self.EditMoveStep .setValue(0.1)
        self.EditPositionX.setDecimals(6)
        self.EditPositionY.setDecimals(6)
        self.EditPositionX.setSuffix('mm')
        self.EditPositionY.setSuffix('mm')
        self.InfoMicStep  .setText('Ｚ轴步长')
        self.EditMicStep  .setRange(0, 1700000)
        self.EditMicStep  .setSingleStep(2630)

        self.UPMicMove    .setFixedHeight(35)
        self.EditMicMove  .setFixedHeight(35)
        self.DownMicMove  .setFixedHeight(35)
        self.InfoObjective.setFixedHeight(35)
        self.InfoPositionX.setFixedHeight(35)
        self.InfoPositionY.setFixedHeight(35)
        self.InfoPositionZ.setFixedHeight(35)
        self.EditObjective.setFixedHeight(35)
        self.EditPositionX.setFixedHeight(35)
        self.EditPositionY.setFixedHeight(35)
        self.EditPositionZ.setFixedHeight(35)
        self.InfoMoveStep .setFixedHeight(35)
        self.EditMoveStep .setFixedHeight(35)
        self.InfoMicStep  .setFixedHeight(35)
        self.EditMicStep  .setFixedHeight(35)
        # self.EditObjective.setEnabled(False)
        # self.EditPositionX.setEnabled(False)
        # self.EditPositionY.setEnabled(False)
        # self.EditPositionZ.setEnabled(False)

        self.PositionGroup.setTitle('位置信息')


    def initConnect(self):
        super(DrawPositionView, self).initConnect()
        TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
        self.PositionChangedSignal.connect(self.SlotPositionChangedSignal, TYPE)

    def SlotPositionChangedSignal(self, objective, x, y, z):
        if isinstance(objective, (int, float)):
            self.EditObjective.setValue(int( objective))
        if isinstance(x, (int, float)):
            self.EditPositionX.setValue(round(float(x), 3))
        if isinstance(y, (int, float)):
            self.EditPositionY.setValue(round(float(y), 3))
        if isinstance(z, (int, float)):
            self.EditPositionZ.setValue(round(float(z), 3))

    def initLayouts(self):
        if self.hasWindowLayouts:
            return
        main_layout: Qt.QGridLayout = Qt.QGridLayout()
        main_layout.setSpacing(1)
        main_layout.setContentsMargins(5, 5, 5, 5)

        group_layout: Qt.QGridLayout = Qt.QGridLayout()
        group_layout.setSpacing(1)
        group_layout.setContentsMargins(10, 10, 10, 10)
        group_layout.setAlignment(Qt.Qt.AlignTop)
        group_layout.addWidget(self.InfoObjective, 0, 0, 1, 1 )
        group_layout.addWidget(self.EditObjective, 0, 1, 1, -1)
        group_layout.addWidget(self.InfoPositionX, 1, 0, 1, 1 )
        group_layout.addWidget(self.EditPositionX, 1, 1, 1, -1)
        group_layout.addWidget(self.InfoPositionY, 2, 0, 1, 1 )
        group_layout.addWidget(self.EditPositionY, 2, 1, 1, -1)
        group_layout.addWidget(self.InfoPositionZ, 3, 0, 1, 1 )
        group_layout.addWidget(self.EditPositionZ, 3, 1, 1, -1)
        group_layout.addWidget(self.InfoMoveStep , 4, 0, 1, 1 )
        group_layout.addWidget(self.EditMoveStep , 4, 1, 1, -1)
        group_layout.addWidget(self.InfoMicStep  , 5, 0, 1, 1 )
        group_layout.addWidget(self.EditMicStep  , 5, 1, 1, -1)
        group_layout.addWidget(self.UPMicMove    , 6, 0, 1, 1)
        group_layout.addWidget(self.EditMicMove  , 6, 1, 1, 1)
        group_layout.addWidget(self.DownMicMove  , 6, 2, 1, 1)
        self.PositionGroup.setLayout(group_layout)

        main_layout.addWidget(self.PositionGroup, 0, 0, 1, 1)
        self.setLayout(main_layout)


    def setPosition(self, objective: int, x: float, y: float, z: float):
        if isinstance(objective, (int, float)):
            self.EditObjective.setValue(int( objective))
        if isinstance(x, (int, float)):
            self.EditPositionX.setValue(round(float(x), 4))
        if isinstance(y, (int, float)):
            self.EditPositionY.setValue(round(float(y), 4))
        if isinstance(z, (int, float)):
            self.EditPositionZ.setValue(round(float(z), 4))

    def setPosition_xy(self,x: float, y: float):
        if isinstance(x, (int, float)):
            self.EditPositionX.setValue(round(float(x), 4))
        if isinstance(y, (int, float)):
            self.EditPositionY.setValue(round(float(y), 4))

    def setObjective(self,objective: int):
        if isinstance(objective, (int, float)):
            self.EditObjective.setValue(int(objective))

    def setPosition_z(self,z: float):
        if isinstance(z, (int, float)):
            self.EditPositionZ.setValue(round(float(z), 4))


    def initInfoObjective(self):
        if not self.hasInfoObjective:
            self.InfoObjective = Qt.QPushButton          ()

    def initInfoPositionX(self):
        if not self.hasInfoPositionX:
            self.InfoPositionX = Qt.QPushButton          ()

    def initInfoPositionY(self):
        if not self.hasInfoPositionY:
            self.InfoPositionY = Qt.QPushButton          ()

    def initInfoPositionZ(self):
        if not self.hasInfoPositionZ:
            self.InfoPositionZ = Qt.QPushButton          ()

    def initEditObjective(self):
        if not self.hasEditObjective:
            self.EditObjective = Qt.QSpinBox        ()

    def initEditPositionX(self):
        if not self.hasEditPositionX:
            self.EditPositionX = Qt.QDoubleSpinBox  ()

    def initEditPositionY(self):
        if not self.hasEditPositionY:
            self.EditPositionY = Qt.QDoubleSpinBox  ()

    def initEditPositionZ(self):
        if not self.hasEditPositionZ:
            self.EditPositionZ = Qt.QDoubleSpinBox  ()

    def initPositionGroup(self):
        if not self.hasPositionGroup:
            self.PositionGroup = Qt.QGroupBox       ()

    def initInfoMoveStep(self):
        if not self.hasInfoMoveStep:
            self.InfoMoveStep = Qt.QPushButton     ()

    def initEditMoveStep(self):
        if not self.hasEditMoveStep:
            self.EditMoveStep = Qt.QDoubleSpinBox  ()

    def initInfoMicStep (self):
        if not self.hasInfoMicStep :
            self.InfoMicStep  = Qt.QPushButton  ()
    def initEditMicStep (self):
        if not self.hasEditMicStep :
            self.EditMicStep  = Qt.QSpinBox     ()

    def initUPMicMove    (self):
        if not self.hasUPMicMove    :
            self.UPMicMove     = Qt.QPushButton()

    def initEditMicMove  (self):
        if not self.hasEditMicMove  :
            self.EditMicMove   = Qt.QDoubleSpinBox   ()

    def initDownMicMove  (self):
        if not self.hasDownMicMove  :
            self.DownMicMove   = Qt.QPushButton()

    @property
    def microscope_step(self) -> int:
        return 0 if not self.hasEditMicStep else int(self.EditMicStep.value())

    @property
    def objective(self) -> int:
        return ObjectiveLevel.Objective1 if not self.hasEditObjective else int(self.EditObjective.value())
    @property
    def movestep(self) -> float:
        return 0.0 if not self.hasEditMoveStep else round(self.EditMoveStep.value(), 6)

    @property
    def hasInfoObjective(self) -> bool:
        return self.has_field('InfoObjective', Qt.QLabel         )

    @property
    def hasInfoPositionX(self) -> bool:
        return self.has_field('InfoPositionX', Qt.QLabel         )

    @property
    def hasInfoPositionY(self) -> bool:
        return self.has_field('InfoPositionY', Qt.QLabel         )

    @property
    def hasInfoPositionZ(self) -> bool:
        return self.has_field('InfoPositionZ', Qt.QLabel         )

    @property
    def hasEditObjective(self) -> bool:
        return self.has_field('EditObjective', Qt.QSpinBox       )

    @property
    def hasEditPositionX(self) -> bool:
        return self.has_field('EditPositionX', Qt.QDoubleSpinBox )

    @property
    def hasEditPositionY(self) -> bool:
        return self.has_field('EditPositionY', Qt.QDoubleSpinBox )

    @property
    def hasEditPositionZ(self) -> bool:
        return self.has_field('EditPositionZ', Qt.QDoubleSpinBox )

    @property
    def hasPositionGroup(self) -> bool:
        return self.has_field('PositionGroup', Qt.QGroupBox      )

    @property
    def hasInfoMoveStep(self) -> bool:
        return self.has_field('InfoMoveStep', Qt.QPushButton   )

    @property
    def hasEditMoveStep(self) -> bool:
        return self.has_field('EditMoveStep', Qt.QDoubleSpinBox)

    @property
    def hasInfoMicStep  (self) -> bool:
        return self.has_field('InfoMicStep', Qt.QPushButton)
    @property
    def hasEditMicStep  (self) -> bool:
        return self.has_field('EditMicStep', Qt.QSpinBox   )

    @property
    def hasUPMicMove    (self) -> bool:
        return self.has_field('UPMicMove'  , Qt.QPushButton   )

    @property
    def hasEditMicMove  (self) -> bool:
        return self.has_field('EditMicMove', Qt.QDoubleSpinBox      )

    @property
    def hasDownMicMove  (self) -> bool:
        return self.has_field('DownMicMove', Qt.QPushButton   )
