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

class LogMode(int):
    LOG_READ = 0
    LOG_SEND = 1
    @classmethod
    def isReadable(cls, value: int) -> int:
        return value == cls.LOG_READ

    @classmethod
    def isWritable(cls, value: int) -> int:
        return value == cls.LOG_SEND



class LogLevel(int):
    LOG_OTHER       = -1
    LOG_DEBUG       = 0
    LOG_WARNING     = 1
    LOG_SYSTEM      = 2
    LOG_FATAL       = 3
    LOG_INFO        = 4
    LOG_MODBUS      = 5
    LOG_SERIAL      = 6
    LOG_MANIPULATOR = 7
    LOG_MICROSCOPE  = 8
    LOG_FLOW        = 9
    LOG_DB          = 10
    LOG_WEBSERVER   = 11
    LOG_CAMERA      = 12
    LOG_HITBOT      = 13
    LOG_SOCKET      = 14
    LOG_SCANNING    = 15
    @staticmethod
    def toDatePath() -> str:
        return Qt.QDate.currentDate().toString('yyyyMMdd')
    @classmethod
    def all(cls) -> list[int]:
        return [
            cls.LOG_DEBUG       ,
            cls.LOG_WARNING     ,
            cls.LOG_SYSTEM      ,
            cls.LOG_FATAL       ,
            cls.LOG_INFO        ,
            cls.LOG_MODBUS      ,
            cls.LOG_SERIAL      ,
            cls.LOG_MANIPULATOR ,
            cls.LOG_MICROSCOPE  ,
            cls.LOG_FLOW        ,
            cls.LOG_DB          ,
            cls.LOG_WEBSERVER   ,
            cls.LOG_CAMERA      ,
            cls.LOG_HITBOT      ,
            cls.LOG_SOCKET      ,
            cls.LOG_SCANNING    ,
            cls.LOG_OTHER       ,
        ]
    @classmethod
    def toList(cls) -> list[str]:
        return [
            'DEBUG'         ,
            'WARNING'       ,
            'SYSTEM'        ,
            'FATAL'         ,
            'INFO'          ,
            'MODBUS'        ,
            'SERIAL'        ,
            'MANIPULATOR'   ,
            'MICROSCOPE'    ,
            'FLOW'          ,
            'DB'            ,
            'WEBSERVER'     ,
            'CAMERA'        ,
            'HITBOT'        ,
            'SOCKET'        ,
            'SCANNING'      ,
            'OTHER'         ,
        ]

    @classmethod
    def toFiles(cls) -> dict[int, str]:
        filenames: list[str] = cls.toList()
        return {
            cls.LOG_OTHER       : f'log/{cls.toDatePath()}/{filenames[cls.LOG_OTHER       ]}.log',
            cls.LOG_DEBUG       : f'log/{cls.toDatePath()}/{filenames[cls.LOG_DEBUG       ]}.log',
            cls.LOG_WARNING     : f'log/{cls.toDatePath()}/{filenames[cls.LOG_WARNING     ]}.log',
            cls.LOG_SYSTEM      : f'log/{cls.toDatePath()}/{filenames[cls.LOG_SYSTEM      ]}.log',
            cls.LOG_FATAL       : f'log/{cls.toDatePath()}/{filenames[cls.LOG_FATAL       ]}.log',
            cls.LOG_INFO        : f'log/{cls.toDatePath()}/{filenames[cls.LOG_INFO        ]}.log',
            cls.LOG_MODBUS      : f'log/{cls.toDatePath()}/{filenames[cls.LOG_MODBUS      ]}.log',
            cls.LOG_SERIAL      : f'log/{cls.toDatePath()}/{filenames[cls.LOG_SERIAL      ]}.log',
            cls.LOG_MANIPULATOR : f'log/{cls.toDatePath()}/{filenames[cls.LOG_MANIPULATOR ]}.log',
            cls.LOG_MICROSCOPE  : f'log/{cls.toDatePath()}/{filenames[cls.LOG_MICROSCOPE  ]}.log',
            cls.LOG_FLOW        : f'log/{cls.toDatePath()}/{filenames[cls.LOG_FLOW        ]}.log',
            cls.LOG_DB          : f'log/{cls.toDatePath()}/{filenames[cls.LOG_DB          ]}.log',
            cls.LOG_WEBSERVER   : f'log/{cls.toDatePath()}/{filenames[cls.LOG_WEBSERVER   ]}.log',
            cls.LOG_CAMERA      : f'log/{cls.toDatePath()}/{filenames[cls.LOG_CAMERA      ]}.log',
            cls.LOG_HITBOT      : f'log/{cls.toDatePath()}/{filenames[cls.LOG_HITBOT      ]}.log',
            cls.LOG_SOCKET      : f'log/{cls.toDatePath()}/{filenames[cls.LOG_SOCKET      ]}.log',
            cls.LOG_SCANNING    : f'log/{cls.toDatePath()}/{filenames[cls.LOG_SCANNING    ]}.log',
        }

    @classmethod
    def filename(cls, level: int) -> str:
        filename: str
        if level in cls.all():
            filename = cls.toFiles().get(level)
        else:
            filename = cls.toFiles().get(cls.LOG_DEBUG)
        return filename


    @classmethod
    def toValue(cls, value: str) -> int:
        index = cls.toList().index(value)
        return index if index >= 0 else cls.LOG_DEBUG

    @classmethod
    def toString(cls, value: int) -> str:
        return cls.toList()[value] if value in cls.all() else 'DEBUG'

    @classmethod
    def Format(cls, level: int, buffer: str) -> str:
        return f"[{cls.toString(level)}][{Qt.QDateTime.currentDateTime().toString('yyyy-MM-dd HH:mm::ss')}]\t{buffer}"

