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 .disposition_combinator_3 import DispositionCombinator3
from .track_tag_combinator_3 import TrackTagCombinator3
from .permutation_combinator_3 import PermutationCombinator3
from .media_tag_combinator import MediaTagCombinator


class MediaCombinator3(MediaCombinator):

    VARIANT = 'VASSS'

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

        self.__createPresets = createPresets


    def getVariant(self):
        return MediaCombinator3.VARIANT


    def getPayload(self,
                    subtitleDispositionTuple = (set(), set(), set()),
                    subtitleTagTuple = ({}, {}, {})):

        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
        trackDescriptor0 = 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
        trackDescriptor1 = TrackDescriptor(**kwargs)

        kwargs = {}
        kwargs[TrackDescriptor.CONTEXT_KEY] = self._context
        kwargs[TrackDescriptor.INDEX_KEY] = 2
        kwargs[TrackDescriptor.SOURCE_INDEX_KEY] = 2
        kwargs[TrackDescriptor.TRACK_TYPE_KEY] = TrackType.SUBTITLE
        kwargs[TrackDescriptor.SUB_INDEX_KEY] = 0
        kwargs[TrackDescriptor.DISPOSITION_SET_KEY] = subtitleDispositionTuple[0]
        kwargs[TrackDescriptor.TAGS_KEY] = subtitleTagTuple[0]
        trackDescriptor2 = TrackDescriptor(**kwargs)

        kwargs = {}
        kwargs[TrackDescriptor.CONTEXT_KEY] = self._context
        kwargs[TrackDescriptor.INDEX_KEY] = 3
        kwargs[TrackDescriptor.SOURCE_INDEX_KEY] = 3
        kwargs[TrackDescriptor.TRACK_TYPE_KEY] = TrackType.SUBTITLE
        kwargs[TrackDescriptor.SUB_INDEX_KEY] = 1
        kwargs[TrackDescriptor.DISPOSITION_SET_KEY] = subtitleDispositionTuple[1]
        kwargs[TrackDescriptor.TAGS_KEY] = subtitleTagTuple[1]
        trackDescriptor3 = TrackDescriptor(**kwargs)

        kwargs = {}
        kwargs[TrackDescriptor.CONTEXT_KEY] = self._context
        kwargs[TrackDescriptor.INDEX_KEY] = 4
        kwargs[TrackDescriptor.SOURCE_INDEX_KEY] = 4
        kwargs[TrackDescriptor.TRACK_TYPE_KEY] = TrackType.SUBTITLE
        kwargs[TrackDescriptor.SUB_INDEX_KEY] = 2
        kwargs[TrackDescriptor.DISPOSITION_SET_KEY] = subtitleDispositionTuple[2]
        kwargs[TrackDescriptor.TAGS_KEY] = subtitleTagTuple[2]
        trackDescriptor4 = TrackDescriptor(**kwargs)

        kwargs = {}
        kwargs[MediaDescriptor.CONTEXT_KEY] = self._context
        kwargs[MediaDescriptor.TRACK_DESCRIPTOR_LIST_KEY] =  [trackDescriptor0,
                trackDescriptor1,
                trackDescriptor2,
                trackDescriptor3,
                trackDescriptor4]

        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():
            for DC3 in DispositionCombinator3.getAllClassReferences():
                for TC3 in TrackTagCombinator3.getAllClassReferences():

                        dc3 = DC3(self._context)
                        tc3 = TC3(self._context)

                        mtc = MTC(self._context)

                        yObj = {}

                        yObj['identifier'] = self.getIdentifier()
                        yObj['variants'] = [self.getVariant(),
                                                f"S:{dc3.getVariant()}",
                                                f"S:{tc3.getVariant()}",
                                                mtc.getVariant()]

                        yObj['payload'] = self.getPayload(dc3.getPayload(),
                                                            tc3.getPayload())

                        yObj['assertSelectors'] = ['M', 'SD', 'ST', 'MT']
                        yObj['assertFuncs'] = [self.assertFunc,
                                                    dc3.createAssertFunc(),
                                                    tc3.createAssertFunc(),
                                                    mtc.createAssertFunc()]

                        yObj['shouldFail'] = (self.shouldFail()
                                                    | dc3.shouldFail()
                                                    | tc3.shouldFail()
                                                    | mtc.shouldFail())
                        yieldObj = {'target': yObj}

                        if self.__createPresets:

                            dc3_p = DC3(self._context, createPresets = True)
                            tc3_p = TC3(self._context, createPresets = True)

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

                            yObj_p = {}

                            yObj_p['identifier'] = self.getIdentifier()
                            yObj_p['variants'] = [self.getVariant(),
                                                    f"S:{dc3_p.getVariant()}",
                                                    f"S:{tc3_p.getVariant()}",
                                                    mtc_p.getVariant()]

                            yObj_p['payload'] = self.getPayload(dc3_p.getPayload(),
                                                                tc3_p.getPayload())

                            yObj_p['assertSelectors'] = ['M', 'SD', 'ST', 'MT']
                            yObj_p['assertFuncs'] = [self.assertFunc,
                                                        dc3_p.createAssertFunc(),
                                                        tc3_p.createAssertFunc(),
                                                        mtc_p.createAssertFunc()]

                            yObj_p['shouldFail'] = (self.shouldFail()
                                                        | dc3_p.shouldFail()
                                                        | tc3_p.shouldFail()
                                                        | mtc_p.shouldFail())
                            yieldObj['preset'] = yObj_p

                        yield yieldObj
