import os, sys, importlib, glob, inspect

from ffx.track_disposition import TrackDisposition
from .track_tag_combinator_2 import TrackTagCombinator2


class TrackTagCombinator21(TrackTagCombinator2):

    VARIANT = 'T01'


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

        self.__createPresets = createPresets

    def getVariant(self):
        return TrackTagCombinator21.VARIANT


    def getPayload(self):

        subTrack0 = {}
        subTrack1 = ({'language': 'chn', 'title': 'China'}
                        if self.__createPresets else {'language': 'bas', 'title': 'Baskisch'})

        if self.__createPresets:
            subTrack0['THIS_IS'] = 'track0'
            subTrack1['THIS_IS'] = 'track1'

        return (subTrack0,
                subTrack1)
    

    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 ('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 not set language tag"
                assert (resortedTrackDescriptors[1].getTags()['language'] == 'bas'
                ), f"Stream src_index={resortedTrackDescriptors[1].getSourceIndex()} index={resortedTrackDescriptors[1].getIndex()} [{resortedTrackDescriptors[1].getType().label()}:{resortedTrackDescriptors[1].getSubIndex()}] has not set language tag 'bas'"
                assert ('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 not set title tag"
                assert (resortedTrackDescriptors[1].getTags()['title'] == 'Baskisch'
                ), f"Stream src_index={resortedTrackDescriptors[1].getSourceIndex()} index={resortedTrackDescriptors[1].getIndex()} [{resortedTrackDescriptors[1].getType().label()}:{resortedTrackDescriptors[1].getSubIndex()}] has not set title 'Baskisch'"

                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"

        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 ('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 not set language tag"
                assert (resortedTrackDescriptors[1].getTags()['language'] == 'bas'
                ), f"Stream src_index={resortedTrackDescriptors[1].getSourceIndex()} index={resortedTrackDescriptors[1].getIndex()} [{resortedTrackDescriptors[1].getType().label()}:{resortedTrackDescriptors[1].getSubIndex()}] has not set language tag 'bas'"
                assert ('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 not set title tag"
                assert (resortedTrackDescriptors[1].getTags()['title'] == 'Baskisch'
                ), f"Stream src_index={resortedTrackDescriptors[1].getSourceIndex()} index={resortedTrackDescriptors[1].getIndex()} [{resortedTrackDescriptors[1].getType().label()}:{resortedTrackDescriptors[1].getSubIndex()}] has not set title 'Baskisch'"

        return f


    def shouldFail(self):
        return False
    
