// Copyright 2026 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
/// `BinaryReaderImpl` contains the low-level operations used by the default
/// implementations of `BinaryReader` methods.
/// Methods in `BinaryReaderImpl` should not be invoked by users directly in general,
/// and implementers should not expose `BinaryReaderImpl` methods via `extend`.
/// In fact, it is recommended to keep `impl BinaryReaderImpl` private.
///
/// Every reader implementation should store a `BinaryReaderController`
/// for buffering, and `get_controller` should return that controller.
///
/// `raw_read_impl` is the actual read implementation,
/// it should ignore the controller and communicate with the underlying reader object directly,
/// reading up to `max_len` bytes into `buf[offset:]`.
/// `raw_read_impl` should return the number of bytes actually read into `buf`.
/// `raw_read_impl` may read less than `max_len` bytes,
/// but it should always read at least one byte on success,
/// and a zero return value should always indicate EOF.
/// `raw_read_impl` should return as soon as possible after at least one byte of data is read.
/// It must not raise `UnexpectedEof` to indicate its own EOF; return zero instead.
/// Any error raised by `raw_read_impl`, including errors propagated from an
/// inner reader, is wrapped in `TransportError` by the `BinaryReader` layer.
pub(open) trait BinaryReaderImpl {
  /// Return a short name identifying this reader in error context.
  fn name(Self) -> String
  fn get_controller(Self) -> BinaryReaderController
  async fn raw_read_impl(
    Self,
    buf : FixedArray[Byte],
    offset~ : Int32,
    max_len~ : Int32,
  ) -> Int32
}

///|
/// Trait for an asynchronous binary input stream.
/// This trait is intended for direct use by end users.
/// All methods in this trait have corresponding default implementations.
/// Custom readers should implement `BinaryReaderImpl` and explicitly implement
/// `BinaryReader` to opt in to these defaults.
/// Methods in `BinaryReader` should not be overridden in general.
/// When overridden for a reader-specific optimization,
/// the alternative implementation should respect behavior of the default implementation.
pub(open) trait BinaryReader: BinaryReaderImpl {
  async fn read_into(Self, MutArrayView[Byte]) -> Int32 raise ReaderError = _
  async fn read(Self) -> Bytes? raise ReaderError = _
  async fn read_at_least(Self, Int32) -> Bytes raise ReaderError = _
  async fn peek(Self) -> BytesView? raise ReaderError = _
  async fn peek_at_least(Self, Int32) -> BytesView raise ReaderError = _
  async fn read_exactly(Self, len : Int32) -> Bytes raise ReaderError = _
  async fn drop_exactly(Self, len : Int32) -> Unit raise ReaderError = _
  async fn peek_exactly(Self, len : Int32) -> BytesView raise ReaderError = _
  async fn read_all(Self) -> Bytes raise ReaderError = _
  async fn drop_all(Self) -> Int64 raise ReaderError = _
  async fn read_until(Self, pattern : BytesView, include_delimiter? : Bool) -> Bytes? raise ReaderError = _
  async fn peek_until(Self, pattern : BytesView, include_delimiter? : Bool) -> BytesView? raise ReaderError = _
}

///|
/// Error raised by `@io.BinaryReader` API
#derive(@debug.Debug, @json.ToJson)
#non_exhaustive
pub(all) suberror ReaderError {
  /// Error raised by binary reader APIs when the binary stream reaches EOF
  /// before enough data can be fetched according to user request
  UnexpectedEof
  /// Raised when the amount of data to be returned in some reader API exceeds limit.
  /// The limit can be either explicit user request,
  /// or implicit from length restriction of `Bytes` etc.
  LengthLimitExceeded
  /// Raised when `@io.TextReader` fail to decode binary data into text
  MalformedText
  /// When using `@io.BinaryReader` API, error raised by the underlying reader implementation
  /// will be wrapped in `TransportError`, so that callers can classify the source of error
  /// for reader transformers.
  ///
  /// The positional parameter of `TransportError` is the actual error,
  /// and `context` is a simple text description of the reader.
  TransportError(Error, context~ : String)
}

///|
fn MutArrayView::buf(self : MutArrayView[Byte]) -> FixedArray[Byte] = "%arrayview.buf"

