local DungeonManager = class("DungeonManager", BaseModule)

--region UI
function DungeonManager:showDungeonDaylyMainUI(page)
	local params = {
		page = page or GConst.DungeonConst.DUNGEON_DAILY_TYPE.GOLD
	}
	UIManager:showUI("app/ui/dungeon/dungeon_daily_main_ui", params)
end

-- function DungeonManager:showRankUI(showPage, rankList, rankSelf)
--     local params = {
--         showPage = showPage,
--         rankList = rankList,
--         rankSelf = rankSelf,
--     }
--     UIManager:showUI("app/ui/rank/rank_ui", params)
-- end

-- function DungeonManager:showRankRewardUI(showModule)
--     local params = {
--         showModule = showModule,
--     }
--     UIManager:showUI("app/ui/rank/rank_reward_ui", params)
-- end
--endregion

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

-- 扫荡
function DungeonManager:reqSweep(module, id)
	self:reqSweepGold(id)
	-- if module == ModuleManager.MODULE_KEY.DUNGEON_GOLD then
	-- 	self:reqSweepGold(id)
	-- elseif module == ModuleManager.MODULE_KEY.DUNGEON_RES 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:onDungeonStartReq(id)
	if not DataManager.FormationData:formationIsFull(GConst.BattleConst.FORMATION_TYPE.STAGE) then
        GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.BATTLE_DESC_8))
        return
    end
	local params = {
		id = id,
	}
	local dungeonDailyType = DataManager.DungeonDailyData:getDungeonDailyTypeByChapterId(id)
	local ticketCount = 0
	if dungeonDailyType == GConst.DungeonConst.DUNGEON_DAILY_TYPE.GOLD then
		local remainTimes = DataManager.DungeonDailyData.GoldData:getTodayRemainTimes()
		ticketCount = DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_DUNGEON_TICKET_1)
		if remainTimes <= 0 and ticketCount <= 0 then
			GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_1))
			return
		end
	elseif dungeonDailyType == GConst.DungeonConst.DUNGEON_DAILY_TYPE.MATERIAL then
		local remainTimes = DataManager.DungeonDailyData.MaterialData:getTodayRemainTimes()
		ticketCount = DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_DUNGEON_TICKET_2)
		if remainTimes <= 0 and ticketCount <= 0 then
			GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_1))
			return
		end
	else
		return
	end
	local itemGetType = dungeonDailyType == GConst.DungeonConst.DUNGEON_DAILY_TYPE.GOLD and BIReport.ITEM_GET_TYPE.ENTER_DUNGEON_DAILY_GOLD_BATTLE or BIReport.ITEM_GET_TYPE.ENTER_DUNGEON_DAILY_MATERIAL_BATTLE
	self:sendMessage(ProtoMsgType.FromMsgEnum.DungeonStartReq, params, self.onDungeonStartRsp, itemGetType)
end

-- 响应挑战金币副本
function DungeonManager:onDungeonStartRsp(result)
	if result.err_code ~= GConst.ERROR_STR.SUCCESS then
		return
	end
	DataManager.DungeonDailyData:setCurrChallengeChapterId(result.reqData.id)
	local dungeonDailyType = DataManager.DungeonDailyData:getDungeonDailyTypeByChapterId(result.reqData.id)
	ModuleManager.BattleManager:playBattle(GConst.BattleConst.BATTLE_TYPE.DUNGEON_GOLD, nil, function()
		UIManager:closeAllUI()
		ModuleManager.MaincityManager:showMainCityUI(GConst.MainCityConst.BOTTOM_PAGE.DUNGEON)
		ModuleManager.DungeonManager:showDungeonDaylyMainUI(dungeonDailyType)
	end)
	-- end, nil, {dungeonType = dungeonDailyType})
end

-- 请求结算金币副本
function DungeonManager:reqEndChallengeGold(chapterId, 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)

	local params = {
		id = chapterId,
		win = combatReport.victory,
		cheat = false,
		task_stat = taskProgress,
		combatReport = combatReport,
	}
	local dungeonDailyType = DataManager.DungeonDailyData:getDungeonDailyTypeByChapterId(chapterId)
	local itemGetType = dungeonDailyType == GConst.DungeonConst.DUNGEON_DAILY_TYPE.GOLD and BIReport.ITEM_GET_TYPE.FINISH_DUNGEON_DAILY_GOLD_BATTLE or BIReport.ITEM_GET_TYPE.FINISH_DUNGEON_DAILY_MATERIAL_BATTLE
	self:sendMessage(ProtoMsgType.FromMsgEnum.DungeonSettlementReq, params, self.respEndChallengeGold, itemGetType)
