import itertools

from .filter import Filter


class PresetFilter(Filter):

    IDENTIFIER = 'preset'

    DEFAULT_PRESET = 5

    PRESET_KEY = 'preset'

    def __init__(self, **kwargs):

        self.__presetsList = []
        presets = str(kwargs.get(PresetFilter.PRESET_KEY, ''))
        if presets:
            presetTokens = presets.split(',')
            for q in presetTokens:
                try:
                    presetValue = int(q)
                except:
                    raise ValueError('PresetFilter: Preset value has to be of type int')
                if presetValue < 0 or presetValue > 13:
                    raise ValueError('PresetFilter: Preset value has to be between 0 and 13')
                self.__presetsList.append(presetValue)
        else:
            self.__presetsList = [PresetFilter.DEFAULT_PRESET]

        super().__init__(self)


    def getPayload(self, preset):

        suffices = []

        if len(self.__presetsList) > 1:
            suffices += [f"p{preset}"]

        payload = {'identifier': PresetFilter.IDENTIFIER,
                   'parameters': {
                        'preset': preset
                    },
                    'suffices': suffices,
                    'variant': f"P{preset}",
                    'tokens': []}

        return payload


    def getYield(self):
        for q in self.__presetsList:            
            yield self.getPayload(q)