#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：HitBotDropSlideSplitView.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 HitBotDropSlideSplitView(Qt.QWidget, UiInterface, SingletonInterface):

    InfoChamberNumber   : Qt.QLabel                 #
    InfoBPositionZ      : Qt.QLabel                 #
    InfoEPositionZ      : Qt.QLabel                 #
    InfoSlideSplit      : Qt.QLabel                 #
    EditChamberNumber   : Qt.QSpinBox               #
    EditBPositionZ      : Qt.QDoubleSpinBox         #
    EditEPositionZ      : Qt.QDoubleSpinBox         #
    EditSlideSplit      : Qt.QDoubleSpinBox         #
    GroupBox            : Qt.QGroupBox              #
    EnterButton         : Qt.QPushButton            #
    CancelButton        : Qt.QPushButton            #
    ComputeSplitButton  : Qt.QPushButton            #
    def __init__(self, parent: Qt.QWidget = None):
        super(HitBotDropSlideSplitView, self).__init__(parent=parent)
        self.Initialization('HitBotDropSlideSplitView', self, parent)
        self.initWidgetStyle()
        self.SlotCancelButton()

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

    def initConnect(self):
        super(HitBotDropSlideSplitView, self).initConnect()
        TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
        self.EditChamberNumber.valueChanged.connect(slot=self.SlotEditChamberNumber, type=TYPE)
        self.EnterButton .clicked.connect(slot=self.SlotEnterButton , type=TYPE)
        self.CancelButton.clicked.connect(slot=self.SlotCancelButton, type=TYPE)
        self.ComputeSplitButton.clicked.connect(slot=self.SlotComputeSplitButton, type=TYPE)

    def SlotComputeSplitButton(self):
        from AppSettings.SystemSetting import SystemSetting, system_config
        self.blockSignals(True)
        try:
            row_number = self.EditChamberNumber.value()
            model: SlideSplitModel = SlideSplitModel()
            model.BPositionZ = self.EditBPositionZ.value()
            model.SlideSplit = 3.34
            model.MaxRowNumber = system_config.MaxRowNumber
            model.EPositionZ = self.EditBPositionZ.value() + model.MaxRowNumber * model.SlideSplit
            model.ChamberNumber, model.SlideSplit = model.ConvertSlideSplit(row_number, model.BPositionZ, model.EPositionZ)
            self.EditEPositionZ.setValue(model.EPositionZ)
            self.setSlideSplit(model.SlideSplit)
        except Exception as e:
            AppUtils.print_Exception(e)
        self.blockSignals(False)
        return

    def SlotEnterButton  (self):
        model: SlideSplitModel = self.getModel()
        _model: SlideSplitModel = SlideSplitModel.get(SlideSplitModel.ChamberNumber == model.ChamberNumber)

        if _model is None:
            SlideSplitModel.append_all([model])
        else:
            SlideSplitModel.model_update(model.to_field(False), SlideSplitModel.ChamberNumber == model.ChamberNumber)
        self.setSlideSplit(model.SlideSplit)
        pass
    def SlotCancelButton (self):
        row_number = self.EditChamberNumber.value()
        model: SlideSplitModel = SlideSplitModel.get(SlideSplitModel.ChamberNumber == row_number)
        self.flushModel(model)

        pass
    def SlotEditChamberNumber(self, index):
        row_number = self.EditChamberNumber.value()
        model: SlideSplitModel = SlideSplitModel.get(SlideSplitModel.ChamberNumber == row_number)
        try:
            self.setSlideSplit(model.SlideSplit)
            self.EditBPositionZ.setValue(model.BPositionZ)
            self.EditEPositionZ.setValue(model.EPositionZ)
        except Exception as e:
            AppUtils.print_Exception(e)

    def flushModel(self, model: SlideSplitModel):
        if not isinstance(model, SlideSplitModel):
            return
        try:
            self.EditChamberNumber         .setValue(model.ChamberNumber        )
            self.EditBPositionZ .setValue(model.BPositionZ)
            self.EditEPositionZ .setValue(model.EPositionZ)
            self.setSlideSplit(model.SlideSplit)
        except Exception as e:
            AppUtils.print_Exception(e)

    def setModel(self, model: SlideSplitModel):
        self.blockSignals(True)
        try:
            self.flushModel(model)
        except Exception as e:
            AppUtils.print_Exception(e)
        self.blockSignals(False)

    def getModel(self) -> SlideSplitModel:
        row_number                  = self.EditChamberNumber.value         ()
        model: SlideSplitModel  = SlideSplitModel           ()
        model.BPositionZ            = -abs(self.EditBPositionZ.value ())
        model.EPositionZ            = -abs(self.EditEPositionZ.value ())
        model.ChamberNumber, model.SlideSplit  = model.ConvertSlideSplit(row_number, self.EditBPositionZ.value(), self.EditEPositionZ.value())
        return model




    def flushFrame(self):
        super(HitBotDropSlideSplitView, self).flushFrame()
        self.SlotCancelButton()




    def initWidgetStyle(self):
        self.setStyleSheet(UiUtils.getWidgetStyle())
        self.setMinimumWidth(600)

    def initLayouts(self):
        if self.hasWindowLayouts:
            return
        layout: Qt.QVBoxLayout = Qt.QVBoxLayout()
        layout.setSpacing(1)
        layout.setContentsMargins(5, 5, 5, 5)
        b_layout: Qt.QHBoxLayout = Qt.QHBoxLayout()
        b_layout.setSpacing(1)
        b_layout.setContentsMargins(5, 5, 5, 5)
        g_layout: Qt.QFormLayout = Qt.QFormLayout()
        g_layout.setSpacing(1)
        g_layout.setContentsMargins(5, 5, 5, 5)
        g_layout.addRow(self.InfoChamberNumber         , self.EditChamberNumber           )
        g_layout.addRow(self.InfoBPositionZ , self.EditBPositionZ   )
        g_layout.addRow(self.InfoEPositionZ , self.EditEPositionZ   )
        g_layout.addRow(self.InfoSlideSplit , self.EditSlideSplit   )
        self.GroupBox.setLayout(g_layout)

        b_layout.addWidget(self.EnterButton         )
        b_layout.addWidget(self.ComputeSplitButton  )
        b_layout.addWidget(self.CancelButton        )

        layout.addWidget(self.GroupBox)
        layout.addLayout(b_layout)
        self.setLayout(layout)


    def setSlideSplit(self, value: float):
        self.EditSlideSplit.setEnabled(True)
        try:
            self.EditSlideSplit.setValue(-abs(value))
        except Exception as e:
            AppUtils.print_Exception(e)
        self.EditSlideSplit.setEnabled(False)
    def initValue(self):
        super(HitBotDropSlideSplitView, self).initValue()
        self.initWindows()

    def initProperty(self):
        super(HitBotDropSlideSplitView, self).initProperty()
        self.InfoChamberNumber          .setStyleSheet(UiUtils.getButtonStyle())
        self.InfoBPositionZ  .setStyleSheet(UiUtils.getButtonStyle())
        self.InfoEPositionZ  .setStyleSheet(UiUtils.getButtonStyle())
        self.InfoSlideSplit  .setStyleSheet(UiUtils.getButtonStyle())
        self.EditChamberNumber          .setStyleSheet(UiUtils.getButtonStyle())
        self.EditBPositionZ  .setStyleSheet(UiUtils.getButtonStyle())
        self.EditEPositionZ  .setStyleSheet(UiUtils.getButtonStyle())
        self.EditSlideSplit  .setStyleSheet(UiUtils.getButtonStyle())
        self.EnterButton     .setStyleSheet(UiUtils.getButtonStyle())
        self.CancelButton    .setStyleSheet(UiUtils.getButtonStyle())
        self.ComputeSplitButton.setStyleSheet(UiUtils.getButtonStyle())

        self.InfoChamberNumber          .setText('　片仓组号　')
        self.InfoBPositionZ  .setText('首行Ｚ轴位置')
        self.InfoEPositionZ  .setText('尾行Ｚ轴位置')
        self.InfoSlideSplit  .setText('　玻片间距　')
        self.EditChamberNumber          .setRange(1, 20)
        self.EditBPositionZ  .setRange(-180.0, 180.0)
        self.EditEPositionZ  .setRange(-180.0, 180.0)
        self.EditSlideSplit  .setRange(-180.0, 180.0)
        self.EnterButton     .setText('设置')
        self.CancelButton    .setText('取消')
        self.ComputeSplitButton.setText('计算')
        self.EditSlideSplit.setEnabled(False)


    def initWindows(self):
        self.initInfoChamberNumber              ()
        self.initInfoBPositionZ      ()
        self.initInfoEPositionZ      ()
        self.initInfoSlideSplit      ()
        self.initEditChamberNumber              ()
        self.initEditBPositionZ      ()
        self.initEditEPositionZ      ()
        self.initEditSlideSplit      ()
        self.initGroupBox            ()
        self.initEnterButton         ()
        self.initCancelButton        ()
        self.initComputeSplitButton  ()

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

    def initInfoBPositionZ      (self):
        if not self.hasInfoBPositionZ      :
            self.InfoBPositionZ       = Qt.QLabel         ()

    def initInfoEPositionZ      (self):
        if not self.hasInfoEPositionZ      :
            self.InfoEPositionZ       = Qt.QLabel         ()

    def initInfoSlideSplit      (self):
        if not self.hasInfoSlideSplit      :
            self.InfoSlideSplit       = Qt.QLabel         ()

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

    def initEditBPositionZ      (self):
        if not self.hasEditBPositionZ      :
            self.EditBPositionZ       = Qt.QDoubleSpinBox ()

    def initEditEPositionZ      (self):
        if not self.hasEditEPositionZ      :
            self.EditEPositionZ       = Qt.QDoubleSpinBox ()

    def initEditSlideSplit      (self):
        if not self.hasEditSlideSplit      :
            self.EditSlideSplit       = Qt.QDoubleSpinBox ()

    def initGroupBox            (self):
        if not self.hasGroupBox            :
            self.GroupBox             = Qt.QGroupBox      ()

    def initEnterButton         (self):
        if not self.hasEnterButton         :
            self.EnterButton          = Qt.QPushButton    ()

    def initCancelButton        (self):
        if not self.hasCancelButton        :
            self.CancelButton         = Qt.QPushButton    ()

    def initComputeSplitButton(self):
        if not self.hasComputeSplitButton:
            self.ComputeSplitButton = Qt.QPushButton()


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

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

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

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

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

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

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

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

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

    @property
    def hasGroupBox            (self) -> bool:
        return hasattr(self, 'GroupBox'       ) and isinstance(self.GroupBox            , Qt.QGroupBox     )

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

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