end

-- 响应结算金币副本
function DungeonManager:respEndChallengeGold(result)
	if result.err_code == GConst.ERROR_STR.BATTLE_CHEATER then
		ModuleManager.BattleManager:exitBattle()
		local params = {
			content = I18N:getGlobalText(I18N.GlobalConst.CHEAT_1),
			boxType = GConst.MESSAGE_BOX_TYPE.MB_OK,
			okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
		}
		GFunc.showMessageBox(params)
		return
	elseif result.err_code ~= GConst.ERROR_STR.SUCCESS then
		return
	end
	local chapterId = result.reqData.id
	local victory = result.reqData.win
	DataManager.DungeonDailyData:onFightDungeonDailyFinish(chapterId, victory)
	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

-- 请求扫荡金币副本
function DungeonManager:sweepDungeonDaily(id)
    local params = {
        id = id,
    }
    local dungeonDailyType = DataManager.DungeonDailyData:getDungeonDailyTypeByChapterId(id)
    if dungeonDailyType == GConst.DungeonConst.DUNGEON_DAILY_TYPE.GOLD then
        local remainTimes = DataManager.DungeonDailyData.GoldData:getTodayRemainTimes()
        local ticketCount = DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_DUNGEON_TICKET_1)
        if remainTimes <= 0 and ticketCount <= 0 then
            GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_1))
            return
        end
    elseif dungeonDailyType == GConst.DungeonConst.DUNGEON_DAILY_TYPE.MATERIAL then
        local remainTimes = DataManager.DungeonDailyData.MaterialData:getTodayRemainTimes()
        local ticketCount = DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_DUNGEON_TICKET_2)
        if remainTimes <= 0 and ticketCount <= 0 then
            GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_1))
            return
        end
    else
        return
    end
    local itemGetType = dungeonDailyType == GConst.DungeonConst.DUNGEON_DAILY_TYPE.GOLD and BIReport.ITEM_GET_TYPE.SWEEP_DUNGEON_DAILY_GOLD_BATTLE or BIReport.ITEM_GET_TYPE.SWEEP_DUNGEON_DAILY_MATERIAL_BATTLE
    self:sendMessage(ProtoMsgType.FromMsgEnum.DungeonSweepReq, params, self.onSweepDungeonDailyFinish, itemGetType)
end

-- 响应扫荡金币副本
function DungeonManager:onSweepDungeonDailyFinish(result)
    if result.err_code ~= GConst.ERROR_STR.SUCCESS then
        return
    end
    if result.rewards then
        GFunc.showRewardBox(result.rewards)
    end
    DataManager.DungeonDailyData:onSweepDungeonDailyFinish(result.reqData.id)
    ModuleManager.TaskManager:addTaskProgress(GConst.TaskConst.TASK_TYPE.CHALLENGE_DAILY_NUM_SINGLE, 1)
end

--region 上报
-- 日常副本
BIReport.ITEM_GET_TYPE.ENTER_DUNGEON_DAILY_GOLD_BATTLE = "enter_dungeon_daily_gold_battle" -- 进入金币副本
BIReport.ITEM_GET_TYPE.FINISH_DUNGEON_DAILY_GOLD_BATTLE = "finish_dungeon_daily_gold_battle" -- 结算金币副本
BIReport.ITEM_GET_TYPE.SWEEP_DUNGEON_DAILY_GOLD_BATTLE = "sweep_dungeon_daily_gold_battle" -- 扫荡金币副本
BIReport.ITEM_GET_TYPE.ENTER_DUNGEON_DAILY_MATERIAL_BATTLE = "enter_dungeon_daily_material_battle" -- 进入材料副本
BIReport.ITEM_GET_TYPE.FINISH_DUNGEON_DAILY_MATERIAL_BATTLE = "finish_dungeon_daily_material_battle" -- 结算材料副本
BIReport.ITEM_GET_TYPE.SWEEP_DUNGEON_DAILY_MATERIAL_BATTLE = "sweep_dungeon_daily_material_battle" -- 扫荡材料副本
--endregion

return DungeonManager