-------------------------------------------------------
-- 战报

--
-------------------------------------------------------
local LuaMongo = _G.lua_mongo
local WarReportLogic = require("warReport.WarReportLogic")
local InnerMsg = require("core.InnerMsg")
local ObjHuman = require("core.ObjHuman")
local CombatLogic = require("combat.CombatLogic")
local DB = require("common.DB")
local PanelDefine = require("broadcast.PanelDefine")

local war_count_query = { war = 1 }
local war_report_id = { _id = nil }
local war_report_query = { war_index = nil, type = nil }

-- 获取战报数据
local function getWarReport(id)
    war_report_id._id = id
    local war_report_data = { }
    LuaMongo.find(DB.db_war_report, war_report_id)
    if not LuaMongo.next(war_report_data) then
        return
    end
    return war_report_data
end

-- 收藏
local function collectDo(type, id, op)
    if op ~= WarReportLogic.WAR_OP_CONFIRM and op ~= WarReportLogic.WAR_OP_CANCEL then
        return
    end

    local addValue = 1
    if op == WarReportLogic.WAR_OP_CONFIRM then
        addValue = 1
    elseif op == WarReportLogic.WAR_OP_CANCEL then
        addValue = -1
    end

    war_report_id._id = id
    local update_data = { ["$inc"] = { ["collect"] = addValue } }
    LuaMongo.update(DB.db_war_report, war_report_id, update_data)

    return true
end

-- 点赞确认
local function admire(id, uuid)

    local war_report_data = getWarReport(id)
    if not war_report_data then return end

    -- 已经点赞
    if war_report_data.admireList and
        war_report_data.admireList[uuid] then
        return
    end

    war_report_id._id = id
    local update_data = { ["$inc"] = { ["admire"] = 1 }, ["$set"] = { ["admireList." .. uuid] = 1 } }
    LuaMongo.update(DB.db_war_report, war_report_id, update_data)

    return true
end

-- 添加数据
function LW_WARREPORT_ADD(type, combatInfo, atkRank, defRank)
    
    -- 没有索引 返回
    local war_data = { }
    LuaMongo.find(DB.db_war_report, war_count_query)
    if not LuaMongo.next(war_data) then
        return
    end

    local war_index = war_data.war_report_count[type]
    if not war_index then return end

    war_report_query.war_index = war_index
    war_report_query.type = type
    local war_report_data = { }
    LuaMongo.find(DB.db_war_report, war_report_query)

    local warReport = { }
    warReport.type = type
    warReport.war_index = war_index
    warReport.atkUuid = combatInfo.attacker.uuid or ""
    warReport.defUuid = combatInfo.defender.uuid or ""
    warReport.combatInfo = combatInfo
    warReport.atkRank = atkRank
    warReport.defRank = defRank
    warReport.time = os.time()

    -- 覆盖掉最后一场 减少录像数据
    if LuaMongo.next(war_report_data) then
        if war_report_data.collect and war_report_data.collect > 0 then
           -- 有收藏的 删掉索引
           LuaMongo.update(DB.db_war_report, war_report_query, { ["$unset"] = { ["war_index"] = 1 } })

           -- 插入新的数据
           LuaMongo.insert(DB.db_war_report, warReport)

        else

            -- 没有收藏的覆盖掉
           LuaMongo.update(DB.db_war_report, war_report_query, warReport)
        end
    else
        LuaMongo.insert(DB.db_war_report, warReport)
    end

     -- 更新索引
    if war_index >= WarReportLogic.WAR_COUNT_MAX then
        war_index = 1
    else
        war_index = war_index + 1
    end
    war_data.war_report_count[type] = war_index
    LuaMongo.update(DB.db_war_report, war_count_query, war_data)
end


-- [战报] 请求数据
function LW_WARREPORT_QUERY(fd, type, uuid, warReport, questType)
    local war_data_list = nil
    if questType == 1 then
        war_data_list = WarReportLogic.makeReport(type)
    elseif questType == 2 then
        war_data_list = WarReportLogic.makeCollectReport(warReport)
    elseif questType == 3 then
        war_data_list = WarReportLogic.makeMyReport(uuid)
    end

    if not war_data_list then return end

    --local msgInner = InnerMsg.wl.WL_WARREPORT_QUERY
    local msgInner = {}
    msgInner.uuid = uuid
    msgInner.warReport = war_data_list
    msgInner.questType = questType
    --InnerMsg.sendMsg(fd, msgInner)
    WL_WARREPORT_QUERY(_,msgInner)
end

-- [战报] 数据返回
function WL_WARREPORT_QUERY(fd, msg)
    local human = ObjHuman.onlineUuid[msg.uuid]
    if not human then return end

    WarReportLogic.sendReport(human, msg.warReport, msg.questType)
end

