#include "adapters/eft/items/actions/QuestItemSpawner.h"

#include <algorithm>

#include "adapters/eft/items/actions/ItemFactoryService.h"
#include "adapters/eft/items/actions/ItemPlacementService.h"

namespace ta::adapters::eft::items::quest_item_spawner
{
    bool spawn(ta::adapters::il2cpp::Il2CppObjectInstance main_player,
               const std::string& template_id,
               int count)
    {
        constexpr int kMaxQuestSpawnCount = 25;
        count = (std::max)(1, (std::min)(count, kMaxQuestSpawnCount));

        bool any = false;
        for (int i = 0; i < count; ++i)
        {
            Il2CppObject* item = ItemFactoryService::create_preset_item(template_id);
            if (!item) continue;

            const auto is_quest_item = ItemFactoryService::is_quest_item(item);
            if (!is_quest_item)
                continue;

            const bool placed = *is_quest_item
                ? ItemPlacementService::place_in_quest_inventory(main_player, item)
                : ItemPlacementService::place_in_container_slots(main_player, item);

            if (!ItemFactoryService::configure_created_item(item, {1, false, true}))
                continue;
            any = any || placed;
        }
        return any;
    }
}
