#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：SlidePositionView.py\n
 @DateTime: 2025/7/26 11:32\n
 @Author  ：xsh
"""
from abc import abstractmethod
from PyQt5 import Qt
from AppInterfaces.UiInterface import UiInterface, AppConvert, AppUtils
from AppCore.Contexts import *
from AppCore.DBModels import *
from AppCore.Levels import *
from AppUtils.UiUtils import UiUtils

class SlidePositionView(Qt.QWidget, UiInterface):
    currentDataUpdated: Qt.pyqtSignal = Qt.pyqtSignal(object)
    InfoObjectiveLevel  : Qt.QLabel             #
    InfoPosition_X      : Qt.QLabel             #
    InfoPosition_Y      : Qt.QLabel             #
    InfoPosition_Z      : Qt.QLabel             #
    EditObjectiveLevel  : Qt.QDoubleSpinBox     #
    EditPosition_X      : Qt.QDoubleSpinBox     #
    EditPosition_Y      : Qt.QDoubleSpinBox     #
    EditPosition_Z      : Qt.QDoubleSpinBox     #
    PositionButton      : Qt.QPushButton        #

    def __init__(self, parent: Qt.QWidget = None):
        super(SlidePositionView, self).__init__(parent=parent)
        self.Initialization('SlidePositionView', self, parent)
        self.initWidgetStyle()
        self.flushFrame()
    def initConnect(self):
        super(SlidePositionView, self).initConnect()
        TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
        self.currentDataUpdated.connect(slot=self.currentDataChangedSlot, type=TYPE)

    def currentDataChangedSlot(self, model: SlideModelInterface):
        if isinstance(model, SlideModelInterface):
            self.setPosition(model.ObjectiveLevel, model.Position_X, model.Position_Y, model.Position_Z)

    def setPosition(self, _ObjectiveLevel : int  ,_Position_X     : float,_Position_Y     : float,_Position_Z     : float):
        try:
            self.EditObjectiveLevel.setValue(_ObjectiveLevel)
            self.EditPosition_X.setValue(_Position_X)
            self.EditPosition_Y.setValue(_Position_Y)
            self.EditPosition_Z.setValue(_Position_Z)
        except Exception as e:
            AppUtils.print_Exception(e)




    def getPosition(self) -> (int, float, float, float):
        return int  (self.EditObjectiveLevel  .value()),float(self.EditPosition_X      .value()),float(self.EditPosition_Y      .value()),float(self.EditPosition_Z      .value())

    def flushFrame(self):
        self.InfoObjectiveLevel  .setText('物镜编号')
        self.InfoPosition_X      .setText('Ｘ轴位置')
        self.InfoPosition_Y      .setText('Ｙ轴位置')
        self.InfoPosition_Z      .setText('Ｚ轴位置')

    def initWidgetStyle(self):
        self.setStyleSheet(UiUtils.getDrawDataStyle())

    def initValue(self):
        super(SlidePositionView, self).initValue()
        self.initWindows()

    def initLayouts(self):
        if self.hasWindowLayouts:
            return
        layout: Qt.QHBoxLayout = Qt.QHBoxLayout()
        layout.setSpacing(1)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(self.InfoObjectiveLevel  , 1)
        layout.addWidget(self.EditObjectiveLevel  , 1)
        layout.addWidget(self.InfoPosition_X      , 1)
        layout.addWidget(self.EditPosition_X      , 1)
        layout.addWidget(self.InfoPosition_Y      , 1)
        layout.addWidget(self.EditPosition_Y      , 1)
        layout.addWidget(self.InfoPosition_Z      , 1)
        layout.addWidget(self.EditPosition_Z      , 1)
        layout.addWidget(self.PositionButton      , 1)
        self.setLayout(layout)

    def initProperty(self):
        super(SlidePositionView, self).initProperty()
        # self.InfoObjectiveLevel  .setAlignment(Qt.Qt.AlignRight | Qt.Qt.AlignVCenter)
        # self.InfoPosition_X      .setAlignment(Qt.Qt.AlignRight | Qt.Qt.AlignVCenter)
        # self.InfoPosition_Y      .setAlignment(Qt.Qt.AlignRight | Qt.Qt.AlignVCenter)
        # self.InfoPosition_Z      .setAlignment(Qt.Qt.AlignRight | Qt.Qt.AlignVCenter)
        self.EditObjectiveLevel  .setRange(1, 7)
        self.EditPosition_X      .setRange(-2.00, 100.00 )
        self.EditPosition_Y      .setRange(-2.00, 100.00 )
        self.EditPosition_Z      .setRange( 0.00, 7000000)
        self.PositionButton      .setText('当前位置')
        self.InfoObjectiveLevel  .setStyleSheet(UiUtils.getButtonStyle())
        self.EditObjectiveLevel  .setStyleSheet(UiUtils.getButtonStyle())
        self.InfoPosition_X      .setStyleSheet(UiUtils.getButtonStyle())
        self.EditPosition_X      .setStyleSheet(UiUtils.getButtonStyle())
        self.InfoPosition_Y      .setStyleSheet(UiUtils.getButtonStyle())
        self.EditPosition_Y      .setStyleSheet(UiUtils.getButtonStyle())
        self.InfoPosition_Z      .setStyleSheet(UiUtils.getButtonStyle())
        self.EditPosition_Z      .setStyleSheet(UiUtils.getButtonStyle())
        self.PositionButton      .setStyleSheet(UiUtils.getButtonStyle())
    def initWindows(self):
        self.initInfoObjectiveLevel  ()
        self.initInfoPosition_X      ()
        self.initInfoPosition_Y      ()
        self.initInfoPosition_Z      ()
        self.initEditObjectiveLevel  ()
        self.initEditPosition_X      ()
        self.initEditPosition_Y      ()
        self.initEditPosition_Z      ()
        self.initPositionButton      ()

    def initInfoObjectiveLevel (self):
        if not self.hasInfoObjectiveLevel:
            self.InfoObjectiveLevel = Qt.QLabel         (self)

    def initInfoPosition_X     (self):
        if not self.hasInfoPosition_X    :
            self.InfoPosition_X     = Qt.QLabel         (self)

    def initInfoPosition_Y     (self):
        if not self.hasInfoPosition_Y    :
            self.InfoPosition_Y     = Qt.QLabel         (self)

    def initInfoPosition_Z     (self):
        if not self.hasInfoPosition_Z    :
            self.InfoPosition_Z     = Qt.QLabel         (self)

    def initEditObjectiveLevel (self):
        if not self.hasEditObjectiveLevel:
            self.EditObjectiveLevel = Qt.QSpinBox        (self)

    def initEditPosition_X     (self):
        if not self.hasEditPosition_X    :
            self.EditPosition_X     = Qt.QDoubleSpinBox (self)

    def initEditPosition_Y     (self):
        if not self.hasEditPosition_Y    :
            self.EditPosition_Y     = Qt.QDoubleSpinBox (self)

    def initEditPosition_Z     (self):
        if not self.hasEditPosition_Z    :
            self.EditPosition_Z     = Qt.QDoubleSpinBox (self)

    def initPositionButton     (self):
        if not self.hasPositionButton    :
            self.PositionButton     = Qt.QPushButton    (self)


    @property
    def hasInfoObjectiveLevel  (self) -> bool:
        return hasattr(self, 'InfoObjectiveLevel' ) and isinstance(self.InfoObjectiveLevel  , Qt.QLabel         )

    @property
    def hasInfoPosition_X      (self) -> bool:
        return hasattr(self, 'InfoPosition_X'     ) and isinstance(self.InfoPosition_X      , Qt.QLabel         )

    @property
    def hasInfoPosition_Y      (self) -> bool:
        return hasattr(self, 'InfoPosition_Y'     ) and isinstance(self.InfoPosition_Y      , Qt.QLabel         )

    @property
    def hasInfoPosition_Z      (self) -> bool:
        return hasattr(self, 'InfoPosition_Z'     ) and isinstance(self.InfoPosition_Z      , Qt.QLabel         )

    @property
    def hasEditObjectiveLevel  (self) -> bool:
        return hasattr(self, 'EditObjectiveLevel' ) and isinstance(self.EditObjectiveLevel  , Qt.QSpinBox )

    @property
    def hasEditPosition_X      (self) -> bool:
        return hasattr(self, 'EditPosition_X'     ) and isinstance(self.EditPosition_X      , Qt.QDoubleSpinBox )

    @property
    def hasEditPosition_Y      (self) -> bool:
        return hasattr(self, 'EditPosition_Y'     ) and isinstance(self.EditPosition_Y      , Qt.QDoubleSpinBox )

    @property
    def hasEditPosition_Z      (self) -> bool:
        return hasattr(self, 'EditPosition_Z'     ) and isinstance(self.EditPosition_Z      , Qt.QDoubleSpinBox )

    @property
    def hasPositionButton      (self) -> bool:
        return hasattr(self, 'PositionButton'     ) and isinstance(self.PositionButton      , Qt.QPushButton    )
