from typing import Iterator

import flatbuffers
from physiq_cloud.series_frame import fb_builder
from physiq_cloud.series_frame.fb_wrapper import FlatBufferWrapper
from physiq_cloud.time import Instant, TimeUnit
from physiqfb import SeriesFrame

from pojagi_dsp.series_frame import (ASeriesFrameEmitter,
                                     SeriesFramePackage)


class VCIVitalPatchTelemetrySynthesizer(ASeriesFrameEmitter):

    def frames(self) -> Iterator[SeriesFramePackage]:
        builder = flatbuffers.Builder(0)
        target = fb_builder.CreateSeriesFrame(
            builder=builder,
            frame_id=Instant.now().to_unix(
                TimeUnit.MICROSECONDS,
            ) // self.sft_info.frame_size_micros,
            ingested_at_micros=-1,
            sampling_sets_offsets=[
                # replayed_sampling_set(
                #     builder=builder,
                #     sampling_set_data=sampling_set_data,
                #     source_frame_id=source.FrameId(),
                #     target_frame_id=target_frame_id,
                #     frame_size_micros=self.sft_info.frame_size_micros,
                #     is_timestamped=self.timestamped_map[
                #         sampling_set_data.Id()
                #     ],
                # )
                # for sampling_set_data in [
                #     source.SamplingSets(idx)
                #     for idx in range(source.SamplingSetsLength())
                # ]
            ],
            submitter_id=-1,
            sensor_id=-1,
        )
        builder.Finish(target)
        target_frame_bytes = builder.Output()

        return FlatBufferWrapper(
            _bytes=target_frame_bytes,
            schema=SeriesFrame,
        )
