import os, sys, importlib, glob, inspect

from ffx.track_disposition import TrackDisposition
from .track_tag_combinator_3 import TrackTagCombinator3


class TrackTagCombinator33(TrackTagCombinator3):

    VARIANT = 'T001'


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

        self.__createPresets = createPresets

    def getVariant(self):
        return TrackTagCombinator33.VARIANT


    def getPayload(self):

        subtrack0 = {}
        subtrack1 = {}
        subtrack2 = ({'language': 'tkm', 'title': 'Turkmenistan'}
                        if self.__createPresets else {'language': 'eng', 'title': 'English'})

        if self.__createPresets:
            subtrack0['THIS_IS'] = 'track0'
            subtrack1['THIS_IS'] = 'track1'
            subtrack2['THIS_IS'] = 'track2'

        return (subtrack0,
                subtrack1,
                subtrack2)


    def createAssertFunc(self):

        if self.__createPresets:

            def f(assertObj: dict = {}):
                
                if not 'tracks' in assertObj.keys():
                    raise KeyError("assertObj does not contain key 'tracks'")
                resortedTrackDescriptors = sorted(assertObj['tracks'], key=lambda d: d.getSourceIndex())

                # source subIndex 0
                assert (not 'language' in resortedTrackDescriptors[0].getTags().keys()
                ), f"Stream src_index={resortedTrackDescriptors[0].getSourceIndex()} index={resortedTrackDescriptors[0].getIndex()} [{resortedTrackDescriptors[0].getType().label()}:{resortedTrackDescriptors[0].getSubIndex()}] has set language tag"
                assert (not 'title' in resortedTrackDescriptors[0].getTags().keys()
                ), f"Stream src_index={resortedTrackDescriptors[0].getSourceIndex()} index={resortedTrackDescriptors[0].getIndex()} [{resortedTrackDescriptors[0].getType().label()}:{resortedTrackDescriptors[0].getSubIndex()}] has set title tag"

                assert ('THIS_IS' in resortedTrackDescriptors[0].getTags().keys() and resortedTrackDescriptors[0].getTags()['THIS_IS'] == 'track0'
                ), f"Stream src_index={resortedTrackDescriptors[0].getSourceIndex()} index={resortedTrackDescriptors[0].getIndex()} [{resortedTrackDescriptors[0].getType().label()}:{resortedTrackDescriptors[0].getSubIndex()}] has not preserved tag THIS_IS"

                # source subIndex 1
                assert (not 'language' in resortedTrackDescriptors[1].getTags().keys()
                ), f"Stream src_index={resortedTrackDescriptors[1].getSourceIndex()} index={resortedTrackDescriptors[1].getIndex()} [{resortedTrackDescriptors[1].getType().label()}:{resortedTrackDescriptors[1].getSubIndex()}] has set language tag"
                assert (not 'title' in resortedTrackDescriptors[1].getTags().keys()
                ), f"Stream src_index={resortedTrackDescriptors[1].getSourceIndex()} index={resortedTrackDescriptors[1].getIndex()} [{resortedTrackDescriptors[1].getType().label()}:{resortedTrackDescriptors[1].getSubIndex()}] has set title tag"

                assert ('THIS_IS' in resortedTrackDescriptors[1].getTags().keys() and resortedTrackDescriptors[1].getTags()['THIS_IS'] == 'track1'
                ), f"Stream src_index={resortedTrackDescriptors[1].getSourceIndex()} index={resortedTrackDescriptors[1].getIndex()} [{resortedTrackDescriptors[1].getType().label()}:{resortedTrackDescriptors[1].getSubIndex()}] has not preserved tag THIS_IS"

                # source subIndex 2
                assert ('language' in resortedTrackDescriptors[2].getTags().keys()
                ), f"Stream src_index={resortedTrackDescriptors[2].getSourceIndex()} index={resortedTrackDescriptors[2].getIndex()} [{resortedTrackDescriptors[2].getType().label()}:{resortedTrackDescriptors[0].getSubIndex()}] has not set language tag"
                assert (resortedTrackDescriptors[2].getTags()['language'] == 'eng'
                ), f"Stream src_index={resortedTrackDescriptors[2].getSourceIndex()} index={resortedTrackDescriptors[2].getIndex()} [{resortedTrackDescriptors[2].getType().label()}:{resortedTrackDescriptors[0].getSubIndex()}] has not set language tag 'eng'"
                assert ('title' in resortedTrackDescriptors[2].getTags().keys()
                ), f"Stream src_index={resortedTrackDescriptors[2].getSourceIndex()} index={resortedTrackDescriptors[2].getIndex()} [{resortedTrackDescriptors[2].getType().label()}:{resortedTrackDescriptors[0].getSubIndex()}] has not set title tag"
                assert (resortedTrackDescriptors[2].getTags()['title'] == 'English'
                ), f"Stream src_index={resortedTrackDescriptors[2].getSourceIndex()} index={resortedTrackDescriptors[2].getIndex()} [{resortedTrackDescriptors[2].getType().label()}:{resortedTrackDescriptors[0].getSubIndex()}] has not set title 'English'"

                assert ('THIS_IS' in resortedTrackDescriptors[2].getTags().keys() and resortedTrackDescriptors[2].getTags()['THIS_IS'] == 'track2'
                ), f"Stream src_index={resortedTrackDescriptors[2].getSourceIndex()} index={resortedTrackDescriptors[2].getIndex()} [{resortedTrackDescriptors[1].getType().label()}:{resortedTrackDescriptors[2].getSubIndex()}] has not preserved tag THIS_IS"

        else:

            def f(assertObj: dict = {}):
                
                if not 'tracks' in assertObj.keys():
                    raise KeyError("assertObj does not contain key 'tracks'")
                resortedTrackDescriptors = sorted(assertObj['tracks'], key=lambda d: d.getSourceIndex())

                # source subIndex 0
                assert (not 'language' in resortedTrackDescriptors[0].getTags().keys()
                ), f"Stream src_index={resortedTrackDescriptors[0].getSourceIndex()} index={resortedTrackDescriptors[0].getIndex()} [{resortedTrackDescriptors[0].getType().label()}:{resortedTrackDescriptors[0].getSubIndex()}] has set language tag"
                assert (not 'title' in resortedTrackDescriptors[0].getTags().keys()
                ), f"Stream src_index={resortedTrackDescriptors[0].getSourceIndex()} index={resortedTrackDescriptors[0].getIndex()} [{resortedTrackDescriptors[0].getType().label()}:{resortedTrackDescriptors[0].getSubIndex()}] has set title tag"

                # source subIndex 1
                assert (not 'language' in resortedTrackDescriptors[1].getTags().keys()
                ), f"Stream src_index={resortedTrackDescriptors[1].getSourceIndex()} index={resortedTrackDescriptors[1].getIndex()} [{resortedTrackDescriptors[1].getType().label()}:{resortedTrackDescriptors[1].getSubIndex()}] has set language tag"
                assert (not 'title' in resortedTrackDescriptors[1].getTags().keys()
                ), f"Stream src_index={resortedTrackDescriptors[1].getSourceIndex()} index={resortedTrackDescriptors[1].getIndex()} [{resortedTrackDescriptors[1].getType().label()}:{resortedTrackDescriptors[1].getSubIndex()}] has set title tag"

                # source subIndex 2
                assert ('language' in resortedTrackDescriptors[2].getTags().keys()
                ), f"Stream src_index={resortedTrackDescriptors[2].getSourceIndex()} index={resortedTrackDescriptors[2].getIndex()} [{resortedTrackDescriptors[2].getType().label()}:{resortedTrackDescriptors[0].getSubIndex()}] has not set language tag"
                assert (resortedTrackDescriptors[2].getTags()['language'] == 'eng'
                ), f"Stream src_index={resortedTrackDescriptors[2].getSourceIndex()} index={resortedTrackDescriptors[2].getIndex()} [{resortedTrackDescriptors[2].getType().label()}:{resortedTrackDescriptors[0].getSubIndex()}] has not set language tag 'eng'"
                assert ('title' in resortedTrackDescriptors[2].getTags().keys()
                ), f"Stream src_index={resortedTrackDescriptors[2].getSourceIndex()} index={resortedTrackDescriptors[2].getIndex()} [{resortedTrackDescriptors[2].getType().label()}:{resortedTrackDescriptors[0].getSubIndex()}] has not set title tag"
                assert (resortedTrackDescriptors[2].getTags()['title'] == 'English'
                ), f"Stream src_index={resortedTrackDescriptors[2].getSourceIndex()} index={resortedTrackDescriptors[2].getIndex()} [{resortedTrackDescriptors[2].getType().label()}:{resortedTrackDescriptors[0].getSubIndex()}] has not set title 'English'"

        return f


    def shouldFail(self):
        return False