from msgpack import unpackb

cdef class AIRecognitionConfig:
    def __init__(self,
                 frame_period_recognition,
                 frame_recognition_seconds,
                 probability_threshold,

                 tracking_distance_confidence,
                 tracking_probability_increase,
                 tracking_intersection_threshold,

                 file_data,
                 paths,
                 model_batch_size,

                 big_image_tile_overlap_percent,

                 altitude,
                 focal_length,
                 sensor_width
                 ):
        self.frame_period_recognition = frame_period_recognition
        self.frame_recognition_seconds = frame_recognition_seconds
        self.probability_threshold = probability_threshold

        self.tracking_distance_confidence = tracking_distance_confidence
        self.tracking_probability_increase = tracking_probability_increase
        self.tracking_intersection_threshold = tracking_intersection_threshold

        self.file_data = file_data
        self.paths = paths
        self.model_batch_size = model_batch_size

        self.big_image_tile_overlap_percent = big_image_tile_overlap_percent

        self.altitude = altitude
        self.focal_length = focal_length
        self.sensor_width = sensor_width

    def __str__(self):
        return (f'frame_seconds : {self.frame_recognition_seconds}, distance_confidence : {self.tracking_distance_confidence}, '
                f'probability_increase : {self.tracking_probability_increase}, '
                f'intersection_threshold : {self.tracking_intersection_threshold}, '
                f'frame_period_recognition : {self.frame_period_recognition}, '
                f'big_image_tile_overlap_percent: {self.big_image_tile_overlap_percent}, '
                f'paths: {self.paths}, '
                f'model_batch_size: {self.model_batch_size}, '
                f'altitude: {self.altitude}, '
                f'focal_length: {self.focal_length}, '
                f'sensor_width: {self.sensor_width}'
                )

    @staticmethod
    cdef from_msgpack(bytes data):
        unpacked = unpackb(data, strict_map_key=False)
        return AIRecognitionConfig(
            unpacked.get("f_pr", 0),
            unpacked.get("f_rs", 0.0),
            unpacked.get("pt", 0.0),

            unpacked.get("t_dc", 0.0),
            unpacked.get("t_pi", 0.0),
            unpacked.get("t_it", 0.0),

            unpacked.get("d", b''),
            unpacked.get("p", []),
            unpacked.get("m_bs"),

            unpacked.get("ov_p", 20),

            unpacked.get("cam_a", 400),
            unpacked.get("cam_fl", 24),
            unpacked.get("cam_sw", 23.5)
        )

    @staticmethod
    def from_dict(dict data):
        return AIRecognitionConfig(
            data.get("frame_period_recognition", 4),
            data.get("frame_recognition_seconds", 2),
            data.get("probability_threshold", 0.25),

            data.get("tracking_distance_confidence", 0.0),
            data.get("tracking_probability_increase", 0.0),
            data.get("tracking_intersection_threshold", 0.6),

            data.get("file_data", b''),
            data.get("paths", []),
            data.get("model_batch_size", 1),

            data.get("big_image_tile_overlap_percent", 20),

            data.get("altitude", 400),
            data.get("focal_length", 24),
            data.get("sensor_width", 23.5)
        )