#pragma once

#include <string>
#include <Widgets/DialogWindow.h>
#include <Widgets/Label.h>
#include <Widgets/LayerWidget.h>
#include <Widgets/Organizers/LinearOrganizer.h>
#include <Widgets/Dialogs/Message.h>
#include <Resource/Image.h>
#include <Gorgon/Generic/Image.h>
#include <Widgets/Numberbox.h>
#include <Graphics/Canvas.h>
#include "ImageList.h"

using namespace gge;

class ImageImport : public widgets::DialogWindow {
	friend class UI;
public:
	ImageImport() : img(NULL) {
		SetOrganizer(organizer);
		organizer.Spacing=1;
		Icon=widgets::WR.Icons("add");

		SetWidth(300);

		preview.Resize(GetUsableWidth()-widgets::WR.ShadowMargins.Right, 128);
		preview.GetLayer().MouseCallback.SetLambda([&](input::mouse::Event::Type event, utils::Point location) {
			return layer_mouse(event, location); 
		});

		filename=" ";
		imgsize=" ";
		mloc=" ";
		full.Text="Fullsize";
		full.ClickEvent.RegisterLambda([&]{
			if(img) {
				Width=img->GetWidth();
				Height=img->GetHeight();
			}
		});

		Width=64;
		Height=64;

		organizer
			<<preview<<organizer.nextline
			<<"Filename: "<<filename<<organizer.nextline
			<<"Image size: "<<imgsize<<organizer.nextline
			<<"Width: "<<Width<<" Height: "<<Height<<organizer.nextline
			<<full<<organizer.nextline
			<<mloc<<organizer.nextline
		;

		Selection=&widgets::WR.Borders.Selection->CreateAnimation(true);


		Ok.Text="OK";

		AddDialogButton(Ok);
		this->SetDefault(Ok);

		Title="Image importer";
		Hide();
	}

	bool layer_mouse(input::mouse::Event::Type event, utils::Point location) {

		std::stringstream ss;
		ss<<location;
		mloc=ss.str();

		return false;
	}

	virtual void Show(const std::string &filename) {
		filepath=filename;

		utils::CheckAndDelete(img);
		img=new ::graphics::Canvas;
		auto ret=img->ImportPNG(filename);
		if(ret==resource::Image::FileNotFound) {
			widgets::dialog::ShowMessage("File not found:\n"+filename, "Import file");
			return;
		}
		img->Prepare();

		if(filepath.find_last_of('/')==filepath.npos)
			this->filename=filepath;
		else
			this->filename=filepath.substr(filepath.find_last_of('/')+1);

		std::stringstream ss;
		ss<<img->GetWidth()<<"x"<<img->GetHeight();

		if(Main.getSystemName()=="tileeditor") {
			Width=32;
			Height=32;
		}
		else {
			if(img->GetHeight()==64 || img->GetHeight()==32) { //probably hurt animation
				Width=img->GetHeight();
				Height=img->GetHeight();
			}
			else {
				if(img->GetHeight()%4==0) {
					Height=img->GetHeight()/4;
					if(img->GetWidth()%(int)Height==0)
						Width=(int)Height;
					else if(img->GetWidth()%3==0)
						Width=img->GetWidth()/3;
				}
				else if(img->GetWidth()%3==0)
					Width=img->GetWidth()/3;
			}
		}

		Width.ChangeEvent.Register(this, &ImageImport::RefreshPreview);
		Height.ChangeEvent.Register(this, &ImageImport::RefreshPreview);

		imgsize=ss.str();

		RefreshPreview();

		widgets::DialogWindow::Show(true);
		MoveToCenter();
	}

	void RefreshPreview() {
		preview.GetLayer().Clear();
		Selection->DrawIn(preview.GetLayer(), utils::Bounds(0,0, Width,Height));
		Selection->DrawIn(preview.GetLayer(), utils::Bounds(Width*2,Height, Width*3,Height*2));
		img->Draw(preview.GetLayer(), 0,0);

	}

	~ImageImport() {
		RemoveOrganizer();
	}

	widgets::Button Ok;

protected:
	std::string filepath;
	::graphics::Canvas *img;
	widgets::BorderData *Selection;

	widgets::LinearOrganizer organizer;

	widgets::Label filename;
	widgets::Label imgsize;
	widgets::Label mloc;
	widgets::Button full;
	widgets::LayerWidget preview;
	widgets::Numberbox<int> Width, Height;

};
