#!D:\IDE\Anaconda3\envs\Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName: EventLevel.py
 @DateTime: 2025/6/28 12:16
 @SoftWare: PyCharm
 @Author  ：xsh
"""
from PyQt5 import Qt

class EventLevel(int):
    WebSocketMode       = 0x01             # 网络发送模式
    QualityMode         = 0x02             # 评价数据模式
    PersistenceMode     = 0x04             # 保存数据模式
    OnlyDrawMode        = 0x08             # 只向调试界面显示
    IdentificationMode  = 0x10             # 核型图像识别

    @classmethod
    def all(cls) -> list[int]:
        return [cls.WebSocketMode   , cls.QualityMode     , cls.PersistenceMode , cls.OnlyDrawMode, cls.IdentificationMode]

    @classmethod
    def setLevel(cls, src: int, level: int) -> int:
        _level = level  if level in cls.all() else cls.OnlyDrawMode
        return cls.set_bit(src, cls.all().index(_level), True)

    @classmethod
    def cancelLevel(cls, src: int, level: int) -> int:
        _level = level if level in cls.all() else cls.OnlyDrawMode
        return cls.set_bit(src, cls.all().index(_level), False)

    @classmethod
    def cancelIdentificationMode(cls, src: int) -> int:
        return cls.cancelLevel(src, cls.IdentificationMode      )

    @classmethod
    def cancelWebSocketMode      (cls, src: int) -> int:
        return cls.cancelLevel(src, cls.WebSocketMode      )

    @classmethod
    def cancelQualityMode        (cls, src: int) -> int:
        return cls.cancelLevel(src, cls.QualityMode        )

    @classmethod
    def cancelPersistenceMode    (cls, src: int) -> int:
        return cls.cancelLevel(src, cls.PersistenceMode    )

    @classmethod
    def cancelOnlyDrawMode       (cls, src: int) -> int:
        return cls.cancelLevel(src, cls.OnlyDrawMode       )
    @classmethod
    def setWebSocketMode      (cls, src: int) -> int:
        return cls.setLevel(src, cls.WebSocketMode      )

    @classmethod
    def setQualityMode        (cls, src: int) -> int:
        return cls.setLevel(src, cls.QualityMode        )

    @classmethod
    def setPersistenceMode    (cls, src: int) -> int:
        return cls.setLevel(src, cls.PersistenceMode    )

    @classmethod
    def setOnlyDrawMode       (cls, src: int) -> int:
        return cls.setLevel(src, cls.OnlyDrawMode       )

    @classmethod
    def setIdentificationMode(cls, src: int) -> int:
        return cls.setLevel(src, cls.IdentificationMode)

    @classmethod
    def isIdentificationMode(cls, src: int) -> bool:
        return cls.get_bit(src, cls.all().index(cls.IdentificationMode))

    @classmethod
    def isWebSocketMode      (cls, src: int) -> bool:
        return cls.get_bit(src, cls.all().index(cls.WebSocketMode      ))

    @classmethod
    def isQualityMode        (cls, src: int) -> bool:
        return cls.get_bit(src, cls.all().index(cls.QualityMode        ))

    @classmethod
    def isPersistenceMode    (cls, src: int) -> bool:
        return cls.get_bit(src, cls.all().index(cls.PersistenceMode    ))

    @classmethod
    def isOnlyDrawMode       (cls, src: int) -> bool:
        return cls.get_bit(src, cls.all().index(cls.OnlyDrawMode       ))

    @classmethod
    def isOtherMode(cls, src: int) -> bool:
        return not (cls.WebSocketMode <= src <= cls.IdentificationMode)

    @staticmethod
    def set_bit(number: int, bit_position: int, enable: bool) -> int:
        """
        Set the bit at the specified position to 1 or 0

        Args:
            number: The original number
            bit_position: The position of the bit to set (0-based)
            enable: True to set bit to 1, False to set bit to 0

        Returns:
            The number with the specified bit set to 1 or 0
        """
        if enable:
            return number | (1 << bit_position)
        else:
            return number & ~(1 << bit_position)

    @staticmethod
    def get_bit(number: int, bit_position: int) -> int:
        """
        Get the value of the bit at the specified position

        Args:
            number: The number to check
            bit_position: The position of the bit to get (0-based)

        Returns:
            0 or 1 depending on the bit value
        """
        return (number >> bit_position) & 1
