#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 "Actions.h"

//for vs
#undef interface

namespace interface {

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

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

			SetDefault(ok);
			SetCancel(cancel);

			Title="Building selection";

			auto nl=organizer.nextline;

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

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

			Hide();

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

			type.ItemClickedEvent.RegisterLambda([&] {
				if(this->type.GetSelectedItem()) {

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

					std::stringstream ss;
					ss<<"Type: ";
					switch(mb->type) {
					case assets::MilitaryBuilding::Storage:
						ss<<"Storage (";

						switch(mb->storageType) {
						case assets::Item::Armor:
							ss<<"armor)"<<std::endl;
							break;
						case assets::Item::Melee:
							ss<<"melee)"<<std::endl;
							break;
						case assets::Item::Ranged:
							ss<<"bow)"<<std::endl;
							break;
						case assets::Item::Thrust:
							ss<<"polearm)"<<std::endl;
							break;
						case assets::Item::Cast:
							ss<<"wand)"<<std::endl;
							break;
						}

						ss<<"Capacity: "<<mb->capacity;

						break;
					case assets::MilitaryBuilding::MeleeTraining:
						ss<<"Melee training"<<std::endl;
						ss<<"Max level: "<<mb->maxLvl;
						break;
					case assets::MilitaryBuilding::RangedTraining:
						ss<<"Ranged training"<<std::endl;
						ss<<"Max level: "<<mb->maxLvl;
						break;
					case assets::MilitaryBuilding::Housing:
						ss<<"Housing"<<std::endl;
						ss<<"Capacity: "<<mb->capacity;
						break;
					case assets::MilitaryBuilding::Tools:
						ss<<"Artisan tool";
						break;
					}

					info.Text=ss.str();

					if(mechanics::PlayerGame::current().gold()>=mb->constructionCost) {
						ok.Enable();
						if(prevselection==this->type.GetValue()) {
							ok.ClickEvent();
						}
					}
					else {
						ok.Disable();
					}


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

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

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

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

			for(auto it=assets::MilitaryBuilding::list.begin();it!=assets::MilitaryBuilding::list.end();++it) {
				if(it->second->constructionCost>=0) {
					IDList.push_back(it->second->ID);
					std::stringstream ss;
					switch(it->second->type) {
					case assets::MilitaryBuilding::Storage:
						ss<<"Storage: ";
						break;
					case assets::MilitaryBuilding::MeleeTraining:
						ss<<"Melee training: ";
						break;
					case assets::MilitaryBuilding::RangedTraining:
						ss<<"Ranged training: ";
						break;
					case assets::MilitaryBuilding::Housing:
						ss<<"Housing: ";
						break;
					case assets::MilitaryBuilding::Tools:
						ss<<"Artisan tool: ";
						break;
					}

					ss<<it->second->ID<<" ("<<it->second->constructionCost<<")";
					type.Add(ss.str());
				}
			}

			for(int i=0;i<type.GetCount();i++) {
				for(int j=i+1;j<type.GetCount();j++) {
					if(type.GetValue(i)>type.GetValue(j)) {
						std::string temp=type.GetItem(i)->Value;
						type.GetItem(i)->Value=std::string(type.GetItem(j)->Value);
						type.GetItem(j)->Value=temp;

						temp=IDList[i];
						IDList[i]=IDList[j];
						IDList[j]=temp;
					}
				}
			}

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

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



		gge::utils::EventChain<BuildableChooser, 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;

	};

}
