from .configuration_controller import ConfigurationController
from .constants import (
    DEFAULT_SHOW_INDEX_EPISODE_DIGITS,
    DEFAULT_SHOW_INDEX_SEASON_DIGITS,
    DEFAULT_SHOW_INDICATOR_EPISODE_DIGITS,
    DEFAULT_SHOW_INDICATOR_SEASON_DIGITS,
)
from .logging_utils import get_ffx_logger


class ShowDescriptor():
    """This class represents the structural content of a media file including streams and metadata"""

    CONTEXT_KEY = 'context'
    
    ID_KEY = 'id'
    NAME_KEY = 'name'
    YEAR_KEY = 'year'

    INDEX_SEASON_DIGITS_KEY = 'index_season_digits'
    INDEX_EPISODE_DIGITS_KEY = 'index_episode_digits'
    INDICATOR_SEASON_DIGITS_KEY = 'indicator_season_digits'
    INDICATOR_EPISODE_DIGITS_KEY = 'indicator_episode_digits'
    QUALITY_KEY = 'quality'
    NOTES_KEY = 'notes'

    DEFAULT_INDEX_SEASON_DIGITS = DEFAULT_SHOW_INDEX_SEASON_DIGITS
    DEFAULT_INDEX_EPISODE_DIGITS = DEFAULT_SHOW_INDEX_EPISODE_DIGITS
    DEFAULT_INDICATOR_SEASON_DIGITS = DEFAULT_SHOW_INDICATOR_SEASON_DIGITS
    DEFAULT_INDICATOR_EPISODE_DIGITS = DEFAULT_SHOW_INDICATOR_EPISODE_DIGITS

    @classmethod
    def getDefaultDigitLengths(cls, context: dict | None = None) -> dict[str, int]:
        configurationData = {}

        if context is not None:
            configController = context.get('config')
            if configController is not None and hasattr(configController, 'getData'):
                configurationData = configController.getData()

        return {
            cls.INDEX_SEASON_DIGITS_KEY: ConfigurationController.getConfiguredIntegerValue(
                configurationData,
                ConfigurationController.DEFAULT_INDEX_SEASON_DIGITS_CONFIG_KEY,
                cls.DEFAULT_INDEX_SEASON_DIGITS,
            ),
            cls.INDEX_EPISODE_DIGITS_KEY: ConfigurationController.getConfiguredIntegerValue(
                configurationData,
                ConfigurationController.DEFAULT_INDEX_EPISODE_DIGITS_CONFIG_KEY,
                cls.DEFAULT_INDEX_EPISODE_DIGITS,
            ),
            cls.INDICATOR_SEASON_DIGITS_KEY: ConfigurationController.getConfiguredIntegerValue(
                configurationData,
                ConfigurationController.DEFAULT_INDICATOR_SEASON_DIGITS_CONFIG_KEY,
                cls.DEFAULT_INDICATOR_SEASON_DIGITS,
            ),
            cls.INDICATOR_EPISODE_DIGITS_KEY: ConfigurationController.getConfiguredIntegerValue(
                configurationData,
                ConfigurationController.DEFAULT_INDICATOR_EPISODE_DIGITS_CONFIG_KEY,
                cls.DEFAULT_INDICATOR_EPISODE_DIGITS,
            ),
        }


    def __init__(self, **kwargs):

        if ShowDescriptor.CONTEXT_KEY in kwargs.keys():
            if type(kwargs[ShowDescriptor.CONTEXT_KEY]) is not dict:
                raise TypeError(
                    f"ShowDescriptor.__init__(): Argument {ShowDescriptor.CONTEXT_KEY} is required to be of type dict"
                )
            self.__context = kwargs[ShowDescriptor.CONTEXT_KEY]
            self.__logger = self.__context['logger']
        else:
            self.__context = {}
            self.__logger = get_ffx_logger()

        if ShowDescriptor.ID_KEY in kwargs.keys():
                if type(kwargs[ShowDescriptor.ID_KEY]) is not int:
                        raise TypeError(f"ShowDescriptor.__init__(): Argument {ShowDescriptor.ID_KEY} is required to be of type int")
                self.__showId = kwargs[ShowDescriptor.ID_KEY]
        else:
                self.__showId = -1

        if ShowDescriptor.NAME_KEY in kwargs.keys():
                if type(kwargs[ShowDescriptor.NAME_KEY]) is not str:
                        raise TypeError(f"ShowDescriptor.__init__(): Argument {ShowDescriptor.NAME_KEY} is required to be of type str")
                self.__showName = kwargs[ShowDescriptor.NAME_KEY]
        else:
                self.__showName = ''

        if ShowDescriptor.YEAR_KEY in kwargs.keys():
                if type(kwargs[ShowDescriptor.YEAR_KEY]) is not int:
                        raise TypeError(f"ShowDescriptor.__init__(): Argument {ShowDescriptor.YEAR_KEY} is required to be of type int")
                self.__showYear = kwargs[ShowDescriptor.YEAR_KEY]
        else:
            self.__showYear = -1

        defaultDigitLengths = self.getDefaultDigitLengths(self.__context)

        if ShowDescriptor.INDEX_SEASON_DIGITS_KEY in kwargs.keys():
                if type(kwargs[ShowDescriptor.INDEX_SEASON_DIGITS_KEY]) is not int:
                        raise TypeError(f"ShowDescriptor.__init__(): Argument {ShowDescriptor.INDEX_SEASON_DIGITS_KEY} is required to be of type int")
                self.__indexSeasonDigits = kwargs[ShowDescriptor.INDEX_SEASON_DIGITS_KEY]
        else:
                self.__indexSeasonDigits = defaultDigitLengths[ShowDescriptor.INDEX_SEASON_DIGITS_KEY]

        if ShowDescriptor.INDEX_EPISODE_DIGITS_KEY in kwargs.keys():
                if type(kwargs[ShowDescriptor.INDEX_EPISODE_DIGITS_KEY]) is not int:
                        raise TypeError(f"ShowDescriptor.__init__(): Argument {ShowDescriptor.INDEX_EPISODE_DIGITS_KEY} is required to be of type int")
                self.__indexEpisodeDigits = kwargs[ShowDescriptor.INDEX_EPISODE_DIGITS_KEY]
        else:
                self.__indexEpisodeDigits = defaultDigitLengths[ShowDescriptor.INDEX_EPISODE_DIGITS_KEY]

        if ShowDescriptor.INDICATOR_SEASON_DIGITS_KEY in kwargs.keys():
                if type(kwargs[ShowDescriptor.INDICATOR_SEASON_DIGITS_KEY]) is not int:
                        raise TypeError(f"ShowDescriptor.__init__(): Argument {ShowDescriptor.INDICATOR_SEASON_DIGITS_KEY} is required to be of type int")
                self.__indicatorSeasonDigits = kwargs[ShowDescriptor.INDICATOR_SEASON_DIGITS_KEY]
        else:
                self.__indicatorSeasonDigits = defaultDigitLengths[ShowDescriptor.INDICATOR_SEASON_DIGITS_KEY]

        if ShowDescriptor.INDICATOR_EPISODE_DIGITS_KEY in kwargs.keys():
                if type(kwargs[ShowDescriptor.INDICATOR_EPISODE_DIGITS_KEY]) is not int:
                        raise TypeError(f"ShowDescriptor.__init__(): Argument {ShowDescriptor.INDICATOR_EPISODE_DIGITS_KEY} is required to be of type int")
                self.__indicatorEpisodeDigits = kwargs[ShowDescriptor.INDICATOR_EPISODE_DIGITS_KEY]
        else:
                self.__indicatorEpisodeDigits = defaultDigitLengths[ShowDescriptor.INDICATOR_EPISODE_DIGITS_KEY]

        if ShowDescriptor.QUALITY_KEY in kwargs.keys():
                if type(kwargs[ShowDescriptor.QUALITY_KEY]) is not int:
                        raise TypeError(f"ShowDescriptor.__init__(): Argument {ShowDescriptor.QUALITY_KEY} is required to be of type int")
                self.__quality = kwargs[ShowDescriptor.QUALITY_KEY]
        else:
                self.__quality = 0

        if ShowDescriptor.NOTES_KEY in kwargs.keys():
                if type(kwargs[ShowDescriptor.NOTES_KEY]) is not str:
                        raise TypeError(f"ShowDescriptor.__init__(): Argument {ShowDescriptor.NOTES_KEY} is required to be of type str")
                self.__notes = kwargs[ShowDescriptor.NOTES_KEY]
        else:
                self.__notes = ''


    def getId(self):
        return self.__showId
    def getName(self):
        return self.__showName
    def getYear(self):
        return self.__showYear

    def getIndexSeasonDigits(self):
        return self.__indexSeasonDigits
    def getIndexEpisodeDigits(self):
        return self.__indexEpisodeDigits
    def getIndicatorSeasonDigits(self):
        return self.__indicatorSeasonDigits
    def getIndicatorEpisodeDigits(self):
        return self.__indicatorEpisodeDigits
    def getQuality(self):
        return self.__quality
    def getNotes(self):
        return self.__notes

    def getFilenamePrefix(self):
        return f"{self.__showName} ({str(self.__showYear)})"
