import os, sys, importlib, glob, inspect, itertools

from ffx.track_type import TrackType

from ffx.track_descriptor import TrackDescriptor
from ffx.media_descriptor import MediaDescriptor

from .media_combinator import MediaCombinator
from .media_tag_combinator import MediaTagCombinator


class MediaCombinator0(MediaCombinator):

    VARIANT = 'VA'

    def __init__(self, context = None,
                    createPresets: bool = False):
        super().__init__(context)

        self.__createPresets = createPresets


    def getVariant(self):
        return MediaCombinator0.VARIANT



    def getPayload(self):

        kwargs = {}
        kwargs[TrackDescriptor.CONTEXT_KEY] = self._context
        kwargs[TrackDescriptor.INDEX_KEY] = 0
        kwargs[TrackDescriptor.SOURCE_INDEX_KEY] = 0
        kwargs[TrackDescriptor.TRACK_TYPE_KEY] = TrackType.VIDEO
        kwargs[TrackDescriptor.SUB_INDEX_KEY] = 0
        trackDescriptor1 = TrackDescriptor(**kwargs)

        kwargs = {}
        kwargs[TrackDescriptor.CONTEXT_KEY] = self._context
        kwargs[TrackDescriptor.INDEX_KEY] = 1
        kwargs[TrackDescriptor.SOURCE_INDEX_KEY] = 1
        kwargs[TrackDescriptor.TRACK_TYPE_KEY] = TrackType.AUDIO
        kwargs[TrackDescriptor.SUB_INDEX_KEY] = 0
        trackDescriptor2 = TrackDescriptor(**kwargs)

        kwargs = {}
        kwargs[MediaDescriptor.CONTEXT_KEY] = self._context
        kwargs[MediaDescriptor.TRACK_DESCRIPTOR_LIST_KEY] = [trackDescriptor1,
                                                                trackDescriptor2]
        mediaDescriptor = MediaDescriptor(**kwargs)
        # mediaDescriptor.reindexSubIndices()

        return mediaDescriptor


    def assertFunc(self, testObj = {}):
        pass

    def shouldFail(self):
        return False

    def getYield(self):

        for MTC in MediaTagCombinator.getAllClassReferences():

            mtc = MTC(self._context)

            yObj = {}

            yObj['identifier'] = self.getIdentifier()
            yObj['variants'] = [self.getVariant(),
                                    mtc.getVariant()]
            yObj['payload'] = self.getPayload()

            yObj['assertSelectors'] = ['M', 'MT']
            yObj['assertFuncs'] = [self.assertFunc, 
                                        mtc.createAssertFunc()]
            yObj['shouldFail'] = (self.shouldFail()
                                        | mtc.shouldFail())

            yieldObj = {'target': yObj}

            if self.__createPresets:

                mtc_p = MTC(self._context, createPresets = True)

                yObj_p = {}

                yObj_p['identifier'] = self.getIdentifier()
                yObj_p['variants'] = [self.getVariant(),
                                        mtc_p.getVariant()]

                yObj_p['payload'] = self.getPayload()

                yObj_p['assertSelectors'] = ['M', 'MT']
                yObj_p['assertFuncs'] = [self.assertFunc, 
                                            mtc_p.createAssertFunc()]
                yObj_p['shouldFail'] = (self.shouldFail()
                                            | mtc_p.shouldFail())

                yieldObj['preset'] = yObj_p

            yield yieldObj
