#include "Folder.h"
#include "File.h"

namespace gorgon {

	void Folder::Save(std::ostream &out) {

		//GID, size, GUID
		auto start=WriteObjectStart(out, *this);

		auto propstart=WriteSectionStart(out, gge::resource::GID::Folder_Props);
		WriteTo(out, (int)SignificantNames);
		WriteEnd(out, propstart);

		//Names & captions
		WriteNames(out, *this, gge::resource::GID::Folder_Names, gge::resource::GID::Folder_Name);

		//Subitems
		WriteSubItems(out, *this);

		//setting size
		WriteEnd(out, start);
	}
	
	Folder * LoadFolder(Base *parent, File &file, std::istream &in, int Size) {
		Folder &f=*new Folder;
		
		if(parent)
			f.SetParent(*parent);
		else
			f.RemoveParent();
		f.file=&file;

		std::vector<namecaption> names;

		LoadEntries(in, Size, file, f, [&](GID::Type &gid, int size) -> bool {
			switch(gid) {
			case gge::resource::GID::Folder_Names:
				LoadNames(in, size, names, gge::resource::GID::Folder_Name);
				return true;

			case gge::resource::GID::Folder_Props:
				f.SignificantNames=gge::resource::ReadFrom<int>(in)!=0;
				return true;

			default:
				return false;
			}
		}, names);
		
		return &f;
	}

}
