#include "AiRule.h"

#include "ai/conditions/Conditions.h"
#include "ai/actions/Actions.h"

namespace ai {

AiRule::~AiRule()
{
}

void ai::AiRule::onConditionSatisfied()
{
    if (!m_owner) {
        WARN << "No script set for rule!";
        return;
    }
    if (!m_owner->m_player) {
        WARN << "Script does not have a player set!";
        return;
    }

    for (const std::shared_ptr<Condition> &condition : m_conditions) {
        if (!condition->satisfied(this)) {
            return;
        }
    }

    DBG << "rule triggered";

    for (const std::shared_ptr<Action> &action : m_actions) {
        action->execute(this);
    }
}

void AiRule::addCondition(const std::shared_ptr<ai::Condition> &condition)
{
    if (!condition) {
        WARN << "Got null condition";
        return;
    }

    m_conditions.push_back(condition);
    condition->connect(Condition::SatisfiedChanged, this, &AiRule::onConditionSatisfied);
}

void AiRule::addAction(const std::shared_ptr<Action> &action)
{
    if (!action) {
        WARN << "Got null action";
        return;
    }

    m_actions.push_back(action);

}

} //namespace ai
