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

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_2 import DispositionCombinator2
from .track_tag_combinator_2 import TrackTagCombinator2
from .permutation_combinator_2 import PermutationCombinator2
from .media_tag_combinator import MediaTagCombinator


class MediaCombinator2(MediaCombinator):

    VARIANT = 'VASS'


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

        self.__createPresets = createPresets

    def getVariant(self):
        return MediaCombinator2.VARIANT


    def getPayload(self,
                    subtitleDispositionTuple = (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[MediaDescriptor.CONTEXT_KEY] = self._context
        kwargs[MediaDescriptor.TRACK_DESCRIPTOR_LIST_KEY] =  [trackDescriptor0,
                trackDescriptor1,
                trackDescriptor2,
                trackDescriptor3]

        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 DC2 in DispositionCombinator2.getAllClassReferences():
                for TC2 in TrackTagCombinator2.getAllClassReferences():

                    dc2 = DC2(self._context)
                    tc2 = TC2(self._context)

                    mtc = MTC(self._context)

                    yObj = {}

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

                    yObj['payload'] = self.getPayload(dc2.getPayload(),
                                                        tc2.getPayload())

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

                    yObj['shouldFail'] = (self.shouldFail()
                                                | dc2.shouldFail()
                                                | tc2.shouldFail()
                                                | mtc.shouldFail())

                    yieldObj = {'target': yObj}

                    if self.__createPresets:

                        dc2_p = DC2(self._context, createPresets = True)
                        tc2_p = TC2(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:{dc2_p.getVariant()}",
                                                f"S:{tc2_p.getVariant()}",
                                                mtc_p.getVariant()]

                        yObj_p['payload'] = self.getPayload(dc2_p.getPayload(),
                                                            tc2_p.getPayload())

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

                        yObj_p['shouldFail'] = (self.shouldFail()
                                                    | dc2_p.shouldFail()
                                                    | tc2_p.shouldFail()
                                                    | mtc_p.shouldFail())

                        yieldObj['preset'] = yObj_p

                    yield yieldObj
