#!D:\IDE\Anaconda3\envs\Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName   : HitBotSetting.py
@Account    : Administrator
@Time       : 2025/9/14 12:08
@Author     : xsh
@Description: 
"""
from PyQt5 import Qt
from AppInterfaces.ModelInterface import (ContextModelInterface, ConvertUtils, AppConvert, AppFactory, AppUtils)
from AppInterfaces.SingletonInterface import SingletonInterface

class HitBotSetting(ContextModelInterface, SingletonInterface):
    Locker                          : Qt.QMutex         = Qt.QMutex()
    IOLocker                        : Qt.QReadWriteLock = Qt.QReadWriteLock()
    ID                              : int                                   #   =1
    Number                          : int                                   #   =180
    InPort                          : int                                   #   =7
    CheckWaitTime                   : int                                   #   =300
    IsClamp                         : int                                   #   =0
    OperationWaitTime               : int                                   #   = 200
    CheckSpeed                      : float                                 #   =100.0
    SlideSplit                      : float                                 #   =3.34
    SplitOffset                     : float                                 #   = 0.0
    ChamberPackedLeadOffset         : float                                 #
    def __init__(self, *args, **kwargs):
        super(HitBotSetting, self).__init__()
        self.initialization("HitBotSetting")

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



    def initValue(self):
        super(HitBotSetting, self).initValue()
        self.ID            = 1     #
        self.Number        = 180   #
        self.InPort        = 7     #
        self.CheckWaitTime = 300   #
        self.IsClamp       = 0     #
        self.CheckSpeed    = 100.0 #
        self.SlideSplit    = 3.34  #
        self.SplitOffset   = 0.0   #
        self.OperationWaitTime = 200
        self.ChamberPackedLeadOffset = -5.0

    def UpdateMaxLocation(self):
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        try:
            self.as_field(HitBotConfigModel.first().to_field())
        except Exception as e:
            AppUtils.print_Exception(e)

    def as_field(self, buffer: dict[str, object], is_primary: bool = True):
        primary: str
        index: int
        keys: list[str] = buffer.keys()
        for index, _property in enumerate(self.__field__):
            if _property in keys:
                setattr(self, _property, AppConvert.ObjectConvert(buffer.get(_property, None), self.__types__[index]))

    def to_field(self, is_primary: bool = True) -> dict[str, object]:
        buffer: dict[str, object] = {}
        primary: str = ''
        for index, _property in enumerate(self.__field__):
            buffer[_property] = getattr(self, _property, None)
        return buffer

    def hitbotNumber           (self) -> int:
        Locker: Qt.QReadLocker = Qt.QReadLocker(self.IOLocker)
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        if not self.hasNumber        :
            self.Number = HitBotConfigModel.getNumber()
        return self.Number

    def hitbotInPort           (self) -> int:
        Locker: Qt.QReadLocker = Qt.QReadLocker(self.IOLocker)
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        if not self.hasInPort:
            self.InPort = HitBotConfigModel.getInPort()
        return self.InPort

    def hitbotCheckWaitTime    (self) -> int:
        Locker: Qt.QReadLocker = Qt.QReadLocker(self.IOLocker)
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        if not self.hasCheckWaitTime:
            self.CheckWaitTime = HitBotConfigModel.getCheckWaitTime()
        return self.CheckWaitTime

    def hitbotIsClamp          (self) -> int:
        Locker: Qt.QReadLocker = Qt.QReadLocker(self.IOLocker)
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        if not self.hasIsClamp:
            self.IsClamp = HitBotConfigModel.getIsClamp()
        return self.IsClamp

    def hitbotCheckSpeed       (self) -> int:
        Locker: Qt.QReadLocker = Qt.QReadLocker(self.IOLocker)
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        if not self.hasCheckSpeed:
            self.CheckSpeed = HitBotConfigModel.getCheckSpeed()
        return self.CheckSpeed

    def hitbotSlideSplit       (self) -> float:
        Locker: Qt.QReadLocker = Qt.QReadLocker(self.IOLocker)
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        if not self.hasSlideSplit:
            self.SlideSplit = HitBotConfigModel.getSlideSplit()
        return self.SlideSplit
    def hitbotSplitOffset       (self) -> float:
        Locker: Qt.QReadLocker = Qt.QReadLocker(self.IOLocker)
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        if not self.hasSplitOffset:
            self.SplitOffset = HitBotConfigModel.getSplitOffset()
        return self.SplitOffset

    def hitbotChamberPackedLeadOffset(self) -> float:
        Locker: Qt.QReadLocker = Qt.QReadLocker(self.IOLocker)
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        if not self.hasChamberPackedLeadOffset:
            self.ChamberPackedLeadOffset = HitBotConfigModel.getChamberPackedLeadOffset()
        return self.ChamberPackedLeadOffset

    def setNumber        (self, value: (int, float)):
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        Locker : Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        self.Number        = value
        HitBotConfigModel.setNumber       (self.Number)


    def setInPort        (self, value: (int, float)):
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        Locker : Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        self.InPort        = value
        HitBotConfigModel.setInPort       (self.InPort)

    def getOperationWaitTime(self) -> int:
        Locker: Qt.QReadLocker = Qt.QReadLocker(self.IOLocker)
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        if not self.hasOperationWaitTime:
            self.OperationWaitTime = HitBotConfigModel.getOperationWaitTime()
        return self.OperationWaitTime

    def setOperationWaitTime(self, value: (int, float)):
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        Locker: Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        self.OperationWaitTime = value
        HitBotConfigModel.setOperationWaitTime(self.OperationWaitTime)

    def setCheckWaitTime (self, value: (int, float)):
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        Locker : Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        self.CheckWaitTime = value
        HitBotConfigModel.setCheckWaitTime(self.CheckWaitTime)


    def setIsClamp       (self, value: (int, float, bool)):
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        Locker : Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        self.IsClamp       = 1 if isinstance(value, (int, float, bool)) and value else 0
        HitBotConfigModel.setIsClamp      (self.IsClamp)


    def setCheckSpeed    (self, value: (int, float)):
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        Locker : Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        self.CheckSpeed    = value
        HitBotConfigModel.setCheckSpeed   (self.CheckSpeed)


    def setSlideSplit    (self, value: (int, float)):
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        Locker : Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        self.SlideSplit    = value
        HitBotConfigModel.setSlideSplit   (self.SlideSplit)
    def setSplitOffset    (self, value: (int, float)):
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        Locker : Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        self.SplitOffset    = value
        HitBotConfigModel.setSplitOffset   (self.SplitOffset)

    def setChamberPackedLeadOffset    (self, value: (int, float)):
        from AppCore.AppDBModels.ConfigModels.HitBotConfigModel import HitBotConfigModel
        Locker : Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        self.ChamberPackedLeadOffset    = value
        HitBotConfigModel.setChamberPackedLeadOffset(self.ChamberPackedLeadOffset)

    __field__ = (
        'ID'                      ,
        'Number'                  ,
        'InPort'                  ,
        'CheckWaitTime'           ,
        'IsClamp'                 ,
        'OperationWaitTime'       ,
        'CheckSpeed'              ,
        'SlideSplit'              ,
        'SplitOffset'             ,
        'ChamberPackedLeadOffset' ,
    )
    __types__ = (
        int  ,
        int  ,
        int  ,
        int  ,
        int  ,
        int  ,
        float,
        float,
        float,
        float,
    )

    @property
    def hasID                      (self) -> bool:
        return hasattr(self, 'ID'                       ) and isinstance(self.ID                     , int             )

    @property
    def hasNumber                  (self) -> bool:
        return hasattr(self, 'Number'                   ) and isinstance(self.Number                 , int             )

    @property
    def hasInPort                  (self) -> bool:
        return hasattr(self, 'InPort'                   ) and isinstance(self.InPort                 , int             )

    @property
    def hasCheckWaitTime           (self) -> bool:
        return hasattr(self, 'CheckWaitTime'            ) and isinstance(self.CheckWaitTime          , int             )

    @property
    def hasActionWaitTime          (self) -> bool:
        return hasattr(self, 'ActionWaitTime'           ) and isinstance(self.ActionWaitTime         , int             )

    @property
    def hasIsClamp                 (self) -> bool:
        return hasattr(self, 'IsClamp'                  ) and isinstance(self.IsClamp                , int             )

    @property
    def hasChamberPackedLeadOffset (self) -> bool:
        return hasattr(self, 'ChamberPackedLeadOffset'  ) and isinstance(self.ChamberPackedLeadOffset, (float, int)    )

    @property
    def hasCheckSpeed              (self) -> bool:
        return hasattr(self, 'CheckSpeed'               ) and isinstance(self.CheckSpeed             , (float, int)    )

    @property
    def hasSlideSplit              (self) -> bool:
        return hasattr(self, 'SlideSplit'               ) and isinstance(self.SlideSplit             , (float, int)    )

    @property
    def hasSplitOffset             (self) -> bool:
        return hasattr(self, 'SplitOffset'              ) and isinstance(self.SplitOffset            , (float, int)    )




HitBotConfig: HitBotSetting = HitBotSetting.GetInstance()