#pragma once

#include <euler/syscall.hpp>

namespace euler {

  class stream {

  public:
    virtual ~stream();
    virtual bool try_read(void *into, uint64_t bytes) = 0;
    virtual bool try_seek(__euler_seek_from from, int64_t offset) = 0;

  };

  class file_stream : public stream {

  private:

    __euler_stream_handle handle;
    bool is_readable;
    bool is_writable;
    uint64_t length;
    uint64_t offset;

    uint8_t *buffer;
    uint64_t buffer_offset;
    uint64_t buffer_size;
    bool buffer_dirty;

    void write_buffer();

  public:

    bool good;

    file_stream(
      __euler_stream_handle handle, bool is_readable, bool is_writable,
      bool clear, bool seek_to_end);

    ~file_stream();

    bool try_read(void *into, uint64_t bytes) override;
    bool try_seek(__euler_seek_from from, int64_t offset) override;

  };

}
