import os, sys, importlib, glob, inspect

from ffx.track_disposition import TrackDisposition
from .disposition_combinator_3 import DispositionCombinator3


class DispositionCombinator32(DispositionCombinator3):

    VARIANT = 'D010'


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

        self.__createPresets = createPresets

    def getVariant(self):
        return DispositionCombinator32.VARIANT


    def getPayload(self):

        if self.__createPresets:
            subtrack0 = set([TrackDisposition.DEFAULT])
            subtrack1 = set()
            subtrack2 = set()
        else:
            subtrack0 = set()
            subtrack1 = set([TrackDisposition.DEFAULT])
            subtrack2 = set()

        #NOTE: Current ffmpeg version will not set most of the dispositions on arbitrary tracks
        # so some checks for preserved dispositions are omitted for now
        if self.__createPresets:
            # subtrack0.add(TrackDisposition.COMMENT) # COMMENT
            # subtrack1.add(TrackDisposition.DESCRIPTIONS) # DESCRIPTIONS
            subtrack2.add(TrackDisposition.FORCED) # HEARING_IMPAIRED

        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'")
                    trackDescriptors = assertObj['tracks']

                    # source subIndex 0
                    assert (not trackDescriptors[0].getDispositionFlag(TrackDisposition.DEFAULT)
                    ), f"Stream #0 index={trackDescriptors[0].getIndex()} [{trackDescriptors[0].getType().label()}:{trackDescriptors[0].getSubIndex()}] has set default disposition"
                    # assert (trackDescriptors[0].getDispositionFlag(TrackDisposition.COMMENT)
                    # ), f"Stream #0 index={trackDescriptors[0].getIndex()} [{trackDescriptors[0].getType().label()}:{trackDescriptors[0].getSubIndex()}] has not preserved set default disposition"
                    # source subIndex 1
                    assert (trackDescriptors[1].getDispositionFlag(TrackDisposition.DEFAULT)
                    ), f"Stream #1 index={trackDescriptors[1].getIndex()} [{trackDescriptors[1].getType().label()}:{trackDescriptors[1].getSubIndex()}] has not set default disposition"
                    # assert (trackDescriptors[1].getDispositionFlag(TrackDisposition.DESCRIPTIONS)
                    # ), f"Stream #1 index={trackDescriptors[1].getIndex()} [{trackDescriptors[1].getType().label()}:{trackDescriptors[1].getSubIndex()}] has not preserved descriptions disposition"
                    # source subIndex 2
                    assert (not trackDescriptors[2].getDispositionFlag(TrackDisposition.DEFAULT)
                    ), f"Stream #2 index={trackDescriptors[2].getIndex()} [{trackDescriptors[2].getType().label()}:{trackDescriptors[2].getSubIndex()}] has set default disposition"
                    assert (trackDescriptors[2].getDispositionFlag(TrackDisposition.FORCED)
                    ), f"Stream #2 index={trackDescriptors[2].getIndex()} [{trackDescriptors[2].getType().label()}:{trackDescriptors[2].getSubIndex()}] has not preserved default disposition"

        else:

            def f(assertObj: dict):
                    if not 'tracks' in assertObj.keys():
                        raise KeyError("assertObj does not contain key 'tracks'")
                    trackDescriptors = assertObj['tracks']

                    # source subIndex 0
                    assert (not trackDescriptors[0].getDispositionFlag(TrackDisposition.DEFAULT)
                    ), f"Stream #0 index={trackDescriptors[0].getIndex()} [{trackDescriptors[0].getType().label()}:{trackDescriptors[0].getSubIndex()}] has set default disposition"
                    # source subIndex 1
                    assert (trackDescriptors[1].getDispositionFlag(TrackDisposition.DEFAULT)
                    ), f"Stream #1 index={trackDescriptors[1].getIndex()} [{trackDescriptors[1].getType().label()}:{trackDescriptors[1].getSubIndex()}] has not set default disposition"
                    # source subIndex 2
                    assert (not trackDescriptors[2].getDispositionFlag(TrackDisposition.DEFAULT)
                    ), f"Stream #2 index={trackDescriptors[2].getIndex()} [{trackDescriptors[2].getType().label()}:{trackDescriptors[2].getSubIndex()}] has set default disposition"
        return f

    def shouldFail(self):
        return False
