import os, sys, importlib, glob, inspect

from ffx.track_disposition import TrackDisposition
from .track_tag_combinator_2 import TrackTagCombinator2


class TrackTagCombinator23(TrackTagCombinator2):

    VARIANT = 'T11'


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

        self.__createPresets = createPresets


    def getVariant(self):
        return TrackTagCombinator23.VARIANT


    def getPayload(self):

        subTrack0 = ({'language': 'chn', 'title': 'China'}
                        if self.__createPresets else {'language': 'deu', 'title': 'Deutsch'})
        subTrack1 = {'language': 'jpn', 'title': 'Japanisch'}

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

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

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

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

        return f


    def shouldFail(self):
        return False
