#pragma once

#include <Widgets/Definitions.h>
#include <Widgets/Basic/Rectangle.h>
#include "../Base.h"
#include "../File.h"
#include "../Game/Animation.h"

namespace gorgon { namespace widgets {

	class Rectangle;

	Rectangle *LoadRectangle(Base *parent, File &file, std::istream &in, int Size);

	class Rectangle : public gge::widgets::RectangleResource, public Base {
		friend Rectangle *LoadRectangle(gorgon::Base *parent, File &file, std::istream &in, int Size);
	public:

		virtual void Save(std::ostream &out) {
			auto start=WriteObjectStart(out, *this);


			auto propstart=WriteSectionStart(out, gge::widgets::GID::Rectangle_Props);

			WriteLink(Mask, out);
			WriteTo(out, (int)Tiling.Center_Horizontal);
			WriteTo(out, (int)Tiling.Center_Vertical);
			WriteTo(out, (int)Tiling.Top);
			WriteTo(out, (int)Tiling.Bottom);
			WriteTo(out, (int)Tiling.Left);
			WriteTo(out, (int)Tiling.Right);

			WriteEnd(out, propstart);

			WriteSubItems(out, *this);


			WriteEnd(out, start);
		}

		virtual GID::Type GetGID() const {
			return gge::widgets::RectangleResource::GetGID();
		}

		virtual void SetSources(
			Animation &tl, Animation &t, Animation &tr, 
			Animation &l , Animation &c, Animation &r , 
			Animation &bl, Animation &b, Animation &br
		) {
			gorgon::Base::Subitems.Clear();

			gorgon::Base::Subitems.Add(t);
			gorgon::Base::Subitems.Add(tl);
			gorgon::Base::Subitems.Add(tr);
			gorgon::Base::Subitems.Add(c);
			gorgon::Base::Subitems.Add(l);
			gorgon::Base::Subitems.Add(r);
			gorgon::Base::Subitems.Add(b);
			gorgon::Base::Subitems.Add(bl);
			gorgon::Base::Subitems.Add(br);

			gge::widgets::RectangleResource::SetSources(tl, t, tr, l, c, r, bl, b, br);
		}

		//templte should contain filename template which is the filename part before -
		//for instance if files are like /home/usr/border-tl.png then it should be
		// /home/usr/border . This function tries to use following files from most generic
		//to least generic:
		//tl, t, tr, l, c, r, bl, b, br
		//n (corner), h, v, c
		//e (edge), n (corner), c
		//e (edge and corners), c
		//c, or none
		void ImportPNGs(const std::string &templte) {
			gorgon::Image *img;
			gorgon::Animation *tr=new gorgon::Animation;
			gorgon::Animation *t =new gorgon::Animation;
			gorgon::Animation *tl=new gorgon::Animation;
			gorgon::Animation * l=new gorgon::Animation;
			gorgon::Animation * c=new gorgon::Animation;
			gorgon::Animation * r=new gorgon::Animation;
			gorgon::Animation *br=new gorgon::Animation;
			gorgon::Animation *b =new gorgon::Animation;
			gorgon::Animation *bl=new gorgon::Animation;

			img=importitem(templte, "-tl");
			if(img) { tl->Add(img); }

			img=importitem(templte, "-t");
			if(img) { t->Add(img); }

			img=importitem(templte, "-tr");
			if(img) { tr->Add(img); }

			img=importitem(templte, "-l");
			if(img) {  l->Add(img); } 

			img=importitem(templte, "-c");
			if(img) { c->Add(img); }  

			img=importitem(templte, "-r");
			if(img) {  r->Add(img); }

			img=importitem(templte, "-bl");
			if(img) { bl->Add(img); } 
									  
			img=importitem(templte, "-b");
			if(img) { b->Add(img); }  
									  
			img=importitem(templte, "-br");
			if(img) { br->Add(img); }

			SetSources(*tl, *t, *tr, *l, *c, *r, *bl, *b, *br);
		}

		virtual void SetMask(Rectangle *mask) {
			Mask=mask;
		}

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

			if(!Mask)
				Mask		= dynamic_cast<RectangleResource*>(file.Root().FindObject(mask));
		}

		using gorgon::Base::Subitems;

	protected:
		gorgon::Image *importitem(const std::string &templte, const std::string &id) {
			if(gge::os::filesystem::IsFileExists(templte+id+".png")) {
				gorgon::Image *img=new gorgon::Image;
				img->ImportPNG(templte+id+".png");
				return img;
			}
			else {
				if(id=="-tl" || id=="-tr" || id=="-bl" || id=="-br") {
					return importitem(templte, "-n");
				}
				else if(id=="-t" || id=="-b") {
					return importitem(templte, "-h");
				}
				else if(id=="-l" || id=="-r") {
					return importitem(templte, "-v");
				}
				else if(id=="-h" || id=="-v" || id=="-n") {
					return importitem(templte, "-e");
				}
				else if(id=="-e") {
					return importitem(templte, "-c");
				}
				else if(id=="-c") {
					return importitem(templte, "");
				}
				else
					return NULL;
			}

		}
	};

}}
