#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：SelectLocationView.py\n
 @DateTime: 2025/7/26 13:03\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 SelectLocationView(Qt.QWidget, UiInterface):
    currentDataUpdated: Qt.pyqtSignal = Qt.pyqtSignal(object)
    InfoObjectiveLevel : Qt.QLabel      #
    InfoTrayNumber     : Qt.QLabel      #
    InfoRowNumber      : Qt.QLabel      #
    InfoColNumber      : Qt.QLabel      #
    EditObjectiveLevel : Qt.QSpinBox    #
    EditTrayNumber     : Qt.QSpinBox    #
    EditRowNumber      : Qt.QSpinBox    #
    EditColNumber      : Qt.QSpinBox    #
    LocationButton     : Qt.QPushButton #
    def __init__(self, parent: Qt.QWidget = None):
        super(SelectLocationView, self).__init__(parent=parent)
        self.Initialization('SelectLocationView', self, parent)
        self.initWidgetStyle()
        self.flushFrame()

    def flushFrame(self):
        self.InfoObjectiveLevel .setText('物镜编号')
        self.InfoTrayNumber     .setText('托盘编号')
        self.InfoRowNumber      .setText('　行号　')
        self.InfoColNumber      .setText('　列号　')

    def initConnect(self):
        super(SelectLocationView, 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.TrayNumber, model.RowNumber, model.ColNumber)

    def getLocation(self) -> (int, int, int, int):
        return self.EditObjectiveLevel.value(), self.EditTrayNumber    .value(),self.EditRowNumber     .value(), self.EditColNumber     .value()

    def setLocation(self, objective: int, tray_number: int, row_number: int, col_number: int):
        try:
            self.EditObjectiveLevel.setValue(objective)
            self.EditTrayNumber.setValue(tray_number)
            self.EditRowNumber.setValue(row_number)
            self.EditColNumber.setValue(col_number)
        except Exception as e:
            AppUtils.print_Exception(e)

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

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

    def initProperty(self):
        super(SelectLocationView, self).initProperty()

        self.EditObjectiveLevel .setRange(1, 7 )
        self.EditTrayNumber     .setRange(1, 2 )
        self.EditRowNumber      .setRange(1, 25)
        self.EditColNumber      .setRange(1, 40)
        self.LocationButton     .setText('移动托盘')

        self.InfoObjectiveLevel .setStyleSheet(UiUtils.getButtonStyle())
        self.InfoTrayNumber     .setStyleSheet(UiUtils.getButtonStyle())
        self.InfoRowNumber      .setStyleSheet(UiUtils.getButtonStyle())
        self.InfoColNumber      .setStyleSheet(UiUtils.getButtonStyle())
        self.EditObjectiveLevel .setStyleSheet(UiUtils.getButtonStyle())
        self.EditTrayNumber     .setStyleSheet(UiUtils.getButtonStyle())
        self.EditRowNumber      .setStyleSheet(UiUtils.getButtonStyle())
        self.EditColNumber      .setStyleSheet(UiUtils.getButtonStyle())
        self.LocationButton     .setStyleSheet(UiUtils.getButtonStyle())
    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.InfoTrayNumber     , 1)
        layout.addWidget(self.EditTrayNumber     , 1)
        layout.addWidget(self.InfoRowNumber      , 1)
        layout.addWidget(self.EditRowNumber      , 1)
        layout.addWidget(self.InfoColNumber      , 1)
        layout.addWidget(self.EditColNumber      , 1)
        layout.addWidget(self.LocationButton     , 1)
        self.setLayout(layout)

    def initWindows(self):
        self.initInfoObjectiveLevel()
        self.initInfoTrayNumber    ()
        self.initInfoRowNumber     ()
        self.initInfoColNumber     ()
        self.initEditObjectiveLevel()
        self.initEditTrayNumber    ()
        self.initEditRowNumber     ()
        self.initEditColNumber     ()
        self.initLocationButton    ()

    def initInfoObjectiveLevel (self):
        if not self.hasInfoObjectiveLevel:
            self.InfoObjectiveLevel = Qt.QLabel     (self)
    def initInfoTrayNumber     (self):
        if not self.hasInfoTrayNumber    :
            self.InfoTrayNumber     = Qt.QLabel     (self)
    def initInfoRowNumber      (self):
        if not self.hasInfoRowNumber     :
            self.InfoRowNumber      = Qt.QLabel     (self)
    def initInfoColNumber      (self):
        if not self.hasInfoColNumber     :
            self.InfoColNumber      = Qt.QLabel     (self)
    def initEditObjectiveLevel (self):
        if not self.hasEditObjectiveLevel:
            self.EditObjectiveLevel = Qt.QSpinBox   (self)
    def initEditTrayNumber     (self):
        if not self.hasEditTrayNumber    :
            self.EditTrayNumber     = Qt.QSpinBox   (self)
    def initEditRowNumber      (self):
        if not self.hasEditRowNumber     :
            self.EditRowNumber      = Qt.QSpinBox   (self)
    def initEditColNumber      (self):
        if not self.hasEditColNumber     :
            self.EditColNumber      = Qt.QSpinBox   (self)
    def initLocationButton     (self):
        if not self.hasLocationButton    :
            self.LocationButton     = Qt.QPushButton(self)




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

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

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

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

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

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

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

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

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