#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：SystemSetting.py
@Description：
@Author：XSH
@Time：2025/11/11 17:04
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from PyQt5 import Qt
from AppInterfaces.SingletonInterface import SingletonInterface
from AppInterfaces.ConfigureInterfaces import ImageConfigInterface, SystemConfigInterface, TrayConfigInterface
from AppCore.Levels import ObjectiveLevel
class SystemSetting(SystemConfigInterface, SingletonInterface):
    Locker  : Qt.QMutex             = Qt.QMutex         ()
    IOLocker: Qt.QReadWriteLock     = Qt.QReadWriteLock ()

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

    def __init__(self, *args, **kwargs):
        super(SystemSetting, self).__init__()
        self.initialization("SystemSetting")




    def initValue(self):
        super(SystemSetting, self).initValue()
        self.ID                                   = 1
        self.ReceivedTails                        = None
        self.ReceivedWaits                        = 500
        self.MaxWaitTimes                         = 100
        self.MinChamberNumber                     = 1
        self.MaxChamberNumber                     = 24
        self.MinRowNumber                         = 1
        self.MaxRowNumber                         = 5
        self.MinTrayRowNumber                     = 1
        self.MinTrayColNumber                     = 1
        self.MaxTrayRowNumber                     = 25
        self.MaxTrayColNumber                     = 40
        self.MaxTrayRowNumber20X                  = 35
        self.MaxTrayColNumber20X                  = 60
        self.MODBUS_POOL_LENGTH                   = 8
        self.DebugMode                            = 1
        self.BuzzerMode                           = 0
        self.EnglishMode                          = 0
        self.WarningSpaceTimes                    = 60000
        self.CameraModel                          = None
        self.ImageConfigure                       = None
        self.TrayConfigure                        = None



    def UpdateMaxLocation(self):
        from AppCore.AppDBModels.ConfigModels.SystemConfigModel import SystemConfigModel
        SystemConfigModel.UpdateMaxLocation()
        self.as_field(SystemConfigModel.first().to_field())
        self.initTrayConfigure()
        self.initImageConfigure()
        if self.hasTrayConfigure:
            self.tray.UpdateMaxLocation()

        if self.hasImageConfigure:
            self.image.UpdateMaxLocation()



    @property
    def getCameraName(self) -> str:
        return self.CameraName if self.hasCameraName else None

    @property
    def camera_names(self) -> list[str]:
        return [] if not self.hasCameraName else self.CameraName.split(',')

    def setCameraName(self, value: str):
        from AppCore.AppDBModels.ConfigModels.SystemConfigModel import SystemConfigModel
        Locker: Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        self.CameraName = value if isinstance(value, str) else None
        SystemConfigModel.setCameraName(self.CameraName)

    @property
    def isDebugMode(self) -> int:
        return self.DebugMode if self.hasDebugMode else 0

    
    def setDebugMode(self, enable: (bool, int)):
        from AppCore.AppDBModels.ConfigModels.SystemConfigModel import SystemConfigModel
        Locker: Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        self.DebugMode = 1 if isinstance(enable, (bool, int)) and enable else 0
        SystemConfigModel.setDebugMode(self.DebugMode)

    @property
    def isBuzzerMode(self) -> bool:
        from AppCore.AppDBModels.ConfigModels.SystemConfigModel import SystemConfigModel
        Locker: Qt.QReadLocker = Qt.QReadLocker(self.IOLocker)
        self.BuzzerMode = SystemConfigModel.isBuzzerMode()
        return self.BuzzerMode if self.hasBuzzerMode else 0

    
    def setBuzzerMode(self, enable: (bool, int)):
        from AppCore.AppDBModels.ConfigModels.SystemConfigModel import SystemConfigModel
        Locker: Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        self.BuzzerMode = 1 if isinstance(enable, (bool, int)) and enable else 0
        SystemConfigModel.setDebugMode(self.BuzzerMode)

    @property
    def isEnglishMode(self) -> bool:
        from AppCore.AppDBModels.ConfigModels.SystemConfigModel import SystemConfigModel
        Locker: Qt.QReadLocker = Qt.QReadLocker(self.IOLocker)
        self.EnglishMode = SystemConfigModel.isEnglishMode()
        return self.EnglishMode if self.hasEnglishMode else 0

    
    def setEnglishMode(self, enable: (bool, int)):
        from AppCore.AppDBModels.ConfigModels.SystemConfigModel import SystemConfigModel
        Locker: Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        self.EnglishMode = 1 if isinstance(enable, (bool, int)) and enable else 0
        SystemConfigModel.setEnglishMode(self.EnglishMode)

    
    def WarningWaitTimes(self) -> int:
        from AppCore.AppDBModels.ConfigModels.SystemConfigModel import SystemConfigModel
        Locker: Qt.QReadLocker = Qt.QReadLocker(self.IOLocker)
        self.WarningSpaceTimes = SystemConfigModel.getWarningSpaceTimes()
        return 60000 if not self.hasWarningSpaceTimes else self.WarningSpaceTimes

    def setWarningWaitTimes(self, waittime: (float, int)):
        from AppCore.AppDBModels.ConfigModels.SystemConfigModel import SystemConfigModel
        Locker: Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        self.WarningSpaceTimes = int(waittime) if isinstance(waittime, (float, int)) else self.WarningSpaceTimes
        SystemConfigModel.setWarningSpaceTimes(self.WarningSpaceTimes)


    def isLeiCaMicroscope(self) -> bool:
        from AppCore.AppDBModels.ConfigModels.SystemConfigModel import SystemConfigModel
        Locker: Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        self.IsLeiCa = SystemConfigModel.isLeiCaMicroscope()
        return self.IsLeiCa if self.hasIsLeiCa else False

    def setLeiCaMicroscope(self, enable: (bool, int)):
        from AppCore.AppDBModels.ConfigModels.SystemConfigModel import SystemConfigModel
        Locker: Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        self.IsLeiCa = 1 if isinstance(enable, (bool, int)) and enable else 0
        SystemConfigModel.setLeiCaMicroscope(self.IsLeiCa)


    
    def initTrayConfigure(self):
        if not  self.hasTrayConfigure:
            from AppSettings.TrayConfigure import TrayConfigure
            self.TrayConfigure = TrayConfigure()
            self.TrayConfigure.UpdateMaxLocation()

    
    def initImageConfigure(self):
        if not self.hasImageConfigure:
            from AppSettings.ImageConfigure import ImageConfigure
            self.ImageConfigure = ImageConfigure()
            self.ImageConfigure.UpdateMaxLocation()




system_config: SystemSetting = SystemSetting.GetInstance()
SystemConfig : SystemSetting = SystemSetting.GetInstance()