from pathlib import Path

from flandre.utils.RfMeta import RfFrameMeta, RfSequenceMeta
from flandre.utils.RfSequence import RfSequence


class RfFrame:
    def __bytes__(self) -> bytes:
        raise NotImplementedError()


class RfFrameFile(RfFrame):
    def __init__(self, meta: RfFrameMeta, seq: RfSequence, data: bytes = None, filename: str = None):
        self.seq = seq
        self.meta = meta
        self.data = data
        self.filename = filename

    def __bytes__(self):
        if self.data is not None:
            return self.data
        match self.seq.type:
            case 'zip':
                from flandre.utils.archive import zip_to_bytes
                if self.filename is not None:
                    return zip_to_bytes(self.seq.path, int(Path(self.filename).stem))
            case 'dir':
                return (self.seq.path / self.filename).read_bytes()
        raise NotImplementedError()


class RfFrameMemory(RfFrame):
    def __init__(self, meta: RfFrameMeta, seq_meta: RfSequenceMeta, data: bytes):
        self.seq_meta = seq_meta
        self.meta = meta
        self.data = data

    def __bytes__(self):
        return self.data
