local MaincityManager = class("MaincityManager", BaseModule)

function MaincityManager:showMainCityUI(isFirstEnter, targetIndex)
	UIManager:showUI(UIManager.UI_PATH.MAINCITY_UI, {isFirstEnter = isFirstEnter, targetIndex = targetIndex})
end

function MaincityManager:showModuleUnlockUI()
	local chapterId = DataManager.PlayerData:getModuleUnlockChapter()
	if chapterId <= 0 then
		return
	end
	DataManager.PlayerData:markShowModuleUnlock()
	UIManager:showUI("app/ui/main_city/module_unlock_ui", {chapterId = chapterId})
end

-- 从登录界面第一次进入主城
function MaincityManager:firstEnterMainCity()
	if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.START_TUTORIAL, true) then
		DataManager.ChapterData:setChapterId(DataManager.ChapterData.MIN_CHAPTER_ID)
		ModuleManager.BattleManager:playBattle(GConst.BattleConst.BATTLE_TYPE.STAGE)
	else
		self:showMainCityUI(true)
	end
	SDKManager:doUncompletePay()
end

function MaincityManager:changeMainCityLeftSideBarOpenOrClose()
	local mainUI = UIManager:getUIByIndex(UIManager.UI_PATH.MAINCITY_UI)
	if not mainUI then
		return nil
	end

	self.isLeftSideBarClose = not self.isLeftSideBarClose
	mainUI:refreshLeftBtns()
end

function MaincityManager:getIsMainCityLeftSideBarClose()
	return self.isLeftSideBarClose
end

function MaincityManager:changeMainCityRightSideBarOpenOrClose()
	local mainUI = UIManager:getUIByIndex(UIManager.UI_PATH.MAINCITY_UI)
	if not mainUI then
		return nil
	end

	self.isRightSideBarClose = not self.isRightSideBarClose
	mainUI:refreshRightBtns()
end

function MaincityManager:getIsMainCityRightSideBarClose()
	return self.isRightSideBarClose
end

-- 获取主界面各个模块的入口坐标
function MaincityManager:getModuleEntrancePos(moduleKey)
	local mainUI = UIManager:getUIByIndex(UIManager.UI_PATH.MAINCITY_UI)
	if not mainUI then
		return nil
	end

	if moduleKey == ModuleManager.MODULE_KEY.TASK then
		return mainUI:getTaskIconPos()
	elseif moduleKey == ModuleManager.MODULE_KEY.MALL_DAILY then
		return mainUI:getMallIconPos()
	elseif moduleKey == ModuleManager.MODULE_KEY.DAILY_CHALLENGE then
		return mainUI:getDailyChallengeIconPos()
	elseif moduleKey == ModuleManager.MODULE_KEY.ARENA then
		return mainUI:getArenaIconPos()
	elseif moduleKey == ModuleManager.MODULE_KEY.FUND or
			moduleKey == ModuleManager.MODULE_KEY.IDLE_DROP or
			moduleKey == ModuleManager.MODULE_KEY.SEVEN_DAY then
		return mainUI:getSideBarActIconPos(moduleKey)
	end
end

-- 模块在侧边栏，-1：左，0：不在侧边栏，1：右
function MaincityManager:getModuleInSideBarWhere(moduleKey)
	local mainUI = UIManager:getUIByIndex(UIManager.UI_PATH.MAINCITY_UI)
	if not mainUI then
		return nil
	end

	if mainUI:isInSideBarLeft(moduleKey) then
		return -1
	elseif mainUI:isInSideBarRight(moduleKey) then
		return 1
	else
		return 0
	end
end

function MaincityManager:isActivSideBarModule(moduleKey)
	local mainUI = UIManager:getUIByIndex(UIManager.UI_PATH.MAINCITY_UI)
	if not mainUI then
		return nil
	end

	return mainUI:isActivSideBarModule(moduleKey)
end

function MaincityManager:setCurModule(moduleKey)
	self.curModule = moduleKey
end

function MaincityManager:getCurModule()
	if not self.curModule then
		-- 默认进主线章节
		return GConst.MainCityConst.MAIN_MODULE.CHAPTER
	end
	return self.curModule
end

return MaincityManager