import os, sys, importlib, glob, inspect

from ffx.track_disposition import TrackDisposition
from .track_tag_combinator_3 import TrackTagCombinator3


class TrackTagCombinator34(TrackTagCombinator3):

    VARIANT = 'T111'


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

        self.__createPresets = createPresets

    def getVariant(self):
        return TrackTagCombinator34.VARIANT


    def getPayload(self):

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

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

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

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

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

        return f


    def shouldFail(self):
        return False
