---------------------------------------------------------
-- boss逻辑
---------------------------------------------------------

local BossDBLogic = require("boss.BossDBLogic")
local CombatDefine = require("combat.CombatDefine")
local CombatLogic = require("combat.CombatLogic")
local CombatImpl = require("combat.CombatImpl")
local RoleLogic = require("role.RoleLogic")

function makeBossHurtNet(net, uuid, hurt)
    if net == nil then return end
    if not RoleLogic.getRoleBaseByUuid(uuid, net.roleBase) then
        return
    end
    net.hurt = hurt or 0
    return true
end

function getCombatObjList(bossData, zhandouli)
    if not bossData then return end

    local objList, pet, rolebase = CombatLogic.getMonsterObjList(bossData.monsterOutID, CombatDefine.COMBAT_OBJ_TYPE3)
    if not objList then return end

    for index, obj in pairs(objList) do
        if BossDBLogic.getBossHp(bossData, index) <= 0 then
            objList[index] = nil
        end
    end
    if zhandouli then
        rolebase.zhandouli = zhandouli
    end
    return objList, pet, rolebase
end

-- 战斗设置
function onFightBegin(human, bossData)
    for index = 1, CombatDefine.COMBAT_HERO_CNT do
        local defPos = CombatLogic.getPos(CombatDefine.DEFEND_SIDE, index)
        local defObj = CombatImpl.objList[defPos]
        if defObj ~= nil then -- 更新血量
            local obj = bossData.objList[index]
            if obj and obj.hp then
                defObj.hp = obj.hp
            end
        end
    end
end

-- 挑战结果
function onFightEnd(human, bossData, combatInfo)
    local hurtSum = 0
    local hurtList = {}
    for index = 1, CombatDefine.COMBAT_HERO_CNT do
        local defPos = CombatLogic.getPos(CombatDefine.DEFEND_SIDE, index)
        local defObj = combatInfo.objList[defPos]
        local hurt = BossDBLogic.updateBossHp(bossData, index, defObj)
        hurtSum = hurtSum + hurt        
        hurtList[index] = hurt
    end

    if bossData.bossType == BossDBLogic.BOSS_TYPE_UNION then
        -- 公会BOSS 伤害为0  不上榜
        if hurtSum > 0 then
            BossDBLogic.updateBossRecord(bossData, human.db._id, hurtSum)
        end
    else
        BossDBLogic.updateBossRecord(bossData, human.db._id, hurtSum)
    end
    BossDBLogic.updateBossData(bossData)
    return hurtSum, hurtList
end

function onFightHurt(human, bossData, combatInfo)
    local hurtSum = 0
    local hurtList = {}
    for index = 1, CombatDefine.COMBAT_HERO_CNT do
        local defPos = CombatLogic.getPos(CombatDefine.DEFEND_SIDE, index)
        local defObj = combatInfo.objList[defPos]
        local hurt = BossDBLogic.updateBossHp(bossData, index, defObj)
        hurtSum = hurtSum + hurt        
        hurtList[index] = hurt
    end
    return hurtSum, hurtList
end

function updateFightHurt(human,  bossData, hurtList, combatInfo)
    local hurtSum = 0
    for index = 1, CombatDefine.COMBAT_HERO_CNT do
        local hurt = hurtList[index]
        if hurt and hurt ~= 0 then
            local defPos = CombatLogic.getPos(CombatDefine.DEFEND_SIDE, index)
            local defObj = combatInfo.objList[defPos]
            local realhurt = BossDBLogic.updateBossHpByHurt(bossData, index, hurt, defObj)
            hurtSum = hurtSum + realhurt        
        end
        
    end
    BossDBLogic.updateBossRecord(bossData, human.db._id, hurtSum)
    BossDBLogic.updateBossData(bossData)

    return hurtSum
end

function saodangEnd(human, bossData,  hurtList)
    local hurtSum = 0
    for index = 1, CombatDefine.COMBAT_HERO_CNT do
        local hurt = hurtList[index]
        if hurt and hurt ~= 0 then
            local realhurt = BossDBLogic.updateBossHpSaoDang(bossData, index, hurt)
            hurtSum = hurtSum + realhurt        
        end
        
    end
    BossDBLogic.updateBossRecord(bossData, human.db._id, hurtSum)
    BossDBLogic.updateBossData(bossData)
    return hurtSum
end

-- 添加记录点赞
function giveLikeBossRecord(bossData, uuid)
    BossDBLogic.updateBossRecordLike(bossData, uuid)
    BossDBLogic.updateBossData(bossData)
    return true
end
