#pragma once
#include <string>
#include <vector>
#include <map>

/**
 * @brief 网络通信协议定义
 * 
 * 协议格式: COMMAND|param1|param2|...\n
 * 所有消息以换行符 \n 结尾
 */

namespace Protocol {
    // ===== 协议命令定义 =====
    
    // === 客户端 -> 服务器 (C2S) ===
    const std::string C2S_REGISTER = "C2S_REG";           // 注册: C2S_REG|username|password
    const std::string C2S_LOGIN = "C2S_LOGIN";            // 登录: C2S_LOGIN|username|password
    const std::string C2S_CHAT = "C2S_CHAT";              // 聊天: C2S_CHAT|message
    const std::string C2S_LIST_PLAYERS = "C2S_LIST";      // 玩家列表: C2S_LIST
    const std::string C2S_INVITE = "C2S_INVITE";          // 邀请: C2S_INVITE|target_username
    const std::string C2S_INVITE_RSP = "C2S_INVITE_RSP";  // 邀请响应: C2S_INVITE_RSP|inviter|accept/reject
    const std::string C2S_BATTLE_ACTION = "C2S_ACTION";   // 战斗行动: C2S_ACTION|skill_index|target
    const std::string C2S_LOGOUT = "C2S_LOGOUT";          // 登出: C2S_LOGOUT
    
    // === 服务器 -> 客户端 (S2C) ===
    const std::string S2C_RESPONSE = "S2C_RESPONSE";      // 通用响应: S2C_RESPONSE|OK/ERROR|message
    const std::string S2C_LOGIN_OK = "S2C_LOGIN_OK";      // 登录成功: S2C_LOGIN_OK|username|class|level|hp|mp
    const std::string S2C_MSG = "S2C_MSG";                // 大厅消息: S2C_MSG|sender|message
    const std::string S2C_LIST_PLAYERS = "S2C_LIST";      // 玩家列表: S2C_LIST|user1,user2,user3
    const std::string S2C_INVITE = "S2C_INVITE";          // 收到邀请: S2C_INVITE|inviter_username
    const std::string S2C_INVITE_RESULT = "S2C_INV_RES";  // 邀请结果: S2C_INV_RES|target|accepted/rejected
    const std::string S2C_BATTLE_START = "S2C_BAT_START"; // 战斗开始: S2C_BAT_START|opponent|class
    const std::string S2C_BATTLE_TURN = "S2C_BAT_TURN";   // 回合通知: S2C_BAT_TURN|YOUR_TURN/OPP_TURN
    const std::string S2C_BATTLE_LOG = "S2C_BAT_LOG";     // 战斗日志: S2C_BAT_LOG|log_message
    const std::string S2C_BATTLE_END = "S2C_BAT_END";     // 战斗结束: S2C_BAT_END|WIN/LOSE
    const std::string S2C_SYSTEM = "S2C_SYSTEM";          // 系统消息: S2C_SYSTEM|message
    
    // ===== 协议工具函数 =====
    
    /**
     * @brief 构建协议消息
     * @param command 命令
     * @param params 参数列表
     * @return std::string 完整的协议消息 (包含换行符)
     */
    std::string buildMessage(const std::string& command, 
                            const std::vector<std::string>& params = {});
    
    /**
     * @brief 解析协议消息
     * @param message 原始消息
     * @param command 输出参数: 命令
     * @param params 输出参数: 参数列表
     * @return bool 解析是否成功
     */
    bool parseMessage(const std::string& message, 
                     std::string& command, 
                     std::vector<std::string>& params);
    
    /**
     * @brief 构建响应消息
     */
    std::string buildResponse(bool success, const std::string& message);
    
    /**
     * @brief 构建登录成功消息
     */
    std::string buildLoginOk(const std::string& username, 
                           const std::string& className,
                           int level, int hp, int mp);
    
    /**
     * @brief 构建聊天消息
     */
    std::string buildChatMessage(const std::string& sender, 
                                const std::string& message);
    
    /**
     * @brief 构建玩家列表消息
     */
    std::string buildPlayerList(const std::vector<std::string>& players);
    
    /**
     * @brief 构建邀请消息
     */
    std::string buildInvite(const std::string& inviter);
    
    /**
     * @brief 构建战斗日志消息
     */
    std::string buildBattleLog(const std::string& logMessage);
    
    /**
     * @brief 构建系统消息
     */
    std::string buildSystemMessage(const std::string& message);
}
