local ModuleManager = {}

local MODULE_PATHS = {
	-- 登录
	LoginManager = "app/module/login/login_manager",
	TipsManager = "app/module/tips/tips_manager",
	LoadingManager = "app/module/loading/loading_manager",
	-- GM
	DevToolManager = "app/module/gm/dev_tool_manager",
	-- 主城
	MaincityManager = "app/module/maincity/maincity_manager",
	-- 引导
	TutorialManager = "app/module/tutorial/tutorial_manager",
	ToastManager = "app/ui/common/toast",
	TaskToastManager = "app/ui/common/task_toast",
	-- 任务
	TaskManager = "app/module/task/task_manager",
	-- 战斗
	BattleManager = "app/module/battle/battle_manager",
	-- 战斗阵容
	FormationManager = "app/module/formation/formation_manager",
	-- 英雄
	HeroManager = "app/module/hero/hero_manager",
	-- 主线关卡
	ChapterManager = "app/module/chapter/chapter_manager",
	-- 每日挑战
	DailyChallengeManager = "app/module/daily_challenge/daily_challenge_manager",
	-- 挂机
	IdleManager = "app/module/idle/idle_manager",
	-- 七天乐
	SevenDayManager = "app/module/activity/seven_day/seven_day_manager",
	-- 基金
	FundManager = "app/module/fund/fund_manager",
	-- 设置
	GameSettingManager = "app/module/game_setting/game_setting_manager",
	-- 活动
	ActivityManager = "app/module/activity/activity_manager",
	-- 礼包
	CommerceManager = "app/module/commerce/commerce_manager",
	-- 战令
	BountyManager = "app/module/bounty/bounty_manager",
	-- 竞技场战令
	ArenaBountyManager = "app/module/arena/arena_bounty_manager",
	-- 道具
	ItemManager = "app/module/item/item_manager",
	-- 商店
	ShopManager = "app/module/shop/shop_manager",
	-- 召唤
	SummonManager = "app/module/summon/summon_manager",
	-- 邮件
	MailManager = "app/module/mail/mail_manager",
	-- 玩家
	PlayerManager = "app/module/player/player_manager",
	-- 账号
	AccountManager= "app/module/account/account_manager",
	-- 活动副本
	DungeonManager = "app/module/dungeon/dungeon_manager",
	-- 竞技场
	ArenaManager = "app/module/arena/arena_manager",
	-- 图鉴
	CollectionManager = "app/module/collection/collection_manager",
	-- 通用
	CommonManager = "app/module/common/common_manager",
	-- 武器副本
	DungeonWeaponManager = "app/module/dungeon_weapon/dungeon_weapon_manager",
	-- 防具副本
	DungeonArmorManager = "app/module/dungeon_armor/dungeon_armor_manager",
	-- 装备
	EquipManager = "app/module/equip/equip_manager",
	-- 皮肤
	SkinManager = "app/module/skin/skin_manager",
	-- 皮肤
	RunesManager = "app/module/runes/runes_manager",
	-- 英雄基金
	HeroFundManager = "app/module/activity/hero_fund/hero_fund_manager",
	-- 世界首领活动
	ActBossRushManager = "app/module/activity/act_boss_rush/act_boss_rush_manager",
	-- 新手14天乐
	FourteenDayManager = "app/module/activity/fourteen_day/fourteen_day_manager",
	-- 圆月活动
	FullMoonManager = "app/module/activity/full_moon/full_moon_manager",
	-- 波次锦标赛活动
	TournWaveManager = "app/module/activity/tourn_wave/tourn_wave_manager",
	-- 竞技场锦标赛活动
	TournArenaManager = "app/module/activity/tourn_arena/tourn_arena_manager",
	-- 符文副本
	DungeonRuneManager = "app/module/dungeon_rune/dungeon_rune_manager",
	-- 梦魇酒馆
	ActPvpManager = "app/module/activity/act_pvp/act_pvp_manager",
}

