#pragma once

#include <sstream>

#include <Widgets/Panel.h>
#include <Widgets/Label.h>
#include <Widgets/Organizers/LinearOrganizer.h>
#include <Widgets/Window.h>
#include <Widgets/DialogWindow.h>
#include <Widgets/Listbox.h>

#include "../Assets/Artisan.h"
#include "../Mechanics/PlayerGame.h"
#include "../Mechanics/Townsfolk.h"
#include "../Mechanics/Soldier.h"
#include "../Mechanics/Artisan.h"
#include "Actions.h"

//for vs
#undef interface

namespace interface {

	class ArtisanChooser : public gge::widgets::DialogWindow {
	public:
		ArtisanChooser() : 
			accepted("Accepted", this)
		{
			SetOrganizer(organizer);

			ok.Text="Ok";
			cancel.Text="Cancel";

			SetDefault(ok);
			SetCancel(cancel);

			Title="Artisan chooser";

			auto nl=organizer.nextline;

			organizer
				<<type<<nl
				<<cancel<<ok<<nl
			;

			Hide();

			cancel.ClickEvent.Register(this, &gge::widgets::DialogWindow::Close);
			ok.Disable();

			type.ItemClickedEvent.RegisterLambda([&] {
				if(this->type.GetSelectedItem()) {
					if(prev==this->type.GetValue()) {
						ok.ClickEvent();
					}
					ok.Enable();
					prev=this->type.GetValue();
				}
			});

			ok.ClickEvent.RegisterLambda([&]{
				accepted(type.GetValue());
				Close();
			});

			ClosingEvent.RegisterLambda([&] {
				cancelCount--;
			});
		}

		virtual void Show() {
			type.DeleteAll();

			for(auto it=assets::Artisan::list.begin();it!=assets::Artisan::list.end();++it) {
				type.Add(it->second->ID);
			}

			if(!this->IsVisible()) cancelCount++;
			gge::widgets::DialogWindow::Show(true);
			Move(
				(gge::widgets::TopLevel.GetWidth()-GetWidth())*3/4,
				gge::widgets::TopLevel.GetHeight()-GetHeight()-150
			);

			prev="";
			ok.Disable();
		}



		gge::utils::EventChain<ArtisanChooser, std::string> accepted;
	protected:
		gge::widgets::Listbox<std::string> type;
		gge::widgets::Button ok, cancel;
		gge::widgets::LinearOrganizer organizer;

		std::string prev;

	};

}
