#pragma once

#include "Animation.h"
#include <Resource/Pointer.h>
#include "../File.h"
#include "../Base.h"

namespace gorgon {

	class Pointer;

	Pointer *LoadPointer(Base *parent, File &file, std::istream &Data, int Size);

	class Pointer : public gge::resource::Pointer, public Base {
		friend Pointer *LoadPointer(gorgon::Base *parent, File &file, std::istream &Data, int Size);
	public:
		Pointer() : notmine(false) {
			gge::utils::CheckAndDelete(animation);
			animation=new gorgon::Animation;
		}

		Animation &GetAnimation() {
			return *dynamic_cast<Animation*>(animation);
		}

		void Save(std::ostream &out) {
			GetAnimation().Save(out, gge::resource::GID::Pointer, [&](std::ostream &out) {
				auto start=WriteSectionStart(out, gge::resource::GID::Pointer_Props);
				WriteTo(out, Hotspot);
				WriteTo(out, (int)Type);
				WriteEnd(out, start);
			});
		}

		GID::Type GetGID() const {
			return gge::resource::Pointer::GetGID();
		}

		virtual void Prepare(gge::GGEMain &main, File &file) {
			GetAnimation().gorgon::Base::Prepare(main, file);
		}

		void SetAnimation(Animation &anim) {
			if(!notmine)
				gge::utils::CheckAndDelete(animation);
			animation=&anim;
			notmine=true;
		}

		~Pointer() {
			if(notmine)
				animation=NULL;
		}

	protected:
		bool notmine;
	};

}