"""Module for a asteex static test instance."""

from asteex.abstract.instance import Instance


class StaticInstance(Instance):
    """Class for a test instance with fixed context sources and case."""

    def __init__(self, instance_id, case, context_sources):
        self.__instance_id = str(instance_id)
        self.__case = case
        self.__context_sources = _validate_context_sources(context_sources)

    def get_id(self):
        """Get the unique identifier of the instance.

        :returns: string id.
        """
        return self.__instance_id

    def get_case(self):
        """Get test case.

        :returns: :class:`Case <asteex.abstract.case.Case>`.
        """
        return self.__case

    def get_context_sources(self):
        """Get information about origins of the context values.

        :returns: dictionary with string names as keys and
            :class:`Source <asteex.abstract.context.Source>` as values.
        """
        return self.__context_sources


def _validate_context_sources(context_sources):
    """Validate context sources.

    :param context_sources: dictionary with string keys and
        :class:`Sources <asteex.abstract.context.Source>` as values.
    :returns: a validated context sources of the same type.
    :raises: :class:`ValueError` if context sources are invalid.
    """
    try:
        context_sources_dict = dict(context_sources)
    except (TypeError, ValueError) as error:
        raise ValueError("Context sources are not a valid dictionary") from error

    refined_context_sources = {str(name): source for name, source in context_sources_dict.items()}

    if len(context_sources) > len(refined_context_sources):
        raise ValueError("Several sources use the same name")

    return refined_context_sources
