#pragma once
#include "../../../../Utils/Process/FSM/Condition.h"
#include "HasRangedAttack.h"
#include <BWAPI.h>

//Peut être envoyé dans un .cpp
#include "../../../../PerceptualState/PerceptualState.h"

class IsWorthFleeing : Condition
{
  BWAPI::Unit* m_unit;
  
  typedef Not<HasRangedAttack> IsMelee;

public:
  IsWorthFleeing(BWAPI::Unit* unit) : m_unit(unit) {}

  bool evaluate() const
  {
    const bool hasRangedAttack = HasRangedAttack(m_unit).evaluate();

    std::set<BWAPI::Unit*> targetingUnits = Spar->getPerceptualState()->getTargetingUnits(m_unit);

    for (std::set<BWAPI::Unit*>::const_iterator i=targetingUnits.begin(); i!=targetingUnits.end(); ++i)
    {
      BWAPI::Unit* enemy = *i;
      if (!(hasRangedAttack && IsMelee(enemy).evaluate()))
      {
        return false;
      }
    }

    return true;
  }
};