///|
/// `reader.read_into(dst)` reads at most `dst.length()` bytes from the reader
/// into `dst`.
/// The number of bytes actually read is returned.
/// Note that the return value may be smaller than `dst.length()`
/// if not enough data is *immediately* available.
/// If EOF is reached and no more data is available, zero will be returned.
impl BinaryReader with fn read_into(self, dst) {
  let buf = dst.buf()
  let offset = dst.start_offset()
  let max_len = dst.length()
  guard max_len > 0 else {
    abort("`BinaryReader::read_into()`: `dst` must not be empty")
  }
  let controller = BinaryReaderImpl::get_controller(self)
  if controller.buf_len > 0 {
    controller.blit_to(buf, offset~, max_len~)
  } else {
    BinaryReaderImpl::raw_read_impl(self, buf, offset~, max_len~) catch {
      err => raise TransportError(err, context=BinaryReaderImpl::name(self))
    }
  }
}

///|
/// `reader.read()` fetches and returns a chunk of data from the reader.
/// It will return as fast as possible when new data become available.
///
/// The returned chunk will contain at least one byte.
/// If EOF is reached before any data is fetched, `None` will be returned.
///
/// `reader.read()` is cancellation safe as long as
/// the underlying reader implementation is cancellation safe.
/// When `reader.read()` is cancelled, no data will be lost.
impl BinaryReader with fn read(self) {
  let controller = BinaryReaderImpl::get_controller(self)
  if self.peek() is Some(data) {
    let data = data.to_owned()
    controller.drop(data.length())
    Some(data)
  } else {
    None
  }
}

///|
/// `reader.read_at_least(min_len)` fetches and returns an owned chunk containing
/// at least `min_len` bytes. The returned chunk may be larger than `min_len`.
///
/// If EOF is reached before enough data is fetched, `UnexpectedEof` will be raised.
/// Any shorter trailing data remains buffered and can be fetched through another
/// reader operation later.
///
/// `reader.read_at_least()` is cancellation safe as long as the underlying
/// reader implementation is cancellation safe. When it is cancelled, no data
/// will be lost.
impl BinaryReader with fn read_at_least(self, min_len : Int32) {
  let controller = BinaryReaderImpl::get_controller(self)
  let data = self.peek_at_least(min_len)
  let data = data.to_owned()
  controller.drop(data.length())
  data
}

///|
/// `reader.peek()` fetches and returns a chunk of data from the reader.
/// The returned data will not get consumed from the reader:
/// other reader API can still fetch them later.
///
/// The chunk returned by `reader.peek()` is a copy-free slice
/// of the reader's internal buffer, and its content will be overwritten
/// by subsequent reader operations. So caller of `peek` MUST NOT use the returned slice
/// after any other `BinaryReader` operation is invoked on the same reader.
/// If the data need to be kept persistent, copy it via `.to_owned()`.
///
/// The returned chunk will contain at least one byte.
/// If EOF is reached before any data is fetched, `None` will be returned.
///
/// `reader.peek()` is cancellation safe as long as
/// the underlying reader implementation is cancellation safe.
/// When `reader.peek()` is cancelled, no data will be lost.
impl BinaryReader with fn peek(self) {
  try self.peek_at_least(1) catch {
    UnexpectedEof => None
    err => raise err
  } noraise {
    data => Some(data)
  }
}

///|
/// `reader.peek_at_least(min_len)` fetches and returns a chunk containing at
/// least `min_len` bytes without consuming it. The returned chunk may be larger
/// than `min_len`.
///
/// The returned view is valid only until another operation is invoked on the
/// same reader. Use `.to_owned()` to keep the data persistently.
///
/// If EOF is reached before enough data is fetched, `UnexpectedEof` will be
/// raised. Any shorter trailing data remains buffered and can be fetched by a
/// later reader operation.
///
/// `reader.peek_at_least()` is cancellation safe as long as the underlying
/// reader implementation is cancellation safe. When it is cancelled, no data
/// will be lost.
impl BinaryReader with fn peek_at_least(self, min_len : Int32) {
  let controller = BinaryReaderImpl::get_controller(self)
  controller.enlarge_to(min_len)
  while controller.buf_len < min_len {
    let offset = controller.buf_start + controller.buf_len
    let n = BinaryReaderImpl::raw_read_impl(
      self,
      controller.buf,
      offset~,
      max_len=controller.buf.length() - offset,
    ) catch {
      err => raise TransportError(err, context=BinaryReaderImpl::name(self))
    }
    if n is 0 {
      raise UnexpectedEof
    }
    controller.buf_len += n
  }
  controller.buffered()
}

