local DungeonManager = class("DungeonManager", BaseModule)

-- 外部接口--------------------------------------------------------------------------

-- 挑战
function DungeonManager:reqChallenge(module, id)
    if module == ModuleManager.MODULE_KEY.DUNGEON_GOLD then
        self:reqChallengeGold(id)
    elseif module == ModuleManager.MODULE_KEY.DUNGEON_SHARDS then
        self:reqChallengeShards(id)
    end
end

-- 结算
function DungeonManager:reqEndChallenge(module, id)
    if module == ModuleManager.MODULE_KEY.DUNGEON_GOLD then
        self:reqEndChallengeGold(id)
    elseif module == ModuleManager.MODULE_KEY.DUNGEON_SHARDS then
        self:reqEndChallengeShards(id)
    end
end

-- 扫荡
function DungeonManager:reqSweep(module, id)
    if module == ModuleManager.MODULE_KEY.DUNGEON_GOLD then
        self:reqSweepGold(id)
    elseif module == ModuleManager.MODULE_KEY.DUNGEON_SHARDS then
        self:reqSweepShards(id)
    end
end

-- 内部接口--------------------------------------------------------------------------

function DungeonManager:checkDayChange()
    if EDITOR_MODE then
        Logger.logHighlight("检查跨天：".. tostring(DataManager.DungeonData:getIfCanReset()))
    end
    if not DataManager.DungeonData:getIfCanReset() then
		return
	end

    -- 跨天重置数据
    DataManager.DungeonData:onDayChange()
end

-- 请求挑战金币副本
function DungeonManager:reqChallengeGold(id)
    local moduleKey = ModuleManager.MODULE_KEY.DUNGEON_GOLD
    -- 判断次数
    if DataManager.DungeonData:getRemainTimes(moduleKey) <= 0 then
        GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_1))
        return
    end

    -- 判断体力
    if not DataManager.DungeonData:isEnoughHp(moduleKey) then
        GFunc.showItemNotEnough(GConst.ItemConst.ITEM_ID_VIT)
        ModuleManager.CommerceManager:showBuyVitUI()
        return
    end

    if not DataManager.DungeonData:isCanChallenge(moduleKey) then
        return
    end

    local parmas = {chapter_gold_id = id}
    self:sendMessage(ProtoMsgType.FromMsgEnum.ChapterGoldChallengeStartReq, parmas, {}, self.respChallengeGold, BIReport.ITEM_GET_TYPE.DUNGEON_GOLD_CHALLENGE)
end

-- 响应挑战金币副本
function DungeonManager:respChallengeGold(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.DungeonData:onFightCountReduce(ModuleManager.MODULE_KEY.DUNGEON_GOLD)
        DataManager.DungeonData:setCurFightChapterId(result.reqData.chapter_gold_id)
        ModuleManager.BattleManager:playBattle(GConst.BattleConst.BATTLE_TYPE.DUNGEON_GOLD)
        EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.DUNGEON_CHALLENGE)
    end
end

-- 请求结算金币副本
function DungeonManager:reqEndChallengeGold(id, combatReport, taskProgress, totalDamage, remainingHp)
    local parmas = {
        win = combatReport.victory,
        total_damage = totalDamage,
        remaining_hp = remainingHp,
        chapter_gold_id = id,
        task_stat = taskProgress,
        combatReport = combatReport,
    }
    self:sendMessage(ProtoMsgType.FromMsgEnum.ChapterGoldChallengeSettlementReq, parmas, {}, self.respEndChallengeGold, BIReport.ITEM_GET_TYPE.DUNGEON_GOLD_END)
end

-- 响应结算金币副本
function DungeonManager:respEndChallengeGold(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        local passId = DataManager.DungeonData:getPassedMaxId(ModuleManager.MODULE_KEY.DUNGEON_GOLD)
        DataManager.DungeonData:initDungeonGold(result.gold_challenge)
        ModuleManager.BattleManager:showBattleResultUI(GConst.BattleConst.BATTLE_TYPE.DUNGEON_GOLD, result.rewards, result.reqData.combatReport)

        if passId ~= DataManager.DungeonData:getPassedMaxId(ModuleManager.MODULE_KEY.DUNGEON_GOLD) then
            local data = {}
            data.dungeon_progress = DataManager.DungeonData:getDungeonBIStr()
            CS.ThinkingAnalytics.ThinkingAnalyticsAPI.UserSet(data)
        end

        if result.reqData then
            local taskStat = result.reqData.task_stat
            if taskStat then
                taskStat[GConst.BattleConst.BATTLE_TASK_FIELD.KILL_BOSS] = 0 -- boss不算
                taskStat[GConst.BattleConst.BATTLE_TASK_FIELD.KILL_NORMAL_MONSTER] = 0 -- 击杀小怪数量不算
                taskStat[GConst.BattleConst.BATTLE_TASK_FIELD.PASS_WAVE] = 0 -- 通关波数不算
                ModuleManager.TaskManager:addFightTaskProgress(taskStat)
            end
        end
    end
end

-- 请求扫荡金币副本
function DungeonManager:reqSweepGold(id)
    local moduleKey = ModuleManager.MODULE_KEY.DUNGEON_GOLD
    -- 判断次数
    if DataManager.DungeonData:getRemainTimes(moduleKey) <= 0 then
        GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_1))
        return
    end

    -- 判断体力
    if not DataManager.DungeonData:isEnoughHp(moduleKey) then
        GFunc.showItemNotEnough(GConst.ItemConst.ITEM_ID_VIT)
        ModuleManager.CommerceManager:showBuyVitUI()
        return
    end

    if not DataManager.DungeonData:isCanChallenge(moduleKey) then
        return
    end

    local parmas = {
        chapter_gold_id = id,
    }
    self:sendMessage(ProtoMsgType.FromMsgEnum.ChapterGoldChallengeFarmReq, parmas, {}, self.respSweepGold, BIReport.ITEM_GET_TYPE.DUNGEON_GOLD_SWEEP)
