#pragma once

#include <algorithm>

#include "../Assets/Buildings.h"
#include "../Animator.h"
#include "Building.h"
#include "../Assets/Constants.h"

namespace mechanics {
	class TownBuilding : public Building {
	public:
		static const int BUILDING_AREA = 6;

		TownBuilding(const assets::TownBuilding &given_provider, Animator &given_animator):
			mProvider(given_provider), mBuildingArea(assets::constants::general::buildRange),
			Building(given_animator, OwnerType::player)
		{
			given_animator.setAnimation(North, "Stand");

			if(given_provider.ID == "Town center") {
				mBuildingArea *= 2;
			}

			mHP=provider().HP;
		}

		virtual float maximumHP() const {
			return provider().HP;
		}

		virtual float XPReward() const {
			return provider().HP/20+provider().size.Area()*2;
		}

		virtual float CalculateDamageReduction(Attacker *attacker, BaseWeapon *weapon, float damage) {
			return damage-provider().armor;
		}

		virtual void Killed(Attacker *attacker, BaseWeapon *weapon);

		virtual void locateTo(const gge::utils::Point &given_location);

		virtual void setLocation(const gge::utils::Point &given_location);

		gge::utils::Bounds currentArea() const {
			return gge::utils::Bounds(location.x, location.y, location.x + mProvider.size.Width, location.y + mProvider.size.Height);
		}

		virtual gge::utils::Size size() const {
			return provider().size;
		}

		int capacity() const {
			return std::max(1,(int)((mProvider.nonPassablePoints.size()+mProvider.size.Area())/assets::constants::general::TBCapDenominator));//!const
		}

		int buildingArea() const {
			return mBuildingArea;
		}

		const assets::TownBuilding &provider() const {
			return mProvider;
		}

		virtual void removeFromMap() {
			Object::removeFromMap();
			rebuildBuildableMap();
			Object::setPassable(mProvider);
		}

		void destroy();
		void rebuildBuildableMap();
		void setAreaBuildable();

	private:
		const assets::TownBuilding &mProvider;
		int mBuildingArea;
	};

}