#pragma once

#include "foundation/result.h"

#include <cstddef>
#include <cstdint>
#include <span>
#include <vector>

namespace pp::foundation {

class ByteReader {
public:
    explicit ByteReader(std::span<const std::byte> bytes) noexcept;

    [[nodiscard]] std::size_t position() const noexcept;
    [[nodiscard]] std::size_t size() const noexcept;
    [[nodiscard]] std::size_t remaining() const noexcept;
    [[nodiscard]] bool empty() const noexcept;

    [[nodiscard]] Status seek(std::size_t position) noexcept;
    [[nodiscard]] Result<std::uint8_t> read_u8() noexcept;
    [[nodiscard]] Result<std::uint16_t> read_u16_le() noexcept;
    [[nodiscard]] Result<std::uint32_t> read_u32_le() noexcept;
    [[nodiscard]] Result<std::span<const std::byte>> read_bytes(std::size_t count) noexcept;

private:
    std::span<const std::byte> bytes_;
    std::size_t position_ = 0;
};

class ByteWriter {
public:
    explicit ByteWriter(std::vector<std::byte>& bytes) noexcept;

    [[nodiscard]] std::size_t size() const noexcept;
    [[nodiscard]] Status write_u8(std::uint8_t value);
    [[nodiscard]] Status write_u16_le(std::uint16_t value);
    [[nodiscard]] Status write_u32_le(std::uint32_t value);
    [[nodiscard]] Status write_bytes(std::span<const std::byte> bytes);

private:
    std::vector<std::byte>* bytes_ = nullptr;
};

}
