--------------------------------
-- 文件名       ：  HeroMiddleLogic.lua
-- 文件说明     ：  中心服获取玩家信息, 处理其他服务器获取跨服玩家信息
-- 创建时间     :   2025/02/18
-- 创建人       :   FC
--------------------------------

local Util = require("common.Util")
local ObjHuman = require("core.ObjHuman")
local InnerMsg = require("core.InnerMsg")
local ChatHandler = require("chat.Handler")
local Config = require("Config")
local Msg = require("core.Msg")
local HeroLogic = require("hero.HeroLogic")
local JjcLogic = require("jjc.JjcLogic")

-- 请求聊天分享的英雄数据
function HeroMiddleLogic_QueryHeroData_LW(human, uuid, heroIndex, nChatType, nServerIndex)
    if ChatHandler.CHAT_TYPE_MIDDLE ~= nChatType and ChatHandler.CHAT_TYPE_WARZONE ~= nChatType then
        return
    end

    local lwMsgRet = InnerMsg.lw.LW_MIDDLE_CHAT_QUERY_HERO_DATA
    lwMsgRet.nSrcServerID = Config.SVR_INDEX
    lwMsgRet.nDesServerID = nServerIndex
    lwMsgRet.nSrcUID = human.db._id
    lwMsgRet.nDesUID = uuid
    lwMsgRet.nHeroIndex = heroIndex
    lwMsgRet.nChatType = nChatType

    print("[HeroMiddleLogic_QueryHeroData_LW] 发送数据到中心服 nSrcServerID = "..lwMsgRet.nSrcServerID.." nDesServerID = "..lwMsgRet.nDesServerID)
    InnerMsg.sendMsg(0, lwMsgRet)
end

-- 获取玩家分享的英雄数据
function HeroMiddleLogic_QueryHeroData_WL(fd, tMsgData)
    local sFindUID = tMsgData.nDesUID
    local nHeroIndex = tMsgData.nHeroIndex

    local tHeroData = Msg.gc.GC_HERO_SHARE_DATA
    local heroGrid, bagIndex, fakeHuman = HeroLogic.getHeroShareGrid(sFindUID, nHeroIndex)
    if heroGrid then
        if not HeroLogic.makeHeroShare(tHeroData.data, heroGrid, bagIndex, fakeHuman) then
            print("[HeroMiddleLogic_QueryHeroData_WL] 获取玩家数据失败")
            return
        end
    else
        local monsterID, mosnterLv = JjcLogic.getHeroShareMonster(sFindUID, nHeroIndex)
        if not monsterID then return end
        HeroLogic.makeHeroShareMonster(tHeroData.data, monsterID, mosnterLv)
    end

    local lwMsgRet = InnerMsg.lw.LW_MIDDLE_CHAT_GET_HERO_DATA
    lwMsgRet.nSrcUID = tMsgData.nSrcUID
    lwMsgRet.nSrcServerID = tMsgData.nSrcServerID
    lwMsgRet.tHeroData = tHeroData

    print("[HeroMiddleLogic_QueryHeroData_WL] 发送数据到中心服 nSrcServerID = "..lwMsgRet.nSrcServerID)
    InnerMsg.sendMsg(0, lwMsgRet)
end

function HeroMiddleLogic_GetHeroData_WL(fd, tMsgData)
    local nSrcUID = tMsgData.nSrcUID
    local human = ObjHuman.onlineUuid[nSrcUID]
    if not human then
        print("[HeroMiddleLogic_GetHeroData_WL] 玩家不在线直接返回")
        return
    end

    -- print("[HeroMiddleLogic_GetHeroData_WL] 获取到数据 nSrcServerID = ")

    -- table.print_lua_table(tMsgData)
    -- print("\n")

    local msgRet = Msg.gc.GC_HERO_SHARE_DATA
    msgRet.data = tMsgData.tHeroData.data
    -- if nil == _G.next(tMsgData.tHeroData.data.equips) then
    --     msgRet.data.equips[0] = 0
    -- end

    -- if nil == _G.next(tMsgData.tHeroData.data.fuWens) then
    --     msgRet.data.fuWens[0] = 0
    -- end

    -- if nil == _G.next(tMsgData.tHeroData.data.heroSimple.gemData) then
    --     msgRet.data.heroSimple.gemData[0] = 0
    -- end

    Msg.send(msgRet, human.fd)
end