import os, sys, importlib, glob, inspect

from ffx.track_disposition import TrackDisposition
from .disposition_combinator_2 import DispositionCombinator2


class DispositionCombinator20(DispositionCombinator2):

    # COMMENT
    # DESCRIPTIONS

    VARIANT = 'D00'

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

        self.__createPresets = createPresets

    def getVariant(self):
        return DispositionCombinator20.VARIANT


    def getPayload(self):

        subtrack0 = set()
        subtrack1 = 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.FORCED) # COMMENT
            # subtrack1.add(TrackDisposition.DESCRIPTIONS) # DESCRIPTIONS

        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'")
                    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.FORCED)
                    ), f"Stream #0 index={trackDescriptors[0].getIndex()} [{trackDescriptors[0].getType().label()}:{trackDescriptors[0].getSubIndex()}] has not preserved set 'forced' disposition"
                    # source subIndex 1
                    assert (not trackDescriptors[1].getDispositionFlag(TrackDisposition.DEFAULT)
                    ), f"Stream #1 index={trackDescriptors[1].getIndex()} [{trackDescriptors[1].getType().label()}:{trackDescriptors[1].getSubIndex()}] has 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 set 'descriptions' 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 (not trackDescriptors[1].getDispositionFlag(TrackDisposition.DEFAULT)
                    ), f"Stream #1 index={trackDescriptors[1].getIndex()} [{trackDescriptors[1].getType().label()}:{trackDescriptors[1].getSubIndex()}] has set default disposition"

        return f


    def shouldFail(self):
        return False
