#include "character/CharacterEquipmentManager.h"
#include <SDL2/SDL.h>

namespace frostbite2D {

bool CharacterEquipmentManager::Init(const character::JobConfig& jobConfig) {
  defaultEquipment_.clear();

  if (jobConfig.defaultAvatarList.empty()) {
    SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
                "CharacterEquipmentManager: no default avatar entries, fallback to skin-only character rendering");
    return true;
  }

  int loadedCount = 0;
  for (const auto& [slotName, equipmentId] : jobConfig.defaultAvatarList) {
    if (equipmentId < 0) {
      continue;
    }

    auto equipmentConfig = character::loadEquipmentConfig(equipmentId);
    if (!equipmentConfig) {
      SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
                  "CharacterEquipmentManager: equipment %d for slot %s unavailable, skip layered part",
                  equipmentId, slotName.c_str());
      continue;
    }

    defaultEquipment_[slotName] = *equipmentConfig;
    ++loadedCount;
  }

  if (loadedCount == 0) {
    SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
                "CharacterEquipmentManager: no avatar equipment loaded, fallback to skin-only character rendering");
  }

  return true;
}

const character::EquipmentConfig* CharacterEquipmentManager::GetEquip(
    const std::string& slotName) const {
  auto it = defaultEquipment_.find(slotName);
  if (it == defaultEquipment_.end()) {
    return nullptr;
  }
  return &it->second;
}

} // namespace frostbite2D
