#pragma once

#include <Widgets/Panel.h>
#include <Widgets/Label.h>
#include "../Mechanics/PlayerGame.h"
#include <sstream>
#include <Widgets/Organizers/LinearOrganizer.h>
#include "../Mechanics/Townsfolk.h"
#include "../Mechanics/Soldier.h"
#include <Widgets/Window.h>
#include <Widgets/DialogWindow.h>
#include <Widgets/Listbox.h>
#include "../Assets/Buildings.h"
#include "../Mechanics/Artisan.h"
#include "Actions.h"

//for vs
#undef interface

namespace interface {

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

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

			SetDefault(ok);
			SetCancel(cancel);

			Title="Crafting Selection";

			auto nl=organizer.nextline;

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

			Hide();

			info.Autosize=gge::widgets::AutosizeModes::None;
			info.SetHeight(45);
			Reorganize();

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

			type.ItemClickedEvent.RegisterLambda([&] {
				if(this->type.GetSelectedItem()) {
					if(mechanics::PlayerGame::current().CheckCraft(IDList[this->type.FindLocation(*this->type.GetSelectedItem())], false)) {
						ok.Enable();
						if(prevselection==this->type.GetValue()) {
							ok.ClickEvent();
						}
					}
					else {
						ok.Disable();
					}

					assets::Item *item=assets::Item::list[IDList[this->type.FindLocation(*this->type.GetSelectedItem())]];

					std::stringstream ss;
					ss<<"Requires: "<<item->cost<<" gold";
					for(int i=0;i<item->requiredBuilding.size();i++)
						ss<<", "<<item->requiredBuilding[i];

					ss<<" and storage building.";

					info.Text=ss.str();

					prevselection=this->type.GetValue();
				}
			});

			ok.ClickEvent.RegisterLambda([&]{
				accepted(IDList[this->type.FindLocation(*this->type.GetSelectedItem())]);
				Close();
			});

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

		virtual void Show(mechanics::Artisan &artisan) {
			type.DeleteAll();
			IDList.clear();

			for(auto it=assets::Item::list.begin();it!=assets::Item::list.end();++it) {
				if(it->second->artisan==artisan.type() && it->second->cost>0) {
					std::stringstream ss;

					ss<<it->second->ID<<" (";

					switch(it->second->type) {
					case assets::Item::Armor:
						ss<<"armor";
						break;
					case assets::Item::Melee:
						ss<<"melee";
						break;
					case assets::Item::Ranged:
						ss<<"bow";
						break;
					case assets::Item::Thrust:
						ss<<"polearm";
						break;
					case assets::Item::Cast:
						ss<<"wand";
						break;
					}
					ss<<"): ";

					switch(it->second->type) {
					case assets::Item::Armor:
						ss<<"armor: "<<it->second->armor;
						break;
					case assets::Item::Melee:
					case assets::Item::Thrust:
					case assets::Item::Cast:
					case assets::Item::Ranged:
						ss<<"dam: "<<it->second->damage<<"("<<std::fixed<<std::setprecision(1)<<(it->second->CD/1000.f)<<"s)";
						break;
					}

					ss<<" ("<<std::setprecision(0)<<it->second->cost<<", "<<it->second->craftTime/1000.f<<"s)";

					type.Add(ss.str());
					IDList.push_back(it->second->ID);
				}
			}

			if(!this->IsVisible()) cancelCount++;
			gge::widgets::DialogWindow::Show(true);
			Move(
				50,
				gge::widgets::TopLevel.GetHeight()-GetHeight()-35
			);

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



		gge::utils::EventChain<CraftingList, std::string> accepted;
	protected:
		gge::widgets::Listbox<std::string> type;
		std::vector<std::string> IDList;
		gge::widgets::Button ok, cancel;
		gge::widgets::Label info;
		gge::widgets::LinearOrganizer organizer;
		std::string prevselection;

	};

}
