/*************************************************************************/
/*  file_access_pack.h                                                   */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                 */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/
#ifndef FILE_ACCESS_PACK_H
#define FILE_ACCESS_PACK_H

#include "os/file_access.h"
#include "os/dir_access.h"
#include "map.h"
#include "list.h"
#include "print_string.h"

class PackSource;

class PackedData {
friend class FileAccessPack;
friend class DirAccessPack;
friend class PackSource;

public:
	struct PackedFile {

		String pack;
		uint64_t offset;
		uint64_t size;
		PackSource* src;
	};

private:
	struct PackedDir {
		PackedDir *parent;
		String name;
		Map<String,PackedDir*> subdirs;
		Set<String> files;
	};


	Map<String,PackedFile> files;
	Vector<PackSource*> sources;

	PackedDir *root;
	//Map<String,PackedDir*> dirs;

	static PackedData *singleton;
	bool disabled;

public:

	void add_pack_source(PackSource* p_source);
	void add_path(const String& pkg_path, const String& path, uint64_t ofs, uint64_t size, PackSource* p_src); // for PackSource

	void set_disabled(bool p_disabled) { disabled=p_disabled; }
	_FORCE_INLINE_ bool is_disabled() const { return disabled; }

	static PackedData *get_singleton() { return singleton; }
	Error add_pack(const String& p_path);

	_FORCE_INLINE_ FileAccess *try_open_path(const String& p_path);
	_FORCE_INLINE_ bool has_path(const String& p_path);

	PackedData();
};

class PackSource {

public:

	virtual bool try_open_pack(const String& p_path)=0;
	virtual FileAccess* get_file(const String& p_path, PackedData::PackedFile* p_file)=0;
};

class PackedSourcePCK : public PackSource {

public:

	virtual bool try_open_pack(const String &p_path);
	virtual FileAccess* get_file(const String& p_path, PackedData::PackedFile* p_file);
};


class FileAccessPack : public FileAccess {

	PackedData::PackedFile pf;

	mutable size_t pos;
	mutable bool eof;

	FileAccess *f;
	virtual Error _open(const String& p_path, int p_mode_flags);
	virtual uint64_t _get_modified_time(const String& p_file) { return 0; }

public:


	virtual void close();
	virtual bool is_open() const;

	virtual void seek(size_t p_position);
	virtual void seek_end(int64_t p_position=0);
	virtual size_t get_pos() const;
	virtual size_t get_len() const;

	virtual bool eof_reached() const;

	virtual uint8_t get_8() const;


	virtual int get_buffer(uint8_t *p_dst,int p_length) const;

	virtual void set_endian_swap(bool p_swap);

	virtual Error get_error() const;

	virtual void store_8(uint8_t p_dest);

	virtual void store_buffer(const uint8_t *p_src,int p_length);

	virtual bool file_exists(const String& p_name);


	FileAccessPack(const String& p_path, const PackedData::PackedFile& p_file);
	~FileAccessPack();
};


FileAccess *PackedData::try_open_path(const String& p_path) {

	if (files.has(p_path)) {
		return files[p_path].src->get_file(p_path, &files[p_path]);
	}

	return NULL;
}

bool PackedData::has_path(const String& p_path) {

	return files.has(p_path);
}


class DirAccessPack : public DirAccess {


	PackedData::PackedDir *current;

	List<String> list_dirs;
	List<String> list_files;
	bool cdir;

public:

	virtual bool list_dir_begin();
	virtual String get_next();
	virtual bool current_is_dir() const;
	virtual void list_dir_end();

	virtual int get_drive_count();
	virtual String get_drive(int p_drive);

	virtual Error change_dir(String p_dir);
	virtual String get_current_dir();


	virtual bool file_exists(String p_file);

	virtual Error make_dir(String p_dir);

	virtual Error rename(String p_from, String p_to);
	virtual Error remove(String p_name);

	size_t get_space_left();

	DirAccessPack();
	~DirAccessPack();

};


#endif // FILE_ACCESS_PACK_H
