#pragma once

#include "fs/ISource.h"
#include "fs/AbsPath.h"
#include "fs/Api.h"

namespace fs
{

class FS_API FileSource final : public ISource
{
public:
	explicit FileSource(AbsPathView i_filepath) noexcept;
	FileSource(AbsPathView i_filepath, Flags i_flags) noexcept;
	~FileSource() noexcept override;

	FileSource(FileSource&& i_other) noexcept;
	FileSource& operator=(FileSource&& i_other) noexcept;

	bool isValid() const noexcept;
	tl::optional<Error> getLastError() const override;

	size_t read(tl::span<uint8_t> data) override;

	void seekBeg(uint64_t i_offset) override;
	void seekRel(int64_t i_offset) override;

	uint64_t tell() const override;
	uint64_t getSize() const override;

	bool isEOS() const override;

	size_t getPreferredBufferSize() const override;

private:
	FileSource() noexcept = default;
	void swap(FileSource& i_other) noexcept;

	void close() noexcept;

	bool m_isConstructed = false;
	std::array<uint64_t, 4> m_arena;

	uint64_t m_offset = 0;
	mutable uint64_t m_size = uint64_t(-1);
	mutable tl::optional<Error> m_optLastError;
};
}