///|
/// `reader.read_exactly(len)` reads exactly `len` bytes from the reader.
/// It will wait until enough number of bytes is available.
/// If EOF is reached before enough data is read, `UnexpectedEof` will be raised.
impl BinaryReader with fn read_exactly(self, len) {
  let buf = FixedArray::make(len, b'\x00')
  for received = 0; received < len; {
    let new_received = self.read_into(buf.mut_view(start=received, end=len))
    if new_received is 0 {
      raise UnexpectedEof
    }
    continue received + new_received
  }
  buf.unsafe_reinterpret_as_bytes()
}

///|
/// `reader.drop_exactly(len)` drops exactly `len` bytes from the reader.
/// `drop_exactly` waits until enough bytes are available.
/// If EOF is reached before enough data is dropped, `UnexpectedEof` will be raised.
impl BinaryReader with fn drop_exactly(self, len) {
  let controller = BinaryReaderImpl::get_controller(self)
  if controller.buf_len >= len {
    controller.drop(len)
    return
  }

  let buffered = controller.buf_len
  controller.drop(buffered)
  controller.enlarge_to(1)
  for dropped = buffered {
    let n = BinaryReaderImpl::raw_read_impl(
      self,
      controller.buf,
      offset=controller.buf_start,
      max_len=controller.buf.length() - controller.buf_start,
    ) catch {
      err => raise TransportError(err, context=BinaryReaderImpl::name(self))
    }
    if n is 0 {
      raise UnexpectedEof
    }
    if dropped + n >= len {
      let remaining = dropped + n - len
      controller.buf_start += n - remaining
      controller.buf_len = remaining
      break
    }
    continue dropped + n
  }
}

///|
/// `reader.peek_exactly(len)` fetches exactly `len` bytes from the reader.
/// It will wait until enough number of bytes is available.
/// The returned data will not get consumed from the reader:
/// other reader API can still fetch them later.
///
/// The chunk returned by `reader.peek_exactly()` is a copy-free slice
/// of the reader's internal buffer, and its content will be overwritten
/// by subsequent reader operations. So caller of `peek` MUST NOT use the returned slice
/// after any other `BinaryReader` operation is invoked on the same reader.
/// If the data need to be kept persistent, copy it via `.to_owned()`.
///
/// If EOF is reached before enough data is read, `UnexpectedEof` will be raised.
/// In this case remaining data remain buffered, and can be fetched by other reader operations later.
///
/// `reader.peek_exactly()` is cancellation safe as long as
/// the underlying reader implementation is cancellation safe.
/// When `reader.peek_exactly()` is cancelled, no data will be lost.
impl BinaryReader with fn peek_exactly(self, len) {
  self.peek_at_least(len)[:len]
}

///|
#internal(internal, "for internal use only")
#doc(hidden)
pub async fn[R : BinaryReader] read_all_binary(
  reader : R,
  prefix_list~ : FixedArray[BytesView] = [],
  chunk_size~ : Int32 = 4096,
) -> Bytes raise ReaderError {
  let buffer_list = []
  let mut buffer = FixedArray::make(chunk_size, b'\x00')
  let mut offset = 0
  while reader.read_into(buffer.mut_view(start=offset)) is n && n > 0 {
    offset += n
    if offset == buffer.length() {
      buffer_list.push(buffer)
      buffer = FixedArray::make(chunk_size, b'0')
      offset = 0
    }
  }
  let prefix_len = for prefix in prefix_list; len = 0 {
    continue len + prefix.length()
  } nobreak {
    len
  }
  if buffer_list.length() > (@int.MAX_VALUE - offset - prefix_len) / chunk_size {
    raise LengthLimitExceeded
  }
  let total_size = prefix_len + buffer_list.length() * chunk_size + offset
  let result = FixedArray::make(total_size, b'0')
  for
    prefix in prefix_list
    dst_offset = 0
    dst_offset = dst_offset + prefix.length() {
    result.blit_from_bytesview(dst_offset, prefix)
  }
  for i, buf in buffer_list {
    result.unsafe_blit(prefix_len + i * chunk_size, buf, 0, chunk_size)
  }
  result.unsafe_blit(
    prefix_len + buffer_list.length() * chunk_size,
    buffer,
    0,
    offset,
  )
  result.unsafe_reinterpret_as_bytes()
}

///|
/// Read all remaining content from a binary reader, until EOF is reached.
impl BinaryReader with fn read_all(self) {
  read_all_binary(self)
}

