# -*- coding: utf-8 -*-
from loguru import logger
from enum import Enum, unique

class ServiceException(Exception): 
    def __init__(self, code, msg, desc=None):
        self.code = code
        if desc is None:
            self.msg = msg
        else:
            self.msg = msg % desc

    def __str__(self):
        logger.error("异常编码：{}, 异常描述：{}", self.code, self.msg)

# 异常枚举
@unique
class ExceptionType(Enum):
    OR_VIDEO_ADDRESS_EXCEPTION = ("SP000", "未拉取到视频流, 请检查拉流地址是否有视频流!")
    ANALYSE_TIMEOUT_EXCEPTION = ("SP001", "AI分析超时!")
    PULLSTREAM_TIMEOUT_EXCEPTION = ("SP002", "原视频拉流超时!")
    READSTREAM_TIMEOUT_EXCEPTION = ("SP003", "原视频读取视频流超时!")
    GET_VIDEO_URL_EXCEPTION = ("SP004", "获取视频播放地址失败!")
    GET_VIDEO_URL_TIMEOUT_EXCEPTION = ("SP005", "获取原视频播放地址超时!")
    PULL_STREAM_URL_EXCEPTION = ("SP006", "拉流地址不能为空!")
    PUSH_STREAM_URL_EXCEPTION = ("SP007", "推流地址不能为空!")
    PUSH_STREAM_TIME_EXCEPTION = ("SP008", "未生成本地视频地址!")
    AI_MODEL_MATCH_EXCEPTION = ("SP009", "未匹配到对应的AI模型!")
    ILLEGAL_PARAMETER_FORMAT = ("SP010", "非法参数格式!")
    PUSH_STREAMING_CHANNEL_IS_OCCUPIED = ("SP011", "推流通道可能被占用, 请稍后再试!")
    VIDEO_RESOLUTION_EXCEPTION = ("SP012", "不支持该分辨率类型的视频，请切换分辨率再试!")
    READ_IAMGE_URL_EXCEPTION = ("SP013", "未能解析图片地址!")
    DETECTION_TARGET_TYPES_ARE_NOT_SUPPORTED = ("SP014", "不支持该类型的检测目标!")
    WRITE_STREAM_EXCEPTION = ("SP015", "写流异常!")
    OR_VIDEO_DO_NOT_EXEIST_EXCEPTION = ("SP016", "原视频不存在!")
    MODEL_LOADING_EXCEPTION = ("SP017", "模型加载异常!")
    MODEL_ANALYSE_EXCEPTION = ("SP018", "算法模型分析异常!")
    AI_MODEL_CONFIG_EXCEPTION = ("SP019", "模型配置不能为空!")
    AI_MODEL_GET_CONFIG_EXCEPTION = ("SP020", "获取模型配置异常, 请检查模型配置是否正确!")
    MODEL_GROUP_LIMIT_EXCEPTION = ("SP021", "模型组合个数超过限制!")
    MODEL_NOT_SUPPORT_VIDEO_EXCEPTION = ("SP022", "%s不支持视频识别!")
    MODEL_NOT_SUPPORT_IMAGE_EXCEPTION = ("SP023", "%s不支持图片识别!")
    THE_DETECTION_TARGET_CANNOT_BE_EMPTY = ("SP024", "检测目标不能为空!")
    URL_ADDRESS_ACCESS_FAILED = ("SP025", "URL地址访问失败, 请检测URL地址是否正确!")
    UNIVERSAL_TEXT_RECOGNITION_FAILED = ("SP026", "识别失败!")
    COORDINATE_ACQUISITION_FAILED = ("SP027", "飞行坐标识别异常!")
    PUSH_STREAM_EXCEPTION = ("SP028", "推流异常!")
    MODEL_DUPLICATE_EXCEPTION = ("SP029", "存在重复模型配置!")
    DETECTION_TARGET_NOT_SUPPORT = ("SP031", "存在不支持的检测目标!")
    TASK_EXCUTE_TIMEOUT = ("SP032", "任务执行超时!")
    PUSH_STREAM_URL_IS_NULL = ("SP033", "拉流、推流地址不能为空!")
    PULL_STREAM_NUM_LIMIT_EXCEPTION = ("SP034", "转推流数量超过限制!")
    NOT_REQUESTID_TASK_EXCEPTION = ("SP993", "未查询到该任务，无法停止任务！")
    NO_RESOURCES = ("SP995", "服务器暂无资源可以使用，请稍后30秒后再试!")
    NO_CPU_RESOURCES = ("SP996", "暂无CPU资源可以使用，请稍后再试!")
    SERVICE_COMMON_EXCEPTION = ("SP997", "公共服务异常!")
    NO_GPU_RESOURCES = ("SP998", "暂无GPU资源可以使用，请稍后再试!")
    SERVICE_INNER_EXCEPTION = ("SP999", "系统内部异常!")
