from scripts.common.constants import JanusDeploymentConstants
from scripts.common.config import MONGO_DB_OBJ, APP_MONGO_COLLECTION

import cv2

from edge_engine.common.logsetup import logger
#from scripts.common.config import MONGO_DB_OBJ, APP_MONGO_COLLECTION
#from scripts.common.constants import JanusDeploymentConstants


class Utilities:

    @classmethod
    def get_extra_fields(
            cls,
            device_id,
    ):
        _janus_deployment = MONGO_DB_OBJ[
            APP_MONGO_COLLECTION.get(JanusDeploymentConstants.JANUS_DEPLOYMENT_COLLECTION)].find_one(
            {JanusDeploymentConstants.DEPLOYMENT_ID: device_id}).get(
            JanusDeploymentConstants.EXTRA_FIELDS_KEY)
        if _janus_deployment is None:
            raise ValueError("Janus deployment configuration is not found/corrupted")

        _key_dictionary = dict()
        for each_field in _janus_deployment:
            _key_dictionary[each_field['key']] = each_field['value']
        return _key_dictionary

    @classmethod
    def get_direction(
            cls,
            device_id,
    ):
        logger.debug("Getting the direction from DB")
        return MONGO_DB_OBJ[APP_MONGO_COLLECTION.get(JanusDeploymentConstants.JANUS_DEPLOYMENT_COLLECTION)].find_one(
            {JanusDeploymentConstants.DEPLOYMENT_ID: device_id}).get(
            JanusDeploymentConstants.DIRECTION_KEY)

    @classmethod
    def set_direction(
            cls,
            device_id: str,
            direction: bool,
    ):
        logger.debug("Updating the direction in DB")
        updated_values = {"$set": {JanusDeploymentConstants.DIRECTION_KEY: direction}}
        MONGO_DB_OBJ[APP_MONGO_COLLECTION.get(JanusDeploymentConstants.JANUS_DEPLOYMENT_COLLECTION)].update_one(
            {JanusDeploymentConstants.DEPLOYMENT_ID: device_id}, updated_values)

    @classmethod
    def draw_circles_on_frame(
            cls,
            frame,
            point,
            radius=3,
            color=(255, 255, 255),
            thickness=1,
    ):
        """
        draw circle on the objects
        :param radius: radius of the circle
        :param frame: frame to draw on
        :param point: co-ordinate to draw on
        :param color: color of the circle
        :param thickness: thickness of the circle
        :return: frame
        """
        return cv2.circle(frame, tuple(point), radius, color, thickness)

    @classmethod
    def resize_to_64_64(
            cls,
            frame,
    ):
        """
        resize the from
        :param frame: frame
        :return: frame
        """
        return cv2.resize(frame, (64, 64))


def get_extra_fields(device_id):
    # _janus_deployment = [
    #     {
    #         "type": "number",
    #         "key": "x1",
    #         "value": 1000
    #     },
    #     {
    #         "type": "number",
    #         "key": "y1",
    #         "value": 0
    #     },
    #     {
    #         "type": "number",
    #         "key": "x2",
    #         "value": 1001
    #     },
    #     {
    #         "type": "number",
    #         "key": "y2",
    #         "value": 720
    #     },
    #     {
    #         "type": "dropdown",
    #         "key": "alignment",
    #         "value": "vertical"
    #     }
    # ]
    _janus_deployment = MONGO_DB_OBJ[APP_MONGO_COLLECTION.get(JanusDeploymentConstants.JANUS_DEPLOYMENT_COLLECTION)]. \
        find_one({JanusDeploymentConstants.DEPLOYMENT_ID: device_id}).get(JanusDeploymentConstants.EXTRA_FIELDS_KEY)

    if _janus_deployment is None:
        raise ValueError("Janus deployment configuration is not found/corrupted")

    _key_dictionary = dict()
    for each_field in _janus_deployment:
        _key_dictionary[each_field['key']] = each_field['value']
    return _key_dictionary
