#pragma once
#include "../../../../PerceptualState/Locations/Static/AutoPopulated/BaseLocation.h"
#include "../../../../PerceptualState/Locations/Static/StaticLocation.h"

class L2BuildingPlacer
{
public:
  L2BuildingPlacer();
  ~L2BuildingPlacer();

  bool needsAdditionalPylons() const;

  void onBuildingCompleted(BWAPI::Unit* building);

  BWAPI::TilePosition getBuildingLocation(BWAPI::Unit* builder, BWAPI::UnitType toBuild, AutoPopulatedLocation* location, int minBuildDistance);
  void reservePosition(BWAPI::TilePosition tile, BWAPI::UnitType toBuild);
  void unreservePosition(BWAPI::TilePosition tile, BWAPI::UnitType toBuild);

  void onFrameDebug();
private:
  bool overReservedPosition(BWAPI::TilePosition tile,BWAPI::UnitType toBuild);
  bool canBuildHere(BWAPI::Unit* builder, BWAPI::TilePosition tile, BWAPI::UnitType type, bool checkExplored = true);
  bool canBuildHereWithSpace(BWAPI::Unit* builder, BWAPI::TilePosition tile, BWAPI::UnitType type, int minBuildDistance, bool checkExplored = true);
  bool buildable(BWAPI::Unit* builder, int x, int y);
  bool tileHasBuilding(BWAPI::TilePosition tile);
  bool isTileWalkable(BWAPI::TilePosition tile);
  bool canWalkAround(BWAPI::TilePosition tile, BWAPI::UnitType type);
  BWAPI::TilePosition getBuildLocationNear(BWAPI::Unit* builder, BWAPI::TilePosition tile, BWAPI::UnitType type, int minBuildDistance, int maxDistance);

  bool** const m_reservedEmplacements;
  bool m_needAdditionalPylons;
  std::set<BWAPI::Unit*> m_nexusesWithoutPylon;

  L2BuildingPlacer& operator=(const L2BuildingPlacer&);
};