end

-- 响应扫荡金币副本
function DungeonManager:respSweepGold(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.DungeonData:onFightCountReduce(ModuleManager.MODULE_KEY.DUNGEON_GOLD)
        GFunc.showRewardBox(result.rewards)
        EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.DUNGEON_SWEEP)
    end
end

-- 请求挑战碎片副本
function DungeonManager:reqChallengeShards(id)
    local moduleKey = ModuleManager.MODULE_KEY.DUNGEON_SHARDS
    -- 判断次数
    if DataManager.DungeonData:getRemainTimes(moduleKey) <= 0 then
        GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_1))
        return
    end

    -- 判断体力
    if not DataManager.DungeonData:isEnoughHp(moduleKey) then
        GFunc.showItemNotEnough(GConst.ItemConst.ITEM_ID_VIT)
        ModuleManager.CommerceManager:showBuyVitUI()
        return
    end

    if not DataManager.DungeonData:isCanChallenge(moduleKey) then
        return
    end

    local parmas = {chapter_shards_id = id}
    self:sendMessage(ProtoMsgType.FromMsgEnum.ChapterShardsChallengeStartReq, parmas, {}, self.respChallengeShards, BIReport.ITEM_GET_TYPE.DUNGEON_SHARDS_CHALLENGE)
end

-- 响应挑战碎片副本
function DungeonManager:respChallengeShards(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.DungeonData:onFightCountReduce(ModuleManager.MODULE_KEY.DUNGEON_SHARDS)
        DataManager.DungeonData:setCurFightChapterId(result.reqData.chapter_shards_id)
        ModuleManager.BattleManager:playBattle(GConst.BattleConst.BATTLE_TYPE.DUNGEON_SHARDS)
        EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.DUNGEON_CHALLENGE)
    end
end

-- 请求结算碎片副本
function DungeonManager:reqEndChallengeShards(id, combatReport, taskProgress, totalDamage)
    local parmas = {
        win = combatReport.victory,
        total_damage = totalDamage,
        chapter_shards_id = id,
        task_stat = taskProgress,
        combatReport = combatReport,
    }
    self:sendMessage(ProtoMsgType.FromMsgEnum.ChapterShardsChallengeSettlementReq, parmas, {}, self.respEndChallengeShards, BIReport.ITEM_GET_TYPE.DUNGEON_SHARDS_END)
end

-- 响应结算碎片副本
function DungeonManager:respEndChallengeShards(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        local passId = DataManager.DungeonData:getPassedMaxId(ModuleManager.MODULE_KEY.DUNGEON_SHARDS)
        DataManager.DungeonData:initDungeonShards(result.shards_challenge)
        local newRewards = {}
        GFunc.mergeRewards2(result.rewards, newRewards, true)
        ModuleManager.BattleManager:showBattleResultUI(GConst.BattleConst.BATTLE_TYPE.DUNGEON_SHARDS, newRewards, result.reqData and result.reqData.combatReport or {}, nil, nil, true)

        if passId ~= DataManager.DungeonData:getPassedMaxId(ModuleManager.MODULE_KEY.DUNGEON_SHARDS) then
            local data = {}
            data.dungeon_progress = DataManager.DungeonData:getDungeonBIStr()
            CS.ThinkingAnalytics.ThinkingAnalyticsAPI.UserSet(data)
        end

        if result.reqData then
            local taskStat = result.reqData.task_stat
            if taskStat then
                taskStat[GConst.BattleConst.BATTLE_TASK_FIELD.KILL_BOSS] = 0 -- boss不算
                taskStat[GConst.BattleConst.BATTLE_TASK_FIELD.KILL_NORMAL_MONSTER] = 0 -- 击杀小怪数量不算
                ModuleManager.TaskManager:addFightTaskProgress(taskStat)
            end
        end
    end
end

-- 请求扫荡碎片副本
function DungeonManager:reqSweepShards(id)
    local moduleKey = ModuleManager.MODULE_KEY.DUNGEON_SHARDS
    -- 判断次数
    if DataManager.DungeonData:getRemainTimes(moduleKey) <= 0 then
        GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_1))
        return
    end

    -- 判断体力
    if not DataManager.DungeonData:isEnoughHp(moduleKey) then
        GFunc.showItemNotEnough(GConst.ItemConst.ITEM_ID_VIT)
        ModuleManager.CommerceManager:showBuyVitUI()
        return
    end

    if not DataManager.DungeonData:isCanChallenge(moduleKey) then
        return
    end

    local parmas = {
        chapter_shards_id = id,
    }
    self:sendMessage(ProtoMsgType.FromMsgEnum.ChapterShardsChallengeFarmReq, parmas, {}, self.respSweepShards, BIReport.ITEM_GET_TYPE.DUNGEON_SHARDS_SWEEP)
end

-- 响应扫荡碎片副本
function DungeonManager:respSweepShards(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.DungeonData:onFightCountReduce(ModuleManager.MODULE_KEY.DUNGEON_SHARDS)
        GFunc.showRewardBox(result.rewards)
        EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.DUNGEON_SWEEP)
    end
end

return DungeonManager