#include "server/BattleRoomManager.h"
#include "server/BattleRoom.h"
#include "server/GameServer.h"
#include "server/ClientHandler.h"
#include "core/Characters.h"
#include <iostream>

BattleRoomManager::BattleRoomManager(GameServer* server)
    : m_server(server), m_nextRoomId(1) {
}

BattleRoomManager::~BattleRoomManager() {
    std::lock_guard<std::mutex> lock(m_mutex);
    m_rooms.clear();
}

int BattleRoomManager::createBattle(const std::string& player1, const std::string& player2) {
    // 获取两个玩家的ClientHandler
    auto client1 = m_server->getClientByUsername(player1);
    auto client2 = m_server->getClientByUsername(player2);
    
    if (!client1 || !client2) {
        std::cerr << "Failed to create battle: player not found" << std::endl;
        return -1;
    }
    
    // 获取用户ID
    int userId1 = client1->getUserId();
    int userId2 = client2->getUserId();
    
    // 从数据库获取角色信息并创建角色实例
    CharacterData charData1, charData2;
    
    if (!m_server->getDatabase()->getCharacterData(userId1, charData1)) {
        std::cerr << "Failed to get character data for " << player1 << std::endl;
        return -1;
    }
    
    if (!m_server->getDatabase()->getCharacterData(userId2, charData2)) {
        std::cerr << "Failed to get character data for " << player2 << std::endl;
        return -1;
    }
    
    auto char1 = createCharacter(charData1.className, player1, charData1.level);
    auto char2 = createCharacter(charData2.className, player2, charData2.level);
    
    if (!char1 || !char2) {
        std::cerr << "Failed to create characters" << std::endl;
        return -1;
    }
    
    // 创建战斗房间
    std::lock_guard<std::mutex> lock(m_mutex);
    int roomId = m_nextRoomId++;
    auto room = std::make_shared<BattleRoom>(roomId, client1, client2, char1, char2);
    m_rooms[roomId] = room;
    
    // 设置玩家的战斗房间ID
    client1->setBattleRoomId(roomId);
    client2->setBattleRoomId(roomId);
    
    std::cout << "Created battle room " << roomId << ": " << player1 << " vs " << player2 << std::endl;
    
    // 启动战斗
    room->start();
    
    return roomId;
}

std::shared_ptr<BattleRoom> BattleRoomManager::getBattleRoom(int roomId) {
    std::lock_guard<std::mutex> lock(m_mutex);
    auto it = m_rooms.find(roomId);
    if (it != m_rooms.end()) {
        return it->second;
    }
    return nullptr;
}

bool BattleRoomManager::handleBattleAction(int roomId, 
                                          const std::string& username,
                                          int skillIndex,
                                          const std::string& targetName) {
    auto room = getBattleRoom(roomId);
    if (!room) {
        return false;
    }
    
    return room->handleAction(username, skillIndex, targetName);
}

void BattleRoomManager::removeBattleRoom(int roomId) {
    std::lock_guard<std::mutex> lock(m_mutex);
    auto it = m_rooms.find(roomId);
    if (it != m_rooms.end()) {
        std::cout << "Removing battle room " << roomId << std::endl;
        m_rooms.erase(it);
    }
}

bool BattleRoomManager::isPlayerInBattle(const std::string& username) {
    std::lock_guard<std::mutex> lock(m_mutex);
    for (const auto& pair : m_rooms) {
        // 这里需要检查房间中是否有该玩家
        // 由于我们没有直接的方法，暂时简化处理
        if (!pair.second->isFinished()) {
            return true;  // 简化版本
        }
    }
    return false;
}

std::shared_ptr<ICharacter> BattleRoomManager::createCharacter(const std::string& className,
                                                               const std::string& name,
                                                               int level) {
    std::shared_ptr<ICharacter> character;
    
    if (className == "Warrior") {
        character = std::make_shared<Warrior>(name);
    } else if (className == "Mage") {
        character = std::make_shared<Mage>(name);
    } else {
        // 默认创建战士
        character = std::make_shared<Warrior>(name);
    }
    
    return character;
}
