# fieldz/tfbuffer.py

import ctypes
import sys

import wireops.chan
from wireops.enum import FieldTypes, PrimTypes

from wireops.raw import(
    read_field_hdr,
    # read_raw_varint, write_raw_varint,
    read_raw_b32,           # write_b32_field,
    read_raw_b64,           # write_b64_field,
    read_raw_len_plus,      # write_len_plus_field,
    read_raw_b128,          # write_b128_field,
    read_raw_b160,          # write_b160_field,
    read_raw_b256,          # write_b256_field,
)

from wireops.typed import T_PUT_FUNCS, T_GET_FUNCS  # , T_LEN_FUNCS

from fieldz import FieldzError
from fieldz.msg_spec import MsgSpec

__all__ = [\
    # value uncertain
    'TFBuffer', 'TFReader', 'TFWriter',
]

# -- CLASSES --------------------------------------------------------


class TFBuffer(wireops.chan.Channel):

    def __init__(self, msg_spec, nnn=1024, buffer=None):
        super(TFBuffer, self).__init__(nnn, buffer)
        if msg_spec is None:
            raise FieldzError('no msgSpec')
        if not isinstance(msg_spec, MsgSpec):
            raise FieldzError('object is not a MsgSpec')
        self._msg_spec = msg_spec

    @classmethod
    def create(cls, msg_spec, nnn):
        if nnn <= 0:
            raise FieldzError("buffer size must be a positive number")
        buffer = bytearray(nnn)
        return cls(msg_spec, nnn, buffer)


class TFReader(TFBuffer):