#include "adapters/eft/quests/conditions/QuestConditionExecutor.h"

#include "adapters/eft/items/actions/QuestItemSpawner.h"
#include "adapters/eft/quests/conditions/QuestConditionInterop.h"

namespace ta::adapters::eft::quests
{
    void QuestConditionExecutor::execute(
        ta::adapters::il2cpp::Il2CppObjectInstance& quest,
        ta::adapters::il2cpp::Il2CppObjectInstance main_player,
        const QuestCompletionPlan& plan)
    {
        for (const QuestCounterWrite& write : plan.counter_writes)
        {
            if (write.counter)
                static_cast<void>(QuestConditionInterop::set_counter_value(write.counter, write.value));
        }

        if (Il2CppObject* completed_set = QuestConditionInterop::completed_conditions(quest))
        {
            for (const auto& id : plan.completed_ids)
            {
                const auto contains = QuestConditionInterop::contains_completed(completed_set, id);
                if (contains && !contains.value())
                    static_cast<void>(QuestConditionInterop::add_completed(completed_set, id));
            }
        }

        for (const QuestItemSpawn& spawn : plan.item_spawns)
        {
            if (spawn.count > 0 && !spawn.template_id.empty())
            {
                static_cast<void>(ta::adapters::eft::items::quest_item_spawner::spawn(
                    main_player, spawn.template_id, spawn.count));
            }
        }
    }
}
