#pragma once
#include "../../Interface/Actions/AttackMove.h"
#include "../../../Layer1Reactive/Layer1FSM/Behaviors/Protoss/EngageEnemies/GroupAttack/GroupAttackBehavior.h"
#include "../../../../Utils/Process/ProcessContainer.h"
#include "L2GroupTacticalAction.h"

class L2AttackMove : public virtual AttackMove, public L2GroupTacticalAction
{
public:
  L2AttackMove(const Process* parent, unsigned int priority, const PersistentUnitGroup* initialGroup, AutoPopulatedLocation* location)
    : AttackMove(parent, priority, initialGroup, location)
    , L2GroupTacticalAction(parent, priority, initialGroup)
    , GroupTacticalAction(parent, priority, initialGroup)
    , TacticalAction(parent, priority)
    , Process(parent)
    , m_event(*this,priority,0,30)
    , m_groupAttackBehavior(*this)
  {
    //assert(dynamic_cast<AutoPopulatedLocation*>(location) != NULL);
    //if(dynamic_cast<AutoPopulatedLocation*>(location))
    //{
      std::pair<AutoPopulatedLocation::building_const_iterator,AutoPopulatedLocation::building_const_iterator> eBuildings = location->getEnemyBuildings();
      if(eBuildings.first == eBuildings.second)
      {
        m_target =  location->getCurrentCenter();
      }
      else
      {
        m_target = (*eBuildings.first)->getPosition();
      }
    //}
    //else
    //{
    //  m_target = location->getCurrentCenter();
    //}
    newUnit = 0;
  }

  Location* getTargetLocation() const
  {
    return m_location;
  }

  void onTimeout();

protected:
  BWAPI::Position m_target;
  int newUnit;

  virtual void onStartImplementation();
  virtual void onCancelImplementation();
  virtual void onEndImplementation();

  virtual void onUnitAddedToGroup(void*, BWAPI::Unit* unit);
  virtual void onUnitRemovedFromGroup(void*, BWAPI::Unit* unit);

  SCHEDULER_EVENT_HANDLER(L2AttackMove,onTimeout) m_event;

private:
  ProcessContainer<GroupAttackBehavior> m_groupAttackBehavior;
};
