#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：MotionChangedView.py\n
 @DateTime: 2025/7/26 12:21\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 MotionChangedView(Qt.QWidget, UiInterface):
    MoveSteps       : list[str]         #
    UpButtonText    : str               #
    DownButtonText  : str               #
    StepName        : str               #
    InfoStep        : Qt.QLabel         #
    EditStep        : Qt.QComboBox      #
    UpButton        : Qt.QPushButton    #
    DownButton      : Qt.QPushButton    #

    def __init__(self, info: str, up_text: str, down_text: str, steps: list[str], parent: Qt.QWidget = None):
        super(MotionChangedView, self).__init__(parent=parent)
        self.MoveSteps      = steps if isinstance(steps, list) else []
        self.UpButtonText   = up_text if isinstance(up_text, str) else ''
        self.DownButtonText = down_text if isinstance(down_text, str) else ''
        self.StepName       = info if isinstance(info, str) else ''
        self.Initialization('MotionChangedView', self, parent)
        self.initWidgetStyle()

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

    def flushFrame(self):
        super(MotionChangedView, self).flushFrame()
        self.InfoStep   .setText(self.StepName      )
        self.UpButton   .setText(self.UpButtonText  )
        self.DownButton .setText(self.DownButtonText)
        if self.EditStep.count() != len(self.MoveSteps):
            while self.EditStep.count() != 0:
                self.EditStep.removeItem(0)
            self.EditStep.addItem([f'{text}' for text in self.MoveSteps])

    def getStep(self) -> str:
        if not self.hasEditStep:
            return None
        return self.EditStep.currentText()

    def initValue(self):
        super(MotionChangedView, 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.InfoStep  )
        layout.addWidget(self.EditStep  )
        layout.addWidget(self.UpButton  )
        layout.addWidget(self.DownButton)
        self.setLayout(layout)

    def initProperty(self):
        super(MotionChangedView, self).initProperty()
        self.InfoStep  .setStyleSheet(UiUtils.getButtonStyle())
        self.EditStep  .setStyleSheet(UiUtils.getButtonStyle())
        self.UpButton  .setStyleSheet(UiUtils.getButtonStyle())
        self.DownButton.setStyleSheet(UiUtils.getButtonStyle())
        self.InfoStep.setText(self.StepName)
        self.UpButton.setText(self.UpButtonText)
        self.DownButton.setText(self.DownButtonText)
        if self.EditStep.count() != len(self.MoveSteps):
            while self.EditStep.count() != 0:
                self.EditStep.removeItem(0)
            self.EditStep.addItems([f'{text}' for text in self.MoveSteps])

    def initWindows(self):
        self.initMoveSteps       ()
        self.initUpButtonText    ()
        self.initDownButtonText  ()
        self.initStepName        ()
        self.initInfoStep        ()
        self.initEditStep        ()
        self.initUpButton        ()
        self.initDownButton      ()

    def initMoveSteps       (self):
        if not self.hasMoveSteps       :
            self.MoveSteps        = []

    def initUpButtonText    (self):
        if not self.hasUpButtonText    :
            self.UpButtonText     = '上移'

    def initDownButtonText  (self):
        if not self.hasDownButtonText  :
            self.DownButtonText   = '下移'

    def initInfoStep        (self):
        if not self.hasInfoStep        :
            self.InfoStep         = Qt.QLabel      (self)

    def initEditStep        (self):
        if not self.hasEditStep        :
            self.EditStep         = Qt.QComboBox   (self)

    def initUpButton        (self):
        if not self.hasUpButton        :
            self.UpButton         = Qt.QPushButton (self)

    def initDownButton      (self):
        if not self.hasDownButton      :
            self.DownButton       = Qt.QPushButton (self)

    def initStepName(self):
        if not self.hasStepName:
            self.StepName = '移动步长'

    @property
    def hasStepName(self) -> bool:
        return hasattr(self, 'StepName') and isinstance(self.StepName, str)

    @property
    def hasMoveSteps       (self) -> bool:
        return hasattr(self, 'MoveSteps'      ) and isinstance(self.MoveSteps       , list            )

    @property
    def hasUpButtonText    (self) -> bool:
        return hasattr(self, 'UpButtonText'   ) and isinstance(self.UpButtonText    , str             )

    @property
    def hasDownButtonText  (self) -> bool:
        return hasattr(self, 'DownButtonText' ) and isinstance(self.DownButtonText  , str             )

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

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

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

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