#pragma once

#include <Engine/Font.h>
#include <Resource/FontTheme.h>
#include <iosfwd>
#include "File.h"

namespace gorgon {	

	static void SaveObject(std::ostream &out, const gge::ShadowParams &sh) {
		auto start=WriteSectionStart(out, gge::resource::GID::FontTheme_Shadow);

		WriteTo(out, (int)sh.Type);

		if(sh.Type==sh.Flat) {
			WriteTo(out, sh.Offset);
			WriteTo(out, sh.Color);
		}
		else if(sh.Type==sh.DropShadow) {
			WriteTo(out, sh.Offset);
			WriteTo(out, sh.Color);
			WriteTo(out, sh.Blur);
		}

		WriteEnd(out, start);
	}

	static void SaveObject(std::ostream &out, const gge::Font &fnt) {
		auto start=WriteSectionStart(out, gge::resource::GID::FontTheme_Font);

		if(fnt.Theme) {
			WriteLink(fnt.Theme, out);
			WriteTo(out, (int)fnt.Style);
			WriteTo(out, fnt.Color);
			SaveObject(out, fnt.Shadow);
		}
		else {
			gge::utils::SGuid::Empty.Save(out);
		}

		WriteEnd(out, start);
	}

	class FontInitiator {
	public:
		FontInitiator() : file(NULL), guid_theme(gge::utils::SGuid::Empty) {
			
		}
		gge::utils::SGuid guid_theme;
		gge::Font::FontStyle Style;
		gge::graphics::RGBint Color;
		gge::ShadowParams Shadow;
		File *file;

		operator gge::Font() {
			if(file==NULL)
				return gge::Font();

			return gge::Font(dynamic_cast<gge::resource::FontTheme*>(file->FindObject(guid_theme)), Color, Style, Shadow);
		}

		static FontInitiator Load(File &file, std::istream &in, int size) {
			FontInitiator f;

			f.file=&file;

			f.guid_theme.Load(in);
			if(f.guid_theme) {
				f.Style=(gge::Font::FontStyle)gge::resource::ReadFrom<int>(in);
				gge::resource::ReadFrom(in, f.Color);

				int gid;

				gge::resource::ReadFrom(in, gid);
				gge::resource::ReadFrom(in, size);

				f.Shadow=gge::ShadowParams::Load(in,size);
			}
			return f;
		}
	};

}
