#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：HitBotPositionView.py\n
 @DateTime: 2025/7/25 16:15\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 HitBotPositionView(Qt.QWidget, UiInterface):
    Model           : HitBotModel       #
    Info_xPosition  : Qt.QLabel         #
    Info_yPosition  : Qt.QLabel         #
    Info_zPosition  : Qt.QLabel         #
    Info_rPosition  : Qt.QLabel         #
    Info_speed      : Qt.QLabel         #
    Edit_xPosition  : Qt.QDoubleSpinBox #
    Edit_yPosition  : Qt.QDoubleSpinBox #
    Edit_zPosition  : Qt.QDoubleSpinBox #
    Edit_rPosition  : Qt.QDoubleSpinBox #
    Edit_speed      : Qt.QDoubleSpinBox #
    def __init__(self, parent: Qt.QWidget = None):
        super(HitBotPositionView, self).__init__(parent=parent)
        self.Initialization('HitBotPositionView', self, parent)
        self.initWidgetStyle()
        self.flushFrame     ()

    def setModel(self, model: HitBotModelInterface):
        if not model:
            return
        self.initModel()
        self.Model.as_field(model.to_field())
        self.flushFrame()

    def getModel(self) -> HitBotModelInterface:
        self.updateFrame()
        return self.Model.copy()

    def flushFrame(self):
        self.initModel()
        self.Edit_xPosition  .setValue(self.Model.xPosition  if self.Model.has_xPosition else 0.0)
        self.Edit_yPosition  .setValue(self.Model.yPosition  if self.Model.has_yPosition else 0.0)
        self.Edit_zPosition  .setValue(self.Model.zPosition  if self.Model.has_zPosition else 0.0)
        self.Edit_rPosition  .setValue(self.Model.rPosition  if self.Model.has_rPosition else 0.0)
        self.Edit_speed      .setValue(self.Model.speed      if self.Model.has_speed     else 0.0)

    def updateFrame(self):
        self.initModel()
        self.Model.xPosition = self.Edit_xPosition.value()
        self.Model.yPosition = self.Edit_yPosition.value()
        self.Model.zPosition = self.Edit_zPosition.value()
        self.Model.rPosition = self.Edit_rPosition.value()
        self.Model.speed     = self.Edit_speed    .value()
        self.Model.efg_distance = None



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

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

    def initLayouts(self):
        if self.hasWindowLayouts:
            return
        layout: Qt.QHBoxLayout = Qt.QHBoxLayout()
        layout.setSpacing(0)
        layout.setContentsMargins(1, 0, 1, 0)
        layout.addWidget(self.Info_xPosition)
        layout.addWidget(self.Edit_xPosition)
        layout.addWidget(self.Info_yPosition)
        layout.addWidget(self.Edit_yPosition)
        layout.addWidget(self.Info_zPosition)
        layout.addWidget(self.Edit_zPosition)
        layout.addWidget(self.Info_rPosition)
        layout.addWidget(self.Edit_rPosition)
        layout.addWidget(self.Info_speed    )
        layout.addWidget(self.Edit_speed    )
        self.setLayout(layout)

    def initWindows(self):
        self.initInfo_speed    ()
        self.initInfo_xPosition()
        self.initInfo_yPosition()
        self.initInfo_zPosition()
        self.initInfo_rPosition()
        self.initInfo_speed    ()
        self.initEdit_xPosition()
        self.initEdit_yPosition()
        self.initEdit_zPosition()
        self.initEdit_rPosition()
        self.initEdit_speed    ()

    def initProperty(self):
        super(HitBotPositionView, self).initProperty()
        self.Info_xPosition  .setAlignment(Qt.Qt.AlignRight | Qt.Qt.AlignVCenter)
        self.Info_yPosition  .setAlignment(Qt.Qt.AlignRight | Qt.Qt.AlignVCenter)
        self.Info_zPosition  .setAlignment(Qt.Qt.AlignRight | Qt.Qt.AlignVCenter)
        self.Info_rPosition  .setAlignment(Qt.Qt.AlignRight | Qt.Qt.AlignVCenter)
        self.Info_speed      .setAlignment(Qt.Qt.AlignRight | Qt.Qt.AlignVCenter)
        self.Info_xPosition  .setStyleSheet(UiUtils.getButtonStyle())
        self.Info_yPosition  .setStyleSheet(UiUtils.getButtonStyle())
        self.Info_zPosition  .setStyleSheet(UiUtils.getButtonStyle())
        self.Info_rPosition  .setStyleSheet(UiUtils.getButtonStyle())
        self.Info_speed      .setStyleSheet(UiUtils.getButtonStyle())
        self.Edit_xPosition  .setStyleSheet(UiUtils.getButtonStyle())
        self.Edit_yPosition  .setStyleSheet(UiUtils.getButtonStyle())
        self.Edit_zPosition  .setStyleSheet(UiUtils.getButtonStyle())
        self.Edit_rPosition  .setStyleSheet(UiUtils.getButtonStyle())
        self.Edit_speed      .setStyleSheet(UiUtils.getButtonStyle())

        self.Edit_xPosition  .setRange(-350 , 350 )
        self.Edit_yPosition  .setRange(-350 , 350 )
        self.Edit_zPosition  .setRange(-175 , -0.5)
        self.Edit_rPosition  .setRange(-225 , 225 )
        self.Edit_speed      .setRange(5    , 350 )
        self.Info_xPosition  .setText(f'Ｘ轴位置:')
        self.Info_yPosition  .setText(f'Ｙ轴位置:')
        self.Info_zPosition  .setText(f'Ｚ轴位置:')
        self.Info_rPosition  .setText(f'Ｒ轴位置:')
        self.Info_speed      .setText(f'移动速度:')

    def initModel           (self):
        if not self.hasModel           :
            self.Model            = HitBotModel      (     )
            model: HitBotSafeAction = HitBotSafeAction.getMinPrimaryItem()
            self.Model.movetype = HitBotMoveType.LMove_XYZR_G
            self.Model.efg_distance = None
            self.Model.isScanning   = None
            self.Model.xPosition    = None
            self.Model.yPosition    = None
            self.Model.zPosition    = None
            self.Model.rPosition    = None
            if not model:
                return
            self.Model.as_field(model.to_field())

    def initInfo_xPosition  (self):
        if not self.hasInfo_xPosition  :
            self.Info_xPosition   = Qt.QLabel        (self )
    def initInfo_yPosition  (self):
        if not self.hasInfo_yPosition  :
            self.Info_yPosition   = Qt.QLabel        (self )
    def initInfo_zPosition  (self):
        if not self.hasInfo_zPosition  :
            self.Info_zPosition   = Qt.QLabel        (self )
    def initInfo_rPosition  (self):
        if not self.hasInfo_rPosition  :
            self.Info_rPosition   = Qt.QLabel        (self )
    def initInfo_speed      (self):
        if not self.hasInfo_speed      :
            self.Info_speed       = Qt.QLabel        (self )
    def initEdit_xPosition  (self):
        if not self.hasEdit_xPosition  :
            self.Edit_xPosition   = Qt.QDoubleSpinBox(self )
    def initEdit_yPosition  (self):
        if not self.hasEdit_yPosition  :
            self.Edit_yPosition   = Qt.QDoubleSpinBox(self )
    def initEdit_zPosition  (self):
        if not self.hasEdit_zPosition  :
            self.Edit_zPosition   = Qt.QDoubleSpinBox(self )
    def initEdit_rPosition  (self):
        if not self.hasEdit_rPosition  :
            self.Edit_rPosition   = Qt.QDoubleSpinBox(self )
    def initEdit_speed      (self):
        if not self.hasEdit_speed      :
            self.Edit_speed       = Qt.QDoubleSpinBox(self )
    @property
    def hasModel           (self):
        return hasattr(self, 'Model'          ) and isinstance(self.Model           , HitBotModelInterface        )

    @property
    def hasInfo_xPosition  (self):
        return hasattr(self, 'Info_xPosition' ) and isinstance(self.Info_xPosition  , Qt.QLabel                   )

    @property
    def hasInfo_yPosition  (self):
        return hasattr(self, 'Info_yPosition' ) and isinstance(self.Info_yPosition  , Qt.QLabel                   )

    @property
    def hasInfo_zPosition  (self):
        return hasattr(self, 'Info_zPosition' ) and isinstance(self.Info_zPosition  , Qt.QLabel                   )

    @property
    def hasInfo_rPosition  (self):
        return hasattr(self, 'Info_rPosition' ) and isinstance(self.Info_rPosition  , Qt.QLabel                   )

    @property
    def hasInfo_speed      (self):
        return hasattr(self, 'Info_speed'     ) and isinstance(self.Info_speed      , Qt.QLabel                   )

    @property
    def hasEdit_xPosition  (self):
        return hasattr(self, 'Edit_xPosition' ) and isinstance(self.Edit_xPosition  , Qt.QDoubleSpinBox           )

    @property
    def hasEdit_yPosition  (self):
        return hasattr(self, 'Edit_yPosition' ) and isinstance(self.Edit_yPosition  , Qt.QDoubleSpinBox           )

    @property
    def hasEdit_zPosition  (self):
        return hasattr(self, 'Edit_zPosition' ) and isinstance(self.Edit_zPosition  , Qt.QDoubleSpinBox           )

    @property
    def hasEdit_rPosition  (self):
        return hasattr(self, 'Edit_rPosition' ) and isinstance(self.Edit_rPosition  , Qt.QDoubleSpinBox           )

    @property
    def hasEdit_speed      (self):
        return hasattr(self, 'Edit_speed'     ) and isinstance(self.Edit_speed      , Qt.QDoubleSpinBox           )