///|
/// Drop all remaining content from a binary reader, until EOF is reached.
impl BinaryReader with fn drop_all(self) {
  let controller = BinaryReaderImpl::get_controller(self)
  let buffered = controller.buf_len
  if buffered > 0 {
    controller.drop(buffered)
  }

  controller.enlarge_to(1)
  for dropped = buffered.to_int64() {
    let n = BinaryReaderImpl::raw_read_impl(
      self,
      controller.buf,
      offset=controller.buf_start,
      max_len=controller.buf.length() - controller.buf_start,
    ) catch {
      err => raise TransportError(err, context=BinaryReaderImpl::name(self))
    }
    if n is 0 {
      break dropped
    }
    continue dropped + n.to_int64()
  }
}

///|
/// `reader.read_until(pat, include_delimiter? : Bool = false)` reads content from `reader`
/// until the byte string `pat` is found in the reader stream.
///
/// If `pat` is found in the input stream, content before that occurrence is returned
/// and the delimiter is consumed.
/// If `include_delimiter` is `true`, `pat` itself is included at the end of the result.
/// The returned data may be empty in this case if `include_delimiter` is `false`.
///
/// EOF is implicitly treated as a delimiter by `reader.read_until()`.
/// If `pat` is not found in the input stream when EOF is reached,
/// trailing data before EOF will be returned (or `None` if there is no trailing data).
/// In this case, the returned data contains no trailing delimiter
/// even if `include_delimiter=true`. This can be used to detect the presence of
/// trailing delimiter in the input stream.
///
/// `reader.read_until()` is cancellation safe as long as
/// the underlying reader implementation is cancellation safe.
/// When `reader.read_until()` is cancelled, no data will be lost.
impl BinaryReader with fn read_until(
  self,
  pattern,
  include_delimiter~ : Bool = false,
) {
  let controller = BinaryReaderImpl::get_controller(self)
  if self.peek_until(pattern, include_delimiter~) is Some(result) {
    let result = result.to_owned()
    let consumed = if include_delimiter {
      result.length()
    } else {
      @cmp.minimum(result.length() + pattern.length(), controller.buf_len)
    }
    controller.drop(consumed)
    Some(result)
  } else {
    None
  }
}

///|
/// `reader.peek_until(pat, include_delimiter? : Bool = false)` fetches content from `reader`
/// until the byte string `pat` is found in the reader stream.
/// The returned data will not get consumed from the reader:
/// other reader API can still fetch them later.
///
/// The chunk returned by `reader.peek_until()` is a copy-free slice
/// of the reader's internal buffer, and its content will be overwritten
/// by subsequent reader operations. So caller of `peek` MUST NOT use the returned slice
/// after any other `BinaryReader` operation is invoked on the same reader.
/// If the data need to be kept persistent, copy it via `.to_owned()`.
///
/// If `pat` is found in the input stream, content before that occurrence is returned.
/// If `include_delimiter` is `true`, `pat` itself is included at the end of the result.
/// The returned data may be empty in this case if `include_delimiter` is `false`.
///
/// EOF is implicitly treated as a delimiter by `reader.peek_until()`.
/// If `pat` is not found in the input stream when EOF is reached,
/// trailing data before EOF will be returned (or `None` if there is no trailing data).
/// In this case, the returned data contains no trailing delimiter
/// even if `include_delimiter=true`. This can be used to detect the presence of
/// trailing delimiter in the input stream.
///
/// `reader.peek_until()` is cancellation safe as long as
/// the underlying reader implementation is cancellation safe.
/// When `reader.peek_until()` is cancelled, no data will be lost.
impl BinaryReader with fn peek_until(
  self,
  pattern,
  include_delimiter~ : Bool = false,
) {
  let controller = BinaryReaderImpl::get_controller(self)
  let pattern_len = pattern.length()
  guard pattern_len > 0 else {
    abort("BinaryReader::peek_until() requires a non-empty pattern")
  }
  for search_start = 0 {
    let search_region = controller.buffered()[search_start:]
    if search_region.find(pattern) is Some(i) {
      let pattern_start = search_start + i
      let end_index = if include_delimiter {
        pattern_start + pattern_len
      } else {
        pattern_start
      }
      return Some(controller.buffered()[:end_index])
    }
    let old_len = controller.buf_len
    try self.peek_at_least(old_len + 1) catch {
      UnexpectedEof if controller.buf_len == 0 => return None
      UnexpectedEof => return Some(controller.buffered())
      err => raise err
    } noraise {
      _ => ()
    }
    continue @cmp.maximum(0, old_len - pattern_len + 1)
  }
}