import abc
from typing import Callable
from enum import IntEnum

from .opf_functions import OPFFunctions, logging, OPFDialogAnswer
from UniTAP.dev.modules.opf.parsers import OPFParametersParser


class WrongOPFFunctionSignature(Exception):
    pass


class OpfHandlerBase:
    """
    Class `OpfHandlerBase` allows working with functions that helps to do required actions during DUT tests.
    - Use DSC content library `set_dsc_content_library`. If DSC image does not exist, it will be generated.
    """
    __FUNCTIONS_MAP = {
        19: OPFFunctions.opf_19_handler,
        103: OPFFunctions.opf_103_handler,
    }

    def __init__(self):
        self.__parser = OPFParametersParser()
        self.__tx = None
        self.__rx = None
        self.__callback_before_handling = {}
        self._dsc_content_library_path = ""

    def _set_roles(self, role_tx, role_rx):
        self.__tx = role_tx
        self.__rx = role_rx

    def set_dsc_content_library(self, dsc_content_library_path: str = ""):
        """
        Set new path to DSC content library folder.
        If path is empty, content library will not use (all images will be generated into OPF functions without saving).

        Arguments:
            dsc_content_library_path (`str`) - full path to DSC content library folder.
        """
        self._dsc_content_library_path = dsc_content_library_path

    def handle(self, opf_id, reply_answer, *args) -> OPFDialogAnswer:
        opf_params = self.__parser.parse(opf_id, *args)

        OPFFunctions.dsc_content_library_path = self._dsc_content_library_path
        return reply_answer(self.__FUNCTIONS_MAP.get(opf_id)(self.__tx, self.__rx, *opf_params))

    def _is_opf_processed(self, opf_id: int):
        return self.__FUNCTIONS_MAP.get(opf_id) is not None
