#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：HitBotChamberPositionView.py\n
 @DateTime: 2025/7/25 16:14\n
 @Author  ：xsh
"""
from abc import abstractmethod
from PyQt5 import Qt
from AppInterfaces.SingletonInterface import SingletonInterface
from AppInterfaces.UiInterface import UiInterface, AppConvert, AppUtils
from AppCore.Contexts import *
from AppCore.DBModels import *
from AppCore.Levels import *
from AppUtils.UiUtils import UiUtils
from decimal import Decimal
class HitBotChamberPositionView(Qt.QWidget, UiInterface, SingletonInterface):
    Locker = Qt.QMutex()
    InfoChamberNumber   : Qt.QLabel         #
    InfoRowNumber       : Qt.QLabel         #
    InfoXPosition       : Qt.QLabel         #
    InfoYPosition       : Qt.QLabel         #
    InfoZPosition       : Qt.QLabel         #
    InfoRPosition       : Qt.QLabel         #
    InfoStep            : Qt.QLabel         #
    InfoSplit           : Qt.QLabel         #
    EditChamberNumber   : Qt.QComboBox      #
    EditRowNumber       : Qt.QComboBox      #
    EditXPosition       : Qt.QDoubleSpinBox #
    EditYPosition       : Qt.QDoubleSpinBox #
    EditZPosition       : Qt.QDoubleSpinBox #
    EditRPosition       : Qt.QDoubleSpinBox #
    EditStep            : Qt.QDoubleSpinBox #
    EditSplit           : Qt.QDoubleSpinBox #
    ChangedRowButton    : Qt.QPushButton    #
    ChangedGroupButton  : Qt.QPushButton    #
    def __init__(self, parent: Qt.QWidget = None):
        super(HitBotChamberPositionView, self).__init__(parent=parent)
        self.Initialization('HitBotChamberPositionView', self, parent)
        self.initWidgetStyle()

    @classmethod
    def GetInstance(cls, *args, **kwargs) -> 'HitBotChamberPositionView':
        return super(HitBotChamberPositionView, cls).GetInstance(*args, **kwargs)

    def initConnect(self):
        super(HitBotChamberPositionView, self).initConnect()
        TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
        self.ChangedRowButton   .clicked.connect(slot=self.SlotChangedRowButton   , type=TYPE)
        self.ChangedGroupButton .clicked.connect(slot=self.SlotChangedGroupButton , type=TYPE)
        self.EditChamberNumber   .currentIndexChanged.connect(slot=self.SlotEditChamberNumber, type=TYPE)
        self.EditRowNumber       .currentIndexChanged.connect(slot=self.SlotEditRowNumber    , type=TYPE)

    def SlotEditChamberNumber (self):
        self.flushFrame()

    def SlotEditRowNumber     (self):
        self.flushFrame()

    def changedChamberRowPosition(self, ChamberGroupNumber: int, ChamberRowNumber: int, xPosition: float,yPosition: float,zPosition: float,rPosition: float, split: float):
        try:
            if not isinstance(ChamberRowNumber, int) or not isinstance(ChamberGroupNumber, int):
                return
            l_model: HitBotCheckSlideLAction = HitBotCheckSlideLAction.get( HitBotCheckSlideLAction.ChamberGroupNumber == ChamberGroupNumber, HitBotCheckSlideLAction.ChamberRowNumber == ChamberRowNumber)
            r_model: HitBotCheckSlideRAction = HitBotCheckSlideRAction.copyInstance(l_model)
            l_model.xPosition = xPosition
            l_model.yPosition = yPosition
            l_model.zPosition = zPosition
            l_model.rPosition = rPosition
            l_model.ChamberRowNumber    = ChamberRowNumber
            l_model.ChamberGroupNumber  = ChamberGroupNumber
            r_model.xPosition           = float(Decimal(f'{xPosition}') - Decimal(f'{split}'))
            r_model.yPosition           = yPosition
            r_model.zPosition           = zPosition
            r_model.rPosition           = rPosition
            r_model.ChamberRowNumber    = ChamberRowNumber
            r_model.ChamberGroupNumber  = ChamberGroupNumber
            l_buffer: dict[str, object] = l_model.to_field(False)
            r_buffer: dict[str, object] = r_model.to_field(False)
            # l_buffer.pop('ChamberGroupNumber')
            # l_buffer.pop('ChamberRowNumber')
            # r_buffer.pop('ChamberGroupNumber')
            # r_buffer.pop('ChamberRowNumber')
            HitBotCheckSlideLAction.model_update(l_buffer, and_(HitBotCheckSlideLAction.ChamberGroupNumber == ChamberGroupNumber, HitBotCheckSlideLAction.ChamberRowNumber == ChamberRowNumber))
            HitBotCheckSlideRAction.model_update(r_buffer, and_(HitBotCheckSlideRAction.ChamberGroupNumber == ChamberGroupNumber, HitBotCheckSlideRAction.ChamberRowNumber == ChamberRowNumber))

        except Exception as e:
            AppUtils.print_Exception(e)





    def SlotChangedRowButton   (self):

        try:
            if not self.hasEditChamberNumber or not self.hasEditRowNumber:
                return
            ChamberGroupNumber  : int = AppConvert.ObjectConvert(self.EditChamberNumber.currentText(), int)
            ChamberRowNumber    : int = AppConvert.ObjectConvert(self.EditRowNumber.currentText(), int)
            xPosition: float = self.EditXPosition.value()
            yPosition: float = self.EditYPosition.value()
            zPosition: float = self.EditZPosition.value()
            rPosition: float = self.EditRPosition.value()
            if not isinstance(ChamberRowNumber, int) or not isinstance(ChamberGroupNumber, int):
                return
            if not isinstance(xPosition, (float, int)) or not isinstance(yPosition, (float, int)):
                return
            if not isinstance(zPosition, (float, int)) or not isinstance(rPosition, (float, int)):
                return
            self.changedChamberRowPosition(ChamberGroupNumber, ChamberRowNumber, xPosition, yPosition, zPosition, rPosition)
        except Exception as e:
            AppUtils.print_Exception(e)

    def SlotChangedGroupButton (self):
        pass
        try:
            if not self.hasEditChamberNumber or not self.hasEditRowNumber:
                return
            ChamberGroupNumber  : int = AppConvert.ObjectConvert(self.EditChamberNumber.currentText(), int)
            ChamberRowNumber    : int = AppConvert.ObjectConvert(self.EditRowNumber.currentText(), int)
            xPosition: float = self.EditXPosition.value()
            yPosition: float = self.EditYPosition.value()
            zPosition: float = self.EditZPosition.value()
            rPosition: float = self.EditRPosition.value()
            split    : float = self.EditSplit.value()
            step     : float = self.EditStep.value()
            _xPosition: float
            _yPosition: float
            _zPosition: float
            _rPosition: float
            if not isinstance(ChamberRowNumber, int) or not isinstance(ChamberGroupNumber, int):
                return
            if not isinstance(xPosition, (float, int)) or not isinstance(yPosition, (float, int)):
                return
            if not isinstance(zPosition, (float, int)) or not isinstance(rPosition, (float, int)):
                return

            for RowNumber in range(1, 21):
                _xPosition: float = xPosition
                _yPosition: float = yPosition
                _zPosition: float = Decimal(f'{zPosition}') - Decimal(f'{step}') * (RowNumber - 1)
                _rPosition: float = rPosition
                self.changedChamberRowPosition(ChamberGroupNumber, RowNumber, _xPosition, _yPosition, _zPosition, _rPosition, split)
        except Exception as e:
            AppUtils.print_Exception(e)

    def flushFrame(self):
        super(HitBotChamberPositionView, self).flushFrame()

        try:
            if not self.hasEditChamberNumber or not self.hasEditRowNumber:
                return
            ChamberGroupNumber  : int = AppConvert.ObjectConvert(self.EditChamberNumber.currentText(), int)
            ChamberRowNumber    : int = AppConvert.ObjectConvert(self.EditRowNumber.currentText(), int)
            if not isinstance(ChamberRowNumber, int) or not isinstance(ChamberGroupNumber, int):
                return
            model: HitBotCheckSlideLAction = HitBotCheckSlideLAction.get(HitBotCheckSlideLAction.ChamberGroupNumber == ChamberGroupNumber, HitBotCheckSlideLAction.ChamberRowNumber == ChamberRowNumber)
            if not isinstance(model, HitBotCheckSlideLAction):
                return
            self.EditXPosition.setValue(model.xPosition)
            self.EditYPosition.setValue(model.yPosition)
            self.EditZPosition.setValue(model.zPosition)
            self.EditRPosition.setValue(model.rPosition)
        except Exception as e:
            AppUtils.print_Exception(e)



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

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


    def initLayouts(self):
        if self.hasWindowLayouts:
            return
        layout: Qt.QVBoxLayout = Qt.QVBoxLayout()
        layout.setSpacing(1)
        layout.setContentsMargins(1, 0, 1, 0)
        row_1: Qt.QHBoxLayout = Qt.QHBoxLayout()
        row_1.setSpacing(1)
        row_1.setContentsMargins(1, 0, 1, 0)
        row_2: Qt.QHBoxLayout = Qt.QHBoxLayout()
        row_2.setSpacing(1)
        row_2.setContentsMargins(1, 0, 1, 0)
        row_3: Qt.QHBoxLayout = Qt.QHBoxLayout()
        row_3.setSpacing(1)
        row_3.setContentsMargins(1, 0, 1, 0)
        row_4: Qt.QHBoxLayout = Qt.QHBoxLayout()
        row_4.setSpacing(1)
        row_4.setContentsMargins(1, 0, 1, 0)

        row_5: Qt.QHBoxLayout = Qt.QHBoxLayout()
        row_5.setSpacing(1)
        row_5.setContentsMargins(1, 0, 1, 0)

        row_1.addWidget(self.InfoChamberNumber     )
        row_1.addWidget(self.EditChamberNumber     )
        row_1.addWidget(self.InfoRowNumber         )
        row_1.addWidget(self.EditRowNumber         )
        row_2.addWidget(self.InfoXPosition         )
        row_2.addWidget(self.EditXPosition         )
        row_2.addWidget(self.InfoYPosition         )
        row_2.addWidget(self.EditYPosition         )
        row_3.addWidget(self.InfoZPosition         )
        row_3.addWidget(self.EditZPosition         )
        row_3.addWidget(self.InfoRPosition         )
        row_3.addWidget(self.EditRPosition         )
        row_4.addWidget(self.InfoStep              )
        row_4.addWidget(self.InfoSplit             )
        row_4.addWidget(self.EditStep              )
        row_4.addWidget(self.EditSplit             )
        row_5.addWidget(self.ChangedRowButton      )
        row_5.addWidget(self.ChangedGroupButton    )

        layout.addLayout(row_1)
        layout.addLayout(row_2)
        layout.addLayout(row_3)
        layout.addLayout(row_4)
        layout.addLayout(row_5)
        self.setLayout(layout)

    def show(self) -> None:
        self.flushFrame()
        return super(HitBotChamberPositionView, self).show()


    def initWindows(self):
        self.initInfoChamberNumber     ()
        self.initEditChamberNumber     ()
        self.initInfoRowNumber         ()
        self.initEditRowNumber         ()
        self.initInfoXPosition         ()
        self.initEditXPosition         ()
        self.initInfoYPosition         ()
        self.initEditYPosition         ()
        self.initInfoZPosition         ()
        self.initEditZPosition         ()
        self.initInfoRPosition         ()
        self.initEditRPosition         ()
        self.initInfoStep              ()
        self.initInfoSplit             ()
        self.initEditStep              ()
        self.initEditSplit             ()
        self.initChangedRowButton      ()
        self.initChangedGroupButton    ()

    def initProperty(self):
        super(HitBotChamberPositionView, self).initProperty()
        self.InfoChamberNumber   .setText('片仓编号')
        self.InfoRowNumber       .setText('玻片行号')
        self.InfoXPosition       .setText('Ｘ轴位置')
        self.InfoYPosition       .setText('Ｙ轴位置')
        self.InfoZPosition       .setText('Ｚ轴位置')
        self.InfoRPosition       .setText('Ｒ轴位置')
        self.ChangedRowButton    .setText('设置行号坐标')
        self.ChangedGroupButton  .setText('设置起始坐标')
        self.InfoStep            .setText('玻片间距')
        self.InfoSplit           .setText('左右间距')
        self.EditChamberNumber  .addItems(['{:^2d}'.format(number) for number in range(1, 11)])
        self.EditRowNumber      .addItems(['{:^2d}'.format(number) for number in range(1, 21)])
        self.EditXPosition      .setRange(-300.0, 300.0)
        self.EditYPosition      .setRange(-300.0, 300.0)
        self.EditZPosition      .setRange(-180.0,  -0.5)
        self.EditRPosition      .setRange(-125.0, 125.0)
        self.EditStep           .setRange(2.0,  5.0 )
        self.EditSplit          .setRange(1.0,  20.0)
        self.EditStep           .setValue(3.35)
        self.EditSplit          .setValue(13.0)
        self.initWindowIcon()

    def initInfoChamberNumber   (self):
        if not self.hasInfoChamberNumber   :
            self.InfoChamberNumber    = Qt.QLabel         (self)

    def initInfoRowNumber       (self):
        if not self.hasInfoRowNumber       :
            self.InfoRowNumber        = Qt.QLabel         (self)

    def initInfoXPosition       (self):
        if not self.hasInfoXPosition       :
            self.InfoXPosition        = Qt.QLabel         (self)

    def initInfoYPosition       (self):
        if not self.hasInfoYPosition       :
            self.InfoYPosition        = Qt.QLabel         (self)

    def initInfoZPosition       (self):
        if not self.hasInfoZPosition       :
            self.InfoZPosition        = Qt.QLabel         (self)

    def initInfoRPosition       (self):
        if not self.hasInfoRPosition       :
            self.InfoRPosition        = Qt.QLabel         (self)

    def initEditChamberNumber   (self):
        if not self.hasEditChamberNumber   :
            self.EditChamberNumber    = Qt.QComboBox      (self)

    def initEditRowNumber       (self):
        if not self.hasEditRowNumber       :
            self.EditRowNumber        = Qt.QComboBox      (self)

    def initEditXPosition       (self):
        if not self.hasEditXPosition       :
            self.EditXPosition        = Qt.QDoubleSpinBox (self)

    def initEditYPosition       (self):
        if not self.hasEditYPosition       :
            self.EditYPosition        = Qt.QDoubleSpinBox (self)

    def initEditZPosition       (self):
        if not self.hasEditZPosition       :
            self.EditZPosition        = Qt.QDoubleSpinBox (self)

    def initEditRPosition       (self):
        if not self.hasEditRPosition       :
            self.EditRPosition        = Qt.QDoubleSpinBox (self)

    def initChangedRowButton    (self):
        if not self.hasChangedRowButton    :
            self.ChangedRowButton     = Qt.QPushButton    (self)

    def initChangedGroupButton  (self):
        if not self.hasChangedGroupButton  :
            self.ChangedGroupButton   = Qt.QPushButton    (self)

    def initInfoStep   (self):
        if not self.hasInfoStep   :
            self.InfoStep    = Qt.QLabel         (self)
    def initInfoSplit  (self):
        if not self.hasInfoSplit  :
            self.InfoSplit   = Qt.QLabel         (self)
    def initEditStep   (self):
        if not self.hasEditStep   :
            self.EditStep    = Qt.QDoubleSpinBox (self)
    def initEditSplit  (self):
        if not self.hasEditSplit  :
            self.EditSplit   = Qt.QDoubleSpinBox (self)





    @property
    def hasInfoChamberNumber   (self) -> bool:
        return hasattr(self, "InfoChamberNumber"   ) and isinstance(self.InfoChamberNumber   , Qt.QLabel         )

    @property
    def hasInfoRowNumber       (self) -> bool:
        return hasattr(self, "InfoRowNumber"       ) and isinstance(self.InfoRowNumber       , Qt.QLabel         )

    @property
    def hasInfoXPosition       (self) -> bool:
        return hasattr(self, "InfoXPosition"       ) and isinstance(self.InfoXPosition       , Qt.QLabel         )

    @property
    def hasInfoYPosition       (self) -> bool:
        return hasattr(self, "InfoYPosition"       ) and isinstance(self.InfoYPosition       , Qt.QLabel         )

    @property
    def hasInfoZPosition       (self) -> bool:
        return hasattr(self, "InfoZPosition"       ) and isinstance(self.InfoZPosition       , Qt.QLabel         )

    @property
    def hasInfoRPosition       (self) -> bool:
        return hasattr(self, "InfoRPosition"       ) and isinstance(self.InfoRPosition       , Qt.QLabel         )

    @property
    def hasEditChamberNumber   (self) -> bool:
        return hasattr(self, "EditChamberNumber"   ) and isinstance(self.EditChamberNumber   , Qt.QComboBox      )

    @property
    def hasEditRowNumber       (self) -> bool:
        return hasattr(self, "EditRowNumber"       ) and isinstance(self.EditRowNumber       , Qt.QComboBox      )

    @property
    def hasEditXPosition       (self) -> bool:
        return hasattr(self, "EditXPosition"       ) and isinstance(self.EditXPosition       , Qt.QDoubleSpinBox )

    @property
    def hasEditYPosition       (self) -> bool:
        return hasattr(self, "EditYPosition"       ) and isinstance(self.EditYPosition       , Qt.QDoubleSpinBox )

    @property
    def hasEditZPosition       (self) -> bool:
        return hasattr(self, "EditZPosition"       ) and isinstance(self.EditZPosition       , Qt.QDoubleSpinBox )

    @property
    def hasEditRPosition       (self) -> bool:
        return hasattr(self, "EditRPosition"       ) and isinstance(self.EditRPosition       , Qt.QDoubleSpinBox )

    @property
    def hasChangedRowButton    (self) -> bool:
        return hasattr(self, "ChangedRowButton"    ) and isinstance(self.ChangedRowButton    , Qt.QPushButton    )

    @property
    def hasChangedGroupButton  (self) -> bool:
        return hasattr(self, "ChangedGroupButton"  ) and isinstance(self.ChangedGroupButton  , Qt.QPushButton    )






    @property
    def hasEditStep        (self) -> bool:
        return hasattr(self, "EditStep"      ) and isinstance(self.EditStep        , Qt.QLabel)

    @property
    def hasEditSplit       (self) -> bool:
        return hasattr(self, "EditSplit"     ) and isinstance(self.EditSplit       , Qt.QLabel)

    @property
    def hasInfoStep        (self) -> bool:
        return hasattr(self, "InfoStep"      ) and isinstance(self.InfoStep        , Qt.QLabel)

    @property
    def hasInfoSplit       (self) -> bool:
        return hasattr(self, "InfoSplit"     ) and isinstance(self.InfoSplit       , Qt.QLabel)
