import json
import sys

from loguru import logger

cdef str CONFIG_FILE = "config.yaml"  # Port for the zmq

cdef str QUEUE_CONFIG_FILENAME = "secured-config.json"
cdef str AI_ONNX_MODEL_FILE = "azaion.onnx"

cdef str CDN_CONFIG = "cdn.yaml"
cdef str MODELS_FOLDER = "models"

cdef int SMALL_SIZE_KB = 3

cdef str SPLIT_SUFFIX = "!split!"
cdef double TILE_DUPLICATE_CONFIDENCE_THRESHOLD = 0.01
cdef int METERS_IN_TILE = 25

cdef class AnnotationClass:
    def __init__(self, id, name, color, max_object_size_meters):
        self.id = id
        self.name = name
        self.color = color
        self.max_object_size_meters = max_object_size_meters

    def __str__(self):
        return f'{self.id} {self.name} {self.color} {self.max_object_size_meters}'

cdef int weather_switcher_increase = 20

WEATHER_MODE_NAMES = {
    Norm: "Norm",
    Wint: "Wint",
    Night: "Night"
}

with open('classes.json', 'r', encoding='utf-8') as f:
    j = json.loads(f.read())
    annotations_dict = {}

    for i in range(0, weather_switcher_increase * 3, weather_switcher_increase):
        for cl in j:
            id = i + cl['Id']
            mode_name = WEATHER_MODE_NAMES.get(i, "Unknown")
            name = cl['Name'] if i == 0 else f'{cl["Name"]}({mode_name})'
            annotations_dict[id] = AnnotationClass(id, name, cl['Color'], cl['MaxSizeM'])

logger.remove()
log_format = "[{time:HH:mm:ss} {level}] {message}"
logger.add(
    sink="Logs/log_inference_{time:YYYYMMDD}.txt",
    level="INFO",
    format=log_format,
    enqueue=True,
    rotation="1 day",
    retention="30 days",
)
logger.add(
    sys.stdout,
    level="DEBUG",
    format=log_format,
    filter=lambda record: record["level"].name in ("INFO", "DEBUG", "SUCCESS"),
    colorize=True
)
logger.add(
    sys.stderr,
    level="WARNING",
    format=log_format,
    colorize=True
)

cdef log(str log_message):
    logger.info(log_message)

cdef logerror(str error):
    logger.error(error)

cdef format_time(int ms):
    # Calculate hours, minutes, seconds, and hundreds of milliseconds.
    h = ms // 3600000  # Total full hours.
    ms_remaining = ms % 3600000
    m = ms_remaining // 60000  # Full minutes.
    ms_remaining %= 60000
    s = ms_remaining // 1000  # Full seconds.
    f = (ms_remaining % 1000) // 100  # Hundreds of milliseconds.
    h = h % 10
    return f"{h}{m:02}{s:02}{f}"
