# Path: ebu_tt_live/clocks/local.py
# class LocalMachineClock(Clock):
# 
#     _time_base = 'clock'
#     _clock_mode = 'local'
# 
#     def get_real_clock_time(self):
#         now = datetime.now().time()
#         return timedelta(hours=now.hour, minutes=now.minute, seconds=now.second, microseconds=now.microsecond)
# 
# Path: ebu_tt_live/bindings/_ebuttdt.py
# class LimitedClockTimingType(_TimedeltaBindingMixin, ebuttdt_raw.limitedClockTimingType):
#     """
#     Extending the string type with conversions to and from timedelta
#     """
# 
#     _compatible_timebases = {
#         'begin': ['clock'],
#         'dur': ['clock'],
#         'end': ['clock']
#     }
#     _groups_regex = re.compile('([0-9][0-9]):([0-5][0-9]):([0-5][0-9]|60)(?:\.([0-9]+))?')
# 
#     @classmethod
#     def as_timedelta(cls, instance):
#         """
#         Using regex parse value and create timedelta
#         :param instance:
#         :return:
#         """
#         hours_str, minutes_str, seconds_str, seconds_fraction_str = [x for x in cls._groups_regex.match(instance).groups()]
#         milliseconds = seconds_fraction_str and float('0.' + seconds_fraction_str) * 1000 or 0
#         return timedelta(hours=int(hours_str), 
#             minutes=int(minutes_str), 
#             seconds=int(seconds_str), 
#             milliseconds=milliseconds)
#         
# 
#     @classmethod
#     def from_timedelta(cls, instance):
#         """
#         Generate limited clock type from timedelta
#         :param instance:
#         :return:
#         """
#         hours, minutes, seconds, milliseconds = _get_time_members(instance)
#         # We have our most significant value. Time for range check
#         if hours > 99:
#             raise TimeFormatOverflowError(ERR_TIME_FORMAT_OVERFLOW)
#         if milliseconds:
#             return '{hours:02d}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}'.format(
#                 hours=hours,
#                 minutes=minutes,
#                 seconds=seconds,
#                 milliseconds=milliseconds
#             )
#         else:
#             return '{hours:02d}:{minutes:02d}:{seconds:02d}'.format(
#                 hours=hours,
#                 minutes=minutes,
#                 seconds=seconds
#             )
# 
# Path: ebu_tt_live/adapters/document_data.py
# class XMLtoEBUTT3Adapter(IDocumentDataAdapter):
# class XMLtoEBUTTDAdapter(IDocumentDataAdapter):
# class EBUTTDtoXMLAdapter(IDocumentDataAdapter):
# class EBUTT3toXMLAdapter(IDocumentDataAdapter):
# class EBUTT3toEBUTTDAdapter(IDocumentDataAdapter):
#     def convert_data(self, data, availability_time=None, sequence_identifier=None, **kwargs):
#     def convert_data(self, data, **kwargs):
#     def convert_data(self, data, **kwargs):
#     def convert_data(self, data, **kwargs):
#     def __init__(self, media_clock=None):
#     def convert_data(self, data, **kwargs):
# def get_document_data_adapter(expects, provides):


from ebu_tt_live.node.delay import BufferDelayNode
from ebu_tt_live.clocks.local import LocalMachineClock
from ebu_tt_live.bindings._ebuttdt import LimitedClockTimingType
from ebu_tt_live.carriage.filesystem import FilesystemProducerImpl
from ebu_tt_live.adapters.node_carriage import ProducerNodeCarriageAdapter
from ebu_tt_live.adapters import document_data
from pytest_bdd import scenarios, given, when, then
from pytest import fixture
from tempfile import mkdtemp
from shutil import rmtree
import os
    test_context['doc'] = gen_document


@given('the document is emitted')
def given_document_emitted(test_context, temp_dir):

    # read the availability time from the manifest file stored in initial/tmp
    manifest_path = os.path.join(temp_dir, 'initial', 'manifest_delayTest.txt')
    avail_time = ''

    with open(manifest_path, 'r') as f:
        for last_line in f:
            avail_time, _ = last_line.split(',')

    test_context['doc'].avail_time = avail_time

    # read the emission time from the manifest file stored in buffer/tmp
    manifest_path = os.path.join(temp_dir, 'buffer', 'manifest_delayTest.txt')
    emission_time = ''

    with open(manifest_path, 'r') as f:
        for last_line in f:
            emission_time, _ = last_line.split(',')

    test_context['doc'].emission = emission_time


@then('the delta between emission and availability time is greater or equal to <delay_offset>')
def then_delta_should_be_correct(delay_offset, test_context):


