#pragma once

#include "fs/ISink.h"
#include "fs/Mode.h"
#include "fs/AbsPath.h"
#include "tl/flag_set2.h"
#include "fs/Api.h"

namespace fs
{

class FS_API FileSink final : public ISink
{
public:
	FileSink(AbsPathView filepath, Mode mode, Flags flags = Flags()) noexcept;
	~FileSink() noexcept override;

	FileSink(FileSink&& other) noexcept;
	FileSink& operator=(FileSink&& other) noexcept;

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

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

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

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

	size_t getPreferredBufferSize() const override;

	bool resize(uint64_t newSize) noexcept;

private:
	//static tl::result<Handle, Error> openFile(const AbsPath& filepath, Mode mode, Flags flags) noexcept;

	void close() noexcept;

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

	uint64_t m_offset = 0;
	mutable tl::optional<Error> m_optLastError;
};
}
