# Copyright 2025 Digitalwerk GmbH.
#
#     This Source Code Form is subject to the terms of the Mozilla
#     Public License, v. 2.0. If a copy of the MPL was not distributed
#     with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# If it is not possible or desirable to put the notice in a particular file, then
# You may include the notice in a location (such as a LICENSE file in a
# relevant directory) where a recipient would be likely to look for such a notice.
#
# You may add additional accurate notices of copyright ownership.

import adtf_file

print('exporter module imported')

sample_decoder = {}

def open(streams, destination_url):
    global sample_decoder

    for stream in streams:
        print(f'stream: {stream.name} -> {stream.stream_id}')
        sample_decoder[stream.stream_id] = adtf_file.SampleDecoder(stream.initial_type)

def process(item):
    global sample_decoder

    print(f'process: {item.type}')

    if item.type == adtf_file.ItemType.StreamType:
        sample_decoder[item.stream_id] = adtf_file.SampleDecoder(item.stream_type)

    if item.type == adtf_file.ItemType.Sample:
        sample = item.sample
        print(f'    sample:')
        print(f'      time_stamp: {sample.time_stamp}')
        print(f'      stream id: {item.stream_id}')
        print(f'      substream_id: {sample.substream_id}')
        print(f'      flags: {sample.flags}')
        print(f'      data: {sample_decoder.get(item.stream_id).decode(sample)}')