-- [战报] 战斗伤害记录
function LW_WARREPORT_GET_COMBATINFO(fd, msg)
    local war_report_data = getWarReport(msg.id)
    if not war_report_data then return end

    local msgInner = {} --InnerMsg.wl.WL_WARREPORT_GET_COMBATINFO
    msgInner.uuid = msg.uuid
    msgInner.mode = msg.mode
    msgInner.combatInfo = war_report_data.combatInfo
    --InnerMsg.sendMsg(fd, msgInner)
    WL_WARREPORT_GET_COMBATINFO(fd,msgInner)
end

-- [战报] 战斗伤害记录 回放
function WL_WARREPORT_GET_COMBATINFO(fd, msg)
    local human = ObjHuman.onlineUuid[msg.uuid]
    if not human then return end

    
    if msg.mode == WarReportLogic.WAR_COMBATINFO_PLAYBACK then
        msg.combatInfo.panelID = PanelDefine.PANEL_ID_1018
        CombatLogic.repeatCombat(human, msg.combatInfo)
    elseif msg.mode == WarReportLogic.WAR_COMBATINFO_HARM then
        WarReportLogic.sendFinish(human, msg.combatInfo)
    end
end

-- [战报] 收藏
function LW_WARREPORT_COLLECT(fd, msg)
    local war_report_data = getWarReport(msg.id)
    if not war_report_data then return end

    if not collectDo(msg.type, msg.id, msg.op) then
        return
    end

    local msgInner = {} --InnerMsg.wl.WL_WARREPORT_COLLECT
    msgInner.uuid = msg.uuid
    msgInner.type = msg.type
    msgInner.id = msg.id
    msgInner.op = msg.op
    --InnerMsg.sendMsg(fd, msgInner)
    WL_WARREPORT_COLLECT(fd,msgInner)
end

-- [战报] 收藏
function WL_WARREPORT_COLLECT(fd, msg)
    local human = ObjHuman.onlineUuid[msg.uuid]
    if not human then return end

    WarReportLogic.collectHumanDo(human, msg.type, msg.id, msg.op)
end

-- [战报] 点赞
function LW_WARREPORT_ADMIRE(fd, msg)
    if not admire(msg.id, msg.uuid) then
       return
    end

    local msgInner = {} --InnerMsg.wl.WL_WARREPORT_ADMIRE
    msgInner.uuid = msg.uuid
    msgInner.id = msg.id
    --InnerMsg.sendMsg(fd, msgInner)
    WL_WARREPORT_ADMIRE(fd,msgInner)
end 

-- [战报] 点赞
function WL_WARREPORT_ADMIRE(fd, msg)
    local human = ObjHuman.onlineUuid[msg.uuid]
    if not human then return end

    WarReportLogic.admireDo(human, msg.id)
end 


-- 战报清除
local delQuery = {war = { ["$exists"] = 0}, war_index = { ["$exists"] = 0}, ["$or"] = {{collect = {["$exists"]=0}}, {collect = 0}}}
function onZero()
   if _G.is_middle ~= true then 
      return 
   end
   -- 0点清除 没有索引 且没有收藏的 数据
   LuaMongo.remove(DB.db_war_report, delQuery)
end

--------------------------- 以下是新的跨服获取战报相关数据 --------------------------------
-- TODO : 理论上应该将这个地方代码重写，但是策划只管完成，也没给多的时间改就先这样吧

-- 获取战报信息(中心->目标服)
function WL_WARREPORT_GET_COMBATINFO_NEW(fd, msg)
    -- 先获取数据
    local war_report_data = getWarReport(msg.id)
    if not war_report_data then
        print("[WL_WARREPORT_GET_COMBATINFO_NEW] 跨服获取战报失败 id = "..msg.id)
        return
    end

    local tMsgData = InnerMsg.lw.LW_WARREPORT_SEND_COMBATINFO
    tMsgData.nSrcUID = msg.nSrcUID
    tMsgData.mode = msg.mode
    tMsgData.combatInfo = war_report_data.combatInfo
    tMsgData.nSrcServerID = msg.nSrcServerID

    InnerMsg.sendMsg(0, tMsgData)
    print("[WL_WARREPORT_GET_COMBATINFO_NEW] 发送数据到指定的服务器完成")
end

function WL_WARREPORT_SEND_COMBATINFO(fd, msg)
    local human = ObjHuman.onlineUuid[msg.nSrcUID]
    if not human then 
        return
    end
    
    if msg.mode == WarReportLogic.WAR_COMBATINFO_PLAYBACK then
        msg.combatInfo.panelID = PanelDefine.PANEL_ID_1018
        CombatLogic.repeatCombat(human, msg.combatInfo)
    elseif msg.mode == WarReportLogic.WAR_COMBATINFO_HARM then
        WarReportLogic.sendFinish(human, msg.combatInfo)
    end
end