local HeroExcel = require("excel.hero")
local UpNeedExcel = require("excel.upNeed")
local CombatDefine = require("combat.CombatDefine")
local RoleDefine = require("role.RoleDefine")
local PanelDefine = require("broadcast.PanelDefine")
local BagLogic = require("bag.BagLogic")
local ItemDefine = require("bag.ItemDefine")
local HeroGrid = require("hero.HeroGrid")
local Config = require("Config")

-- 战役第五关特殊处理 人生第一次打打则怪物攻击力*1.5 人生第二次打则怪物攻击力*0.5
function handleBattle5(human, combatType, defender)
	if combatType == CombatDefine.COMBAT_TYPE1 and human.db.battleID == 5 then
		human.db.battleHis = human.db.battleHis or 0
		local atkRate
		if human.db.battleHis == 0 then
			atkRate = 1.5
		elseif human.db.battleHis == 1 then
			atkRate = 0.5
		end
		human.db.battleHis = human.db.battleHis + 1
		if atkRate then
			for index = 1, CombatDefine.COMBAT_HERO_CNT do
				local obj = defender[index]
				if obj then
					obj.attrs[RoleDefine.ATK] = math.ceil(obj.attrs[RoleDefine.ATK] * atkRate)
				end
			end
		end
	end
end


-- 征战第8关战斗胜利后回主城
function handleBattle8End(human, isWin, isVideo, combatType, msgRet)
    if Config.PROJECT_NAME ~= "ssecy" then
        if isWin and not isVideo and combatType == CombatDefine.COMBAT_TYPE1 and human.db.battleID == 9 then
            msgRet.data.panelIDs[0] = 1
            msgRet.data.panelIDs[1] = PanelDefine.PANEL_ID_1001
        end
    end

end


-- 征战第12关战斗胜利后回主城
function handleBattle12End(human, isWin, isVideo, combatType, msgRet)
    if Config.PROJECT_NAME ~= "ssecy" then
        if isWin and not isVideo and combatType == CombatDefine.COMBAT_TYPE1 and human.db.battleID == 13 then
            msgRet.data.panelIDs[0] = 1
            msgRet.data.panelIDs[1] = PanelDefine.PANEL_ID_1001
        end
    end    
end

-- 比较2个武将
local function isBetterHero(heroGrid, targetGrid)
    if not targetGrid then return true end
    local heroConfig = HeroExcel.hero[heroGrid.id]
    if not heroConfig then return end
    local targetConfig = HeroExcel.hero[targetGrid.id]
    if not targetConfig then return true end

    if heroConfig.star ~= targetConfig.star then
        return heroConfig.star > targetConfig.star
    end
    return heroGrid.zhandouli > targetGrid.zhandouli
end

-- 征战挑战失败提示 武将升级
function handleBattleFail(human, isWin, combatType, isVideo, msgRet)
	if isWin or isVideo then return end
    if combatType ~= CombatDefine.COMBAT_TYPE1 then return end
    if human.db.lv >= 15 then return end -- 人物等级
    local targetGrid = nil
    for index,heroGrid in pairs(human.db.heroBag) do   
        if index ~= 0 then
            if heroGrid.quality > 0 then
                return -- 进阶过就不提示了
            end
            if isBetterHero(heroGrid, targetGrid) then
                targetGrid = heroGrid
            end
        end
    end
    if not targetGrid then return end

    local heroConfig = HeroExcel.hero[targetGrid.id]
    local nowLv = targetGrid.lv
    local maxLv = HeroGrid.getMaxLv(heroConfig.star, targetGrid.quality)
    if nowLv >= maxLv then -- 进阶
        local upcf = UpNeedExcel.upQuality[targetGrid.quality + 1]
        if not upcf then return end    
        -- 判断消耗材料
        if human.db.jinbi < upcf.money then return end
        if BagLogic.getItemCnt(human, ItemDefine.ITEM_HERO_UPGRADE_ID) < upcf.jinjieshi then
            return 
        end          
    else -- 升级
        local upcf = UpNeedExcel.upLv[nowLv + 1]
        if not upcf then return end
        if human.db.jinbi < upcf.money then return end
        if BagLogic.getItemCnt(human, ItemDefine.ITEM_GREEN_EXP_ID) < upcf.soul then
            return 
        end        
    end
    msgRet.data.param = "1"
end