import inspect
from ajunit.testlistener import TestListener


class SuiteBuilder(object):

    __UNIQUE_TEST_SUITES = []
    __EXECUTION_ROSTER = {}
    __CURRENT_SUITE_OBJECT = None

    @staticmethod
    def get_execution_roster():
        return SuiteBuilder.__EXECUTION_ROSTER

    @staticmethod
    def __set_current_suite_object_defaults():
        from ajunit.decorators import DecoratorType
        SuiteBuilder.__CURRENT_SUITE_OBJECT = {"test_listener": None,
                                               "class_name": None,
                                               "class_retry": None,
                                               "class_skip": False,
                                               "class_object": None,
                                               "suite_definition": {DecoratorType.BEFORE_CLASS: [],
                                                                    DecoratorType.BEFORE_TEST: [],
                                                                    DecoratorType.TEST_CASE: [],
                                                                    DecoratorType.AFTER_TEST: [],
                                                                    DecoratorType.AFTER_CLASS: []}}

    @staticmethod
    def build_suite_definitions(decorated_function, decorator_kwargs, decorator_type):
        from ajunit.debugjunkie import LogJunkie
        _function_name = None
        _class_name = None
        if inspect.isfunction(decorated_function):
            # _class_name = str(decorated_function.__qualname__.split(".")[0])
            _function_name = decorated_function.__name__
        else:
            if decorated_function is not None:
                # _class_name = decorated_function.__qualname__
                _class_name = decorated_function.__name__
                SuiteBuilder.__CURRENT_SUITE_OBJECT["class_object"] = decorated_function
                SuiteBuilder.__CURRENT_SUITE_OBJECT["class_retry"] = decorator_kwargs.get("retry", 1)
                SuiteBuilder.__CURRENT_SUITE_OBJECT["class_skip"] = decorator_kwargs.get("skip", False)
                SuiteBuilder.__CURRENT_SUITE_OBJECT["class_meta"] = decorator_kwargs.get("meta", None)
                SuiteBuilder.__CURRENT_SUITE_OBJECT["test_listener"] = decorator_kwargs.get("listener", TestListener)

        if SuiteBuilder.__CURRENT_SUITE_OBJECT is None:
            SuiteBuilder.__set_current_suite_object_defaults()
        elif SuiteBuilder.__CURRENT_SUITE_OBJECT.get("class_name", None) is None:
            SuiteBuilder.__CURRENT_SUITE_OBJECT["class_name"] = _class_name

        if _function_name is not None:
            SuiteBuilder.__CURRENT_SUITE_OBJECT["suite_definition"][decorator_type]\
                        .append({"decorated_function": decorated_function, "decorator_kwargs": decorator_kwargs})
            LogJunkie.debug("=======================Suite Definition Updated=============================")
            LogJunkie.debug("Function: {}".format(_function_name))
            LogJunkie.debug("Decorator Type: {}".format(decorator_type))
            LogJunkie.debug("Decorator Arguments: {}".format(decorator_kwargs))
            LogJunkie.debug("Function object: {}".format(decorated_function))
            LogJunkie.debug("============================================================================")
        else:
            LogJunkie.debug("=======================Suite Definition Finished=============================")
            LogJunkie.debug("Suite: {}".format(_class_name))
            LogJunkie.debug("Suite Definition: {}".format(SuiteBuilder.__CURRENT_SUITE_OBJECT))
            SuiteBuilder.__EXECUTION_ROSTER.update({decorated_function: SuiteBuilder.__CURRENT_SUITE_OBJECT})
            SuiteBuilder.__UNIQUE_TEST_SUITES.append(_class_name)
            SuiteBuilder.__set_current_suite_object_defaults()
            LogJunkie.debug(">> Definition reset for next suite <<")
            LogJunkie.debug("=============================================================================\n\n")