# Path: ebu_tt_live/clocks/local.py
# class LocalMachineClock(Clock):
# 
#     _time_base = 'clock'
#     _clock_mode = 'local'
# 
#     def get_real_clock_time(self):
#         now = datetime.now().time()
#         return timedelta(hours=now.hour, minutes=now.minute, seconds=now.second, microseconds=now.microsecond)
# 
# Path: ebu_tt_live/bindings/_ebuttdt.py
# class LimitedClockTimingType(_TimedeltaBindingMixin, ebuttdt_raw.limitedClockTimingType):
#     """
#     Extending the string type with conversions to and from timedelta
#     """
# 
#     _compatible_timebases = {
#         'begin': ['clock'],
#         'dur': ['clock'],
#         'end': ['clock']
#     }
#     _groups_regex = re.compile('([0-9][0-9]):([0-5][0-9]):([0-5][0-9]|60)(?:\.([0-9]+))?')
# 
#     @classmethod
#     def as_timedelta(cls, instance):
#         """
#         Using regex parse value and create timedelta
#         :param instance:
#         :return:
#         """
#         hours_str, minutes_str, seconds_str, seconds_fraction_str = [x for x in cls._groups_regex.match(instance).groups()]
#         milliseconds = seconds_fraction_str and float('0.' + seconds_fraction_str) * 1000 or 0
#         return timedelta(hours=int(hours_str), 
#             minutes=int(minutes_str), 
#             seconds=int(seconds_str), 
#             milliseconds=milliseconds)
#         
# 
#     @classmethod
#     def from_timedelta(cls, instance):
#         """
#         Generate limited clock type from timedelta
#         :param instance:
#         :return:
#         """
#         hours, minutes, seconds, milliseconds = _get_time_members(instance)
#         # We have our most significant value. Time for range check
#         if hours > 99:
#             raise TimeFormatOverflowError(ERR_TIME_FORMAT_OVERFLOW)
#         if milliseconds:
#             return '{hours:02d}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}'.format(
#                 hours=hours,
#                 minutes=minutes,
#                 seconds=seconds,
#                 milliseconds=milliseconds
#             )
#         else:
#             return '{hours:02d}:{minutes:02d}:{seconds:02d}'.format(
#                 hours=hours,
#                 minutes=minutes,
#                 seconds=seconds
#             )
# 
# Path: ebu_tt_live/adapters/document_data.py
# class XMLtoEBUTT3Adapter(IDocumentDataAdapter):
# class XMLtoEBUTTDAdapter(IDocumentDataAdapter):
# class EBUTTDtoXMLAdapter(IDocumentDataAdapter):
# class EBUTT3toXMLAdapter(IDocumentDataAdapter):
# class EBUTT3toEBUTTDAdapter(IDocumentDataAdapter):
#     def convert_data(self, data, availability_time=None, sequence_identifier=None, **kwargs):
#     def convert_data(self, data, **kwargs):
#     def convert_data(self, data, **kwargs):
#     def convert_data(self, data, **kwargs):
#     def __init__(self, media_clock=None):
#     def convert_data(self, data, **kwargs):
# def get_document_data_adapter(expects, provides):


# Path: testing/bdd/test_bufferDelayNode.py
from ebu_tt_live.node.delay import BufferDelayNode
from ebu_tt_live.clocks.local import LocalMachineClock
from ebu_tt_live.bindings._ebuttdt import LimitedClockTimingType
from ebu_tt_live.carriage.filesystem import FilesystemProducerImpl
from ebu_tt_live.adapters.node_carriage import ProducerNodeCarriageAdapter
from ebu_tt_live.adapters import document_data
from pytest_bdd import scenarios, given, when, then
from pytest import fixture
from tempfile import mkdtemp
from shutil import rmtree
import os

    test_context['doc'] = gen_document


@given('the document is emitted')
def given_document_emitted(test_context, temp_dir):

    # read the availability time from the manifest file stored in initial/tmp
    manifest_path = os.path.join(temp_dir, 'initial', 'manifest_delayTest.txt')
    avail_time = ''

    with open(manifest_path, 'r') as f:
        for last_line in f:
            avail_time, _ = last_line.split(',')

    test_context['doc'].avail_time = avail_time

    # read the emission time from the manifest file stored in buffer/tmp
    manifest_path = os.path.join(temp_dir, 'buffer', 'manifest_delayTest.txt')
    emission_time = ''

    with open(manifest_path, 'r') as f:
        for last_line in f:
            emission_time, _ = last_line.split(',')

    test_context['doc'].emission = emission_time


@then('the delta between emission and availability time is greater or equal to <delay_offset>')
def then_delta_should_be_correct(delay_offset, test_context):


    delay_timedelta = LimitedClockTimingType(delay_offset).timedelta