-- 这里的key对应func_open里的id
ModuleManager.MODULE_KEY = {
	GOLD_PIG = "act_gold_pig",
	SEVEN_DAY = "act_sevenday",
	TASK = "task",
	STORE_BOX_OPEN = "store_box_open",
	STORE_BOX_3_OPEN = "store_box_3_open",
	BOUNTY_OPEN = "bounty_open",
	IDLE_DROP = "idle_drop",
	ACTIVITY = "activity_open",
	MALL = "mall", -- 商城
	MALL_DAILY = "mall_daily", -- 每日商城
	DAILY_CHALLENGE = "daily_challenge", -- 每日挑战
	FUND = "act_level_gift", -- 成长基金
	ACT_GIFT_SHOW_OPEN = "act_gift_show_open", -- 弹窗礼包通用开启条件
	FIRST_RECHARGE = "first_charge", -- 首充礼包
	BEGINNER_GIFT = "new_player_gift", -- 新手礼包
	MAIL = "mail_open", -- 邮件
	DUNGEON_SHARDS = "dungeon_shards_open", -- 碎片副本
	DUNGEON_GOLD = "dungeon_gold_open", -- 金币副本
	INTRODUCT_GIFT = "introduct_gift", -- 入门礼包
	ARENA = "arena_open", -- 竞技场
	ARENA_GIFT = "act_arena_gift", -- 竞技场礼包
	COLLECT = "collection_open", -- 收集
	DUNGEON_ARMOR = "dungeon_armor_open", -- 支线副本
	DUNGEON_WEAPON = "dungeon_weapon_open", -- 装备副本
	EQUIP_WEAPON = "equip_weapon_open", -- 武器
	EQUIP_ARMOR = "equip_armor_open", -- 防具
	SKIN = "skin_open", -- 皮肤
	RUNES_OPEN = "runes_open", -- 符文
}

local _moduleMgrs = {}

local MODULE_METATABLE = {
	__index = function(t, k)
		local path = MODULE_PATHS[k]
		if path == nil then
			Logger.logError("%s path is not configure in ModuleManager.lua", k)
			return
		end
		local v = require(path):create()
		table.insert(_moduleMgrs, v)
		rawset(t, k, v)
		return v
	end
}
setmetatable(ModuleManager, MODULE_METATABLE)

function ModuleManager:init()
	ModuleManager.TaskManager:init()
	ModuleManager.DailyChallengeManager:init()
	ModuleManager.PlayerManager:init()
	ModuleManager.SkinManager:init()
	ModuleManager.TournArenaManager:init()
end

-- 功能是否开启
function ModuleManager:getIsOpen(key, hideToast)
    local cfg = ConfigManager:getConfig("func_open")[key]
    if cfg == nil then
		return true
	end
	-- 优先判断等级
	if cfg.level then
		local isOpen = DataManager.PlayerData:getLv() >= cfg.level
		if not hideToast and not isOpen then
			GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.FUNC_OPEN_LEVEL, cfg.level))
		end
		return isOpen
	elseif cfg.stage then -- 没有填等级字段就判断关卡
		local isOpen = DataManager.ChapterData:getMaxChapterId() >= cfg.stage
		if not hideToast and not isOpen then
			GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.FUNC_OPEN_STAGE, cfg.stage))
		end
		return isOpen
	end
    return true
end

function ModuleManager:showPop(key)
    local cfg = ConfigManager:getConfig("func_open")[key]
	if not cfg then
		return false
	end
    if cfg and cfg.pop_ups and cfg.pop_ups == 1 then
		return false
	end
	return true
end

function ModuleManager:getNotOpenStr(key)
	local cfg = ConfigManager:getConfig("func_open")[key]
    if cfg == nil then
		return GConst.EMPTY_STRING
	end
	-- 优先判断等级
	if cfg.level then
		return I18N:getGlobalText(I18N.GlobalConst.FUNC_OPEN_LEVEL, cfg.level)
	elseif cfg.stage then -- 没有填等级字段就判断关卡
		return I18N:getGlobalText(I18N.GlobalConst.FUNC_OPEN_STAGE, cfg.stage)
	end
    return GConst.EMPTY_STRING
end

function ModuleManager:getOpenStageId(key)
    local cfg = ConfigManager:getConfig("func_open")[key]
    if not cfg or cfg.level then
		return
	end

	return cfg.stage
end

function ModuleManager:clear()
	for k, v in ipairs(_moduleMgrs) do
		v:_clear()
	end
end

return ModuleManager
