#include "server/GameLobby.h"
#include "server/GameServer.h"
#include "server/ClientHandler.h"
#include "server/BattleRoomManager.h"
#include "common/Protocol.h"
#include <iostream>

GameLobby::GameLobby(GameServer* server) 
    : m_server(server) {
}

GameLobby::~GameLobby() {
}

void GameLobby::broadcastMessage(const std::string& sender, 
                                const std::string& message,
                                int excludeClientId) {
    std::string chatMsg = Protocol::buildChatMessage(sender, message);
    m_server->broadcastToLobby(chatMsg, excludeClientId);
}

bool GameLobby::handleInvite(const std::string& inviter, const std::string& target) {
    // 获取目标玩家
    auto targetClient = m_server->getClientByUsername(target);
    if (!targetClient) {
        // 目标玩家不存在
        auto inviterClient = m_server->getClientByUsername(inviter);
        if (inviterClient) {
            inviterClient->sendMessage(Protocol::buildResponse(false, "玩家不存在"));
        }
        return false;
    }
    
    // 检查目标玩家是否在大厅中
    if (targetClient->getState() != ClientState::Lobby) {
        auto inviterClient = m_server->getClientByUsername(inviter);
        if (inviterClient) {
            inviterClient->sendMessage(Protocol::buildResponse(false, "该玩家正在忙"));
        }
        return false;
    }
    
    // 发送邀请给目标玩家
    std::string inviteMsg = Protocol::buildInvite(inviter);
    targetClient->sendMessage(inviteMsg);
    
    // 设置目标玩家的待处理邀请
    targetClient->setPendingInviter(inviter);
    
    std::cout << inviter << " 邀请 " << target << " 进行战斗" << std::endl;
    return true;
}

bool GameLobby::handleInviteResponse(const std::string& inviter, 
                                     const std::string& target,
                                     bool accept) {
    // 获取邀请者
    auto inviterClient = m_server->getClientByUsername(inviter);
    if (!inviterClient) {
        return false;
    }
    
    // 获取被邀请者
    auto targetClient = m_server->getClientByUsername(target);
    if (!targetClient) {
        return false;
    }
    
    if (accept) {
        // 接受邀请
        std::cout << target << " 接受了 " << inviter << " 的邀请" << std::endl;
        
        // 通知邀请者
        std::string resultMsg = Protocol::buildMessage(Protocol::S2C_INVITE_RESULT, 
            {target, "accepted"});
        inviterClient->sendMessage(resultMsg);
        
        // 设置双方状态为战斗中
        inviterClient->setState(ClientState::InBattle);
        targetClient->setState(ClientState::InBattle);
        
        // 创建战斗房间
        int roomId = m_server->getBattleManager()->createBattle(inviter, target);
        if (roomId < 0) {
            // 创建失败，恢复状态
            inviterClient->setState(ClientState::Lobby);
            targetClient->setState(ClientState::Lobby);
            inviterClient->sendMessage(Protocol::buildResponse(false, "创建战斗失败"));
            targetClient->sendMessage(Protocol::buildResponse(false, "创建战斗失败"));
            return false;
        }
        
        return true;
    } else {
        // 拒绝邀请
        std::cout << target << " 拒绝了 " << inviter << " 的邀请" << std::endl;
        
        std::string resultMsg = Protocol::buildMessage(Protocol::S2C_INVITE_RESULT, 
            {target, "rejected"});
        inviterClient->sendMessage(resultMsg);
        
        return true;
    }
}
