local UIPrefabObject = require "app/bf/unity/uiprefab_object"

local MainCityUI = class("MainCityUI", BaseUI)

local BF_UI_HELPER = GConst.TYPEOF_UNITY_CLASS.BF_UI_HELPER
local HERO_COMP = "app/ui/hero/hero_comp"
local MAIN_COMP = "app/ui/main_city/component/main_comp"
local SHOP_COMP = "app/ui/shop/shop_comp"
local DUNGEON_COMP = "app/ui/dungeon/dungeon_comp"
local COMPANY_COMP = "app/ui/company/company_comp"
local SIDE_BAR_COMP = "app/ui/main_city/component/side_bar_comp"

local BOTTOM_BTN_CELL = "app/ui/main_city/cell/bottom_btn_cell"

local MAIN_COMP_INEDX = 3

MainCityUI.CLICK_BTN_TYPE = {
	[1] = "HOME",
	[2] = "HERO",
	[3] = "SHOP",
	[4] = "COMPANY",
	[5] = "DUNGEON",
}

function MainCityUI:getUIType()
	return UIManager.UI_TYPE.MAIN
end

function MainCityUI:getBGMId()
	return AudioManager.BGM_ID.MAINCITY
end

function MainCityUI:getCurrencyParams()
	if self.currencyParams == nil then
		self.currencyParams = {
			itemIds = {}
		}
	end
	self.selectedIndex = self.selectedIndex or GConst.MainCityConst.BOTTOM_PAGE.MAIN
	self.currencyParams.showType = GConst.CURRENCY_TYPE.HORIZONTAL
	for k, v in ipairs(self.currencyParams.itemIds) do
		table.remove(self.currencyParams.itemIds)
	end
	if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.MAIN then
		self.currencyParams.itemIds[1] = GConst.ItemConst.ITEM_ID_GOLD
		self.currencyParams.itemIds[2] = GConst.ItemConst.ITEM_ID_GEM
		self.currencyParams.itemIds[3] = GConst.ItemConst.ITEM_ID_VIT
	elseif self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.HERO then
	elseif self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.COMPANY then
		-- self.currencyParams.itemIds[1] = GConst.ItemConst.ITEM_ID_GOLD
		-- self.currencyParams.itemIds[2] = GConst.ItemConst.ITEM_ID_VIT
	elseif self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.SHOP then
		self.currencyParams.itemIds[1] = GConst.ItemConst.ITEM_ID_GOLD
		self.currencyParams.itemIds[2] = GConst.ItemConst.ITEM_ID_GEM
		self.currencyParams.itemIds[3] = GConst.ItemConst.ITEM_ID_VIT
	elseif self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.DUNGEON then
		self.currencyParams.itemIds[1] = GConst.ItemConst.ITEM_ID_GOLD
		self.currencyParams.itemIds[2] = GConst.ItemConst.ITEM_ID_GEM
		self.currencyParams.itemIds[3] = GConst.ItemConst.ITEM_ID_VIT
	end
 	return self.currencyParams
end

function MainCityUI:onPressBackspace()
	if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.MAIN then
		local params = {
			content = I18N:getGlobalText(I18N.GlobalConst.CONF_BE_SURE_QUIT_GAME),
			okFunc = function()
				CS.UnityEngine.Application.Quit()
			end,
			boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL,
		}
		GFunc.showMessageBox(params)
	end
end

function MainCityUI:getPrefabPath()
	return "assets/prefabs/ui/main_city/main_ui.prefab"
end

function MainCityUI:ctor(params)
	self.isFirstEnter = params and params.isFirstEnter
	self.targetIndex = params and params.targetIndex
	self.selectedIndex = params and params.targetIndex or GConst.MainCityConst.BOTTOM_PAGE.MAIN
end

function MainCityUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	self.uiMap = uiMap
	self.bottomBgSelected = uiMap["main_ui.bottom_node.bottom_bg_selected"]
	self.bottomBgSelected:setLocalPositionX(0)

	self:_display()
	self:_addListeners()
	self:_bind()
	self:refreshBottomRp()

	self:updateTime()
	self:scheduleGlobal(function()
		self:updateTime()
	end, 1)
end

function MainCityUI:onRefresh()
	self:refreshSettingBtn()
end

function MainCityUI:onClose()
	self.subComps[GConst.MainCityConst.BOTTOM_PAGE.MAIN]:onClose()
	self.subComps = nil
end

function MainCityUI:onReshow()
	-- 检查目前没开的侧边栏功能是否有可以开启的
    if self.sideBarComp then
        self.sideBarComp:onReshow()
    end
end

function MainCityUI:onSetUIOrder()
	if self.subComps then
		for index, comp in pairs(self.subComps) do
			local order = self._baseRootCanvas.sortingOrder
			local uiHelper = comp:getGameObject():GetComponent(BF_UI_HELPER)
			if uiHelper then
				uiHelper:SetSortingOrder(order + 1)
			end
		end
	end
end

function MainCityUI:_display()
	self:initBottomUI()
	self:initComp()
	self:initTopNode()
	self:refreshBottom()
end

function MainCityUI:_addListeners()
	self:addEventListener(EventManager.CUSTOM_EVENT.TIME_TRIGGERED_NEW_EMAIL, function()
		local time = math.random(10000, 30000) / 20000
		ModuleManager.MailManager:performWithDelayGlobal(function()
			ModuleManager.MailManager:getTriggeredTimeMail()
		end, time)
	end)
	-- self:addEventListener(EventManager.CUSTOM_EVENT.TUTORIAL_TASK_STOP, function()
	--     -- 引导完成时 检测主界面的弹窗是否要触发
	--     if not self:checkTutorial(true) then
	--         self:checkGift()
	--     end
	-- end)
	self:addEventListener(EventManager.CUSTOM_EVENT.GO_SHOP, function(params)
		if ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.MALL, false) then
			local page = params and params.page or GConst.ShopConst.PAGE_TYPE.MAIN -- 默认主要商品
			if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.SHOP then
				self.subComps[GConst.MainCityConst.BOTTOM_PAGE.SHOP]:refresh(params.subType)
				return
			end
			-- self.bottomBtnSpines[GConst.MainCityConst.BOTTOM_PAGE.SHOP]:playAnimComplete("born1", false, false, function()
			-- 	-- self.bottomBtnSpines[GConst.MainCityConst.BOTTOM_PAGE.SHOP]:playAnimComplete("born2", false, false, function()
			-- 	-- 	-- self.bottomBtnSpines[GConst.MainCityConst.BOTTOM_PAGE.SHOP]:playAnim("idle", false, false)
			-- 	-- end)
			-- end)
			self:refreshBottom(GConst.MainCityConst.BOTTOM_PAGE.SHOP, true)
			if self.subComps and self.subComps[GConst.MainCityConst.BOTTOM_PAGE.SHOP] then
				self.subComps[GConst.MainCityConst.BOTTOM_PAGE.SHOP]:refresh(params.subType)
			end
		end
	end)

	self:addEventListener(EventManager.CUSTOM_EVENT.CHANGE_MAIN_COMP_MODULE, function(module)
		self:switchMainCompModule(module)
	end)

	self:addEventListener(EventManager.CUSTOM_EVENT.MAIN_UI_CHECK_SIDE_BAR, function(module)
		self:checkSideBarOpenStatus()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.MAIN_UI_CHECK_POP, function()
		self:checkMainPop()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.REFRESH_MAIN_CITY_BOTTOM, function(params)
        self:refreshBottomCell()
    end)
	self:addEventListener(EventManager.CUSTOM_EVENT.CHANGE_MAIN_CITY_PAGE, function(params)
		params = params or {}
		local page = params.page or self.selectedIndex
		if page == GConst.MainCityConst.BOTTOM_PAGE.SHOP then
			if self.selectedIndex == page then -- 直接跳转
				if self.subComps[GConst.MainCityConst.BOTTOM_PAGE.SHOP] then
					self.subComps[GConst.MainCityConst.BOTTOM_PAGE.SHOP]:refresh(params.subType)
				end
			end
		elseif page == GConst.MainCityConst.BOTTOM_PAGE.COMPANY then
			if params.companyIdx == 1 then
				ModuleManager.TalentManager:showMainUI()
			end
		-- elseif page == GConst.MainCityConst.BOTTOM_PAGE.DUNGEON then
		-- 	if self.selectedIndex == page then -- 直接跳转
		-- 		if self.dungeonComp then
		-- 			self.dungeonComp:refresh(params.dungeonModule)
		-- 		end
		-- 	end
		-- elseif page == GConst.MainCityConst.BOTTOM_PAGE.MAIN then
		-- 	if self.selectedIndex == page then -- 直接跳转
		-- 		if self.mainComp then
		-- 			-- 跳转到精英 需要改变下最大关卡
		-- 			if self.showChapterModule and self.showChapterModule == GConst.MainCityConst.MAIN_CHAPTER_MODULE.TEAM then
		-- 				local id = DataManager.ChapterTeamData:getLastChapterId(DataManager.ChapterData:getMaxPassId())
		-- 				id = id or 1
		-- 				DataManager.ChapterData:setChapterId(DataManager.ChapterTeamData:getChapterIdById(id))
		-- 			end
		-- 			self.mainComp:refresh(self.showChapterModule)
		-- 			self.showChapterModule = nil
		-- 		end
		-- 	end
		end
		self:refreshBottom(page, true)
	end)
	DataManager.MailData:checkNewMail()
end

function MainCityUI:_bind()
	self:bind(DataManager.ChapterData, "isDirty", function()
		if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.MAIN then
			if self.subComps[self.selectedIndex] then
				self.subComps[self.selectedIndex]:refreshChapter(true)
			end
		end
	end, true)
	self:bind(DataManager.PlayerData, "dirty", function(binder, value)
		self:refreshPlayerInfo()
	end, true)

	self:bind(DataManager.HeroData, "isDirty", function(binder, value)
		if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.HERO then
			if self.subComps[self.selectedIndex] then
				self.subComps[self.selectedIndex]:refresh(GConst.BattleConst.BATTLE_TYPE.STAGE)
			end
		end
		self:refreshBottomRp()
	end)
	self:bind(DataManager.HeroData, "isPowerDirty", function()
		ModuleManager.MaincityManager:checkShowPowerUI()
	end)

	self:bind(DataManager.CollectionData, "isDirty", function()
		self:refreshBottomRp()
	end)

	self:bind(DataManager.DailyTaskData, "isDirty", function()
		self:refreshTask()
	end)

	self:bind(DataManager.BountyData, "dirty", function()
		self:refreshBounty()
	end)

	self:bind(DataManager.MailData, "redPoint", function()
		self:refreshSettingBtn()
	end)

	self:bind(DataManager.TalentData, "isDirty", function()
		self:refreshBottomRp()
	end)

	self:bind(DataManager.ShopData, "isDirty", function()
		if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.SHOP and self.subComps[self.selectedIndex] then
			if self.subComps[self.selectedIndex] then
				self.subComps[self.selectedIndex]:refresh()
			end
		end
		self:refreshBottomRp()
	end)
	self:bind(DataManager.SummonData, "isDirty", function()
		if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.SHOP and self.subComps[self.selectedIndex] then
			if self.subComps[self.selectedIndex] then
				self.subComps[self.selectedIndex]:refresh()
			end
		end
	end)
	self:bind(DataManager.ArenaData, "isDirty", function()
		if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.MAIN then
			if self.subComps[self.selectedIndex] then
				self.subComps[self.selectedIndex]:refresh()
			end
		end
	end)
	self:bind(DataManager.ActTimeData, "isDirty", function()
        if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.MAIN then
            self.subComps[self.selectedIndex]:refresh()
        end
        self:checkMainPop()
        self:refreshSideBarComp()
    end)
	self:bind(DataManager.AIHelperData, "isDirty", function()
		self:refreshSettingBtn()
	end)
end

function MainCityUI:initBottomUI()
	local uiMap = self.root:genAllChildren()
	self.bottomBtnCells = {}
	self.bottomBtnSpines = {}
	for i = 1, 5 do
		local cellCom = CellManager:addCellComp(uiMap["main_ui.bottom_node.bottom_btn_cell_" .. i], BOTTOM_BTN_CELL)
		cellCom:addClickListener(function()
			if i == 1 then -- 商城有开启条件
				if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.MALL, false) then
					return
				end
			elseif i == 4 then -- 公司有开启条件
				if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.COMPANY_OPEN, false) then
					return
				end
			elseif i == 5 then -- 玩法有开启条件
				if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.DUNGEON_OPEN, false) then
					return
				end
			end
			if self.selectedIndex == i then
				return
			end

			if i <= #MainCityUI.CLICK_BTN_TYPE then
				BIReport:postHomeBtnCilck(BIReport.CLICK_BTN_TYPE[MainCityUI.CLICK_BTN_TYPE[i]])
			end

			-- self.bottomBtnSpines[i]:playAnimComplete("born1", false, false, function()
			-- 	-- self.bottomBtnSpines[i]:playAnimComplete("born2", false, false, function()
			-- 	-- 	-- self.bottomBtnSpines[i]:playAnim("idle", false, false)
			-- 	-- end)
			-- end)
			self:refreshBottom(i, true)
		end)
		table.insert(self.bottomBtnCells, cellCom)

		self.bottomBtnSpines[i] = uiMap["main_ui.bottom_node.icons.ui_spine_obj_" .. i]
		self.bottomBtnSpines[i]:playAnim("idle", false, false)
	end
end

function MainCityUI:initComp()
	if not self.subComps then
		local uiMap = self.root:genAllChildren()
		self.subComps = {}
		-- 主城
		local mainComp = uiMap["main_ui.sub_ui_node.main_comp"]
		mainComp:initPrefabHelper()
		mainComp:genAllChildren()
		self.mainComp = mainComp:addLuaComponent(MAIN_COMP)
		self.subComps[GConst.MainCityConst.BOTTOM_PAGE.MAIN] = self.mainComp
		-- 英雄
		local heroComp = uiMap["main_ui.sub_ui_node.hero_ui"]
		heroComp:initPrefabHelper()
		heroComp:genAllChildren()
		self.subComps[GConst.MainCityConst.BOTTOM_PAGE.HERO] = heroComp:addLuaComponent(HERO_COMP)
		self:onSetUIOrder()
		-- 商城
		local shopComp = uiMap["main_ui.sub_ui_node.shop_comp"]
		shopComp:initPrefabHelper()
		shopComp:genAllChildren()
		self.subComps[GConst.MainCityConst.BOTTOM_PAGE.SHOP] = shopComp:addLuaComponent(SHOP_COMP)

		-- 地牢
		local dungeonComp = uiMap["main_ui.sub_ui_node.dungeon_comp"]
		dungeonComp:initPrefabHelper()
		dungeonComp:genAllChildren()
		self.subComps[GConst.MainCityConst.BOTTOM_PAGE.DUNGEON] = dungeonComp:addLuaComponent(DUNGEON_COMP)

		-- 公司
		local companyComp = uiMap["main_ui.sub_ui_node.company_comp"]
		companyComp:initPrefabHelper()
		companyComp:genAllChildren()
		self.subComps[GConst.MainCityConst.BOTTOM_PAGE.COMPANY] = companyComp:addLuaComponent(COMPANY_COMP)
	end
end

function MainCityUI:refreshBottom(selectedIndex, playAnim)
	if self.targetIndex then
		selectedIndex = self.targetIndex
		playAnim = true
		self.targetIndex = nil
	end

	if selectedIndex and (not self.subComps[selectedIndex]) then
		return
	end
	local oldIndex = self.selectedIndex
	self.selectedIndex = selectedIndex and selectedIndex or GConst.MainCityConst.BOTTOM_PAGE.MAIN
	self.bottomBtnSpines[oldIndex]:playAnimComplete("born2", false, false)
	self.bottomBtnSpines[self.selectedIndex]:playAnimComplete("born1", false, false)
	self:switchComp()

	-- 动效
	if playAnim and (oldIndex ~= selectedIndex) then
		local targetX = self.bottomBtnCells[self.selectedIndex]:getCurLocalPosX()
		local isLeft = self.selectedIndex < oldIndex
		local offset = isLeft and -20 or 20
		GFunc.goTargetPosXShake(self.bottomBgSelected, nil, targetX, offset)
		self:refreshBottomCell(true)
	else
		self:refreshBottomCell(false)
	end
	self:refreshSideBarComp()
end

function MainCityUI:refreshSideBarComp()
    if self.sideBarComp == nil then
        local uiMap = self.root:genAllChildren()
        self.sideBarCompNode = uiMap["main_ui.side_bar_comp"]
        self.sideBarCompNode:initPrefabHelper()
        self.sideBarCompNode:genAllChildren()
        self.sideBarComp = self.sideBarCompNode:addLuaComponent(SIDE_BAR_COMP)
    end

    self.sideBarCompNode:setActive(self:getIsInMainPage())
    self.sideBarComp:refresh()
end

function MainCityUI:refreshBottomCell(showAni)
	for i, cell in ipairs(self.bottomBtnCells) do
		cell:refresh(i, i == self.selectedIndex, showAni)
	end
end

function MainCityUI:getIsInMainPage()
    return self.selectedIndex == MAIN_COMP_INEDX
end

function MainCityUI:refreshPlayerInfo()
	if not self.playerInfoCell then
		self.playerInfoCell = CellManager:addCellComp(self.uiMap["main_ui.player_info_cell"], GConst.TYPEOF_LUA_CLASS.PLAYER_INFO_CELL)
	end
	if self.selectedIndex ~= GConst.MainCityConst.BOTTOM_PAGE.MAIN then
		self.playerInfoCell:setActive(false)
		return
	end
	self.playerInfoCell:setActive(true)
	self.playerInfoCell:refresh()
end

function MainCityUI:initTopNode()
	self.topNode = self.uiMap["main_ui.top_node"]
	self:initTask()
	self:initBounty()
	self:initSetting()
end

function MainCityUI:initTask()
	self.taskBtn = self.uiMap["main_ui.top_node.task_btn"]
	self.taskBtn:addClickListener(function()
		ModuleManager.TaskManager:showTaskMainUI()
	end)
end

function MainCityUI:initBounty()
	self.bountyNode = self.uiMap["main_ui.top_node.bounty_node"]
	self.bountyBanner = self.uiMap["main_ui.top_node.bounty_node.banner"]
	self.bountyBanner:addClickListener(function()
		ModuleManager.BountyManager:showBountyMainUI()
	end)
	self.bountyRewardSpine = self.uiMap["main_ui.top_node.bounty_node.banner.spine"]
	self.bountyBannerInfo = self.uiMap["main_ui.top_node.bounty_node.banner.info"]
	self.bountyBannerDescTx = self.uiMap["main_ui.top_node.bounty_node.banner.desc"]
	self.bountyBannerSlider = self.uiMap["main_ui.top_node.bounty_node.banner.info.progress_bg.slider"]
	self.bountyBannerSliderTx = self.uiMap["main_ui.top_node.bounty_node.banner.info.progress_bg.slider_tx"]
	self.bountyBannerLvTx = self.uiMap["main_ui.top_node.bounty_node.banner.info.progress_bg.lv_tx"]
	self.uiMap["main_ui.top_node.bounty_node.banner.info.name"]:setText(I18N:getGlobalText(I18N.GlobalConst.BOUNTY_TITLE))
end

function MainCityUI:initSetting()
	self.settingBtn = self.uiMap["main_ui.top_node.setting_btn"]
	self.settingBtn:addClickListener(function()
		ModuleManager.GameSettingManager:showSelectOtherBtnUI()
	end)
end

function MainCityUI:setTopNodeVisible(visible)
	self.topNode:setVisible(visible)
end

function MainCityUI:refreshTopNode()
	self:refreshBounty()
	self:refreshTask()
end

function MainCityUI:refreshBounty()
	-- local isOpen = DataManager.BountyData:isOpen()
	-- if not isOpen or GFunc.isShenhe() then
	-- 	self.bountyNode:setVisible(false)
	-- 	self.bountyRewardSpine:getSkeletonGraphic().enabled = false
	-- 	return
	-- end
	-- self.bountyNode:setVisible(true)
	-- if DataManager.BountyData:getIfCanClaimReward() then
	-- 	self.bountyRewardSpine:setVisible(true)
	-- 	self.bountyRewardSpine:playAnim("idle", true)
	-- 	self.bountyRewardSpine:getSkeletonGraphic().enabled = true
	-- 	self.bountyBannerInfo:setVisible(false)
	-- 	self.bountyBannerDescTx:setText(I18N:getGlobalText(I18N.GlobalConst.COLLET_REWARDS))
	-- else
	-- 	self.bountyRewardSpine:setVisible(false)
	-- 	self.bountyRewardSpine:getSkeletonGraphic().enabled = false
	-- 	local bannerName = DataManager.BountyData:getBannerName()
	-- 	self.bountyBanner:setSprite(GConst.ATLAS_PATH.BOUNTY, bannerName)
	-- 	self.bountyBannerInfo:setVisible(true)
	-- 	self.bountyBannerDescTx:setText(GConst.EMPTY_STRING)
	-- 	local exp = DataManager.BountyData:getExp()
	-- 	local lvUpExp = DataManager.BountyData:getLvUpExp()
	-- 	local level = DataManager.BountyData:getLevel()
	-- 	local maxLevel = DataManager.BountyData:getMaxLevel()
	-- 	self.bountyBannerSliderTx:setText(exp .. "/" .. lvUpExp)
	-- 	self.bountyBannerSlider:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = exp / lvUpExp
	-- 	if level >= maxLevel then
	-- 		self.bountyBannerLvTx:setText(I18N:getGlobalText(I18N.GlobalConst.STR_MAX))
	-- 	else
	-- 		self.bountyBannerLvTx:setText(tostring(level))
	-- 	end
	-- end
end

function MainCityUI:refreshArenaBounty()
	if self.mainComp then
		self.mainComp:refreshArenaBounty()
	end
end

function MainCityUI:refreshTask()
	local isOpen = DataManager.DailyTaskData:getIsOpen()
	if not isOpen then
		self.taskBtn:setVisible(false)
		return
	end
	self.taskBtn:setVisible(true)
	local showRedPoint = DataManager.DailyTaskData:showRedPoint()
	if showRedPoint then
		self.taskBtn:addRedPoint(28, 36, 1)
	else
		self.taskBtn:removeRedPoint()
	end
end

function MainCityUI:switchComp(index)
	index = index or self.selectedIndex
	for i, comp in pairs(self.subComps) do
		comp:getBaseObject():setActive(i == index)
		if i == index then
			if index == GConst.MainCityConst.BOTTOM_PAGE.SHOP then
				comp:refresh()
			elseif index == GConst.MainCityConst.BOTTOM_PAGE.HERO then
				comp:refresh(GConst.BattleConst.BATTLE_TYPE.STAGE)
			else
				comp:refresh()
			end

			self:updateTime()
		end
	end
	self:updateCurrencyBar()
	if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.MAIN then
		self:refreshTopNode()
		self:switchMainCompModule()
	else
		self:setTopNodeVisible(false)
	end
	self:refreshPlayerInfo()
end

-- 切换主界面活动模块
function MainCityUI:switchMainCompModule(moduleKey)
	if self.selectedIndex ~= GConst.MainCityConst.BOTTOM_PAGE.MAIN then
		return
	end
	self.mainComp:refresh()
	self:checkMainPop()
	self:setTopNodeVisible(true)
	self:refreshBounty()
	UIManager:updateBarsState(self)
end

function MainCityUI:updateTime()
	if self.subComps[self.selectedIndex] and self.subComps[self.selectedIndex].refreshTime then
		self.subComps[self.selectedIndex]:refreshTime()
	end
    if self.sideBarComp then
        self.sideBarComp:updateTime()
    end

	if DataManager.AIHelperData:getNeedReqTime() then
		ModuleManager.GameSettingManager:reqAiHelper()
	end
end

function MainCityUI:refreshBottomRp()
	local uiMap = self.root:genAllChildren()
	local heroRpObj = uiMap["main_ui.bottom_node.icons.ui_spine_obj_2.rp_node"]
	if DataManager.HeroData:getRp() then
		heroRpObj:addRedPoint(0, 0, 1)
	else
		heroRpObj:removeRedPoint()
	end
	local shopRpObj = uiMap["main_ui.bottom_node.icons.ui_spine_obj_3.rp_node"]
	if DataManager.ShopData:showRedPoint() then
		shopRpObj:addRedPoint(0, 0, 1)
	else
		shopRpObj:removeRedPoint()
	end
	local companyRpObj = uiMap["main_ui.bottom_node.icons.ui_spine_obj_4.rp_node"]
	if DataManager.TalentData:showRedPoint() then
		companyRpObj:addRedPoint(0, 0, 1)
	else
		companyRpObj:removeRedPoint()
	end
end

function MainCityUI:refreshSettingBtn()
	if (DataManager.MailData:getIsOpen() and DataManager.MailData:getRedPoint()) or DataManager.AIHelperData:getRp() then
		self.settingBtn:addRedPoint(32, 36, 1)
	else
		self.settingBtn:removeRedPoint()
	end
end

-- 弹窗优先级： 升级>功能弹窗>英雄解锁弹窗>礼包弹窗>引导
function MainCityUI:checkMainPop()
    local topUI = UIManager:getTopUIObj()
    if topUI:getUIIndex() ~= self:getUIIndex() then
        if topUI:getUIIndex() == UIManager.UI_PATH.REWARD_BOX then
            topUI:addExitAniCompleteListener(function()
                EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.MAIN_UI_CHECK_POP)
            end)
        end
        return
    end
	-- 续关
	if self.isFirstEnter then
		local battleSnapshot = LocalData:getBattleSnapshot()
		if battleSnapshot.battleType then
			if battleSnapshot.currentVersion == Platform:getClientVersion() then
				local snapshotTime = battleSnapshot.snapShotTime
				if snapshotTime and Time:getDayOverTimeStamp(snapshotTime) >= Time:getDayOverTimeStamp() then
					BIReport:postBatttleSnapshotOpen()
					local params = {
						content = I18N:getGlobalText(I18N.GlobalConst.BATTLE_DESC_14),
						okFunc = function()
							LocalData:saveBattleSnapshot({})
							ModuleManager.BattleManager:playBattle(battleSnapshot.battleType, battleSnapshot.parmas, nil, battleSnapshot)
						end,
						cancelFunc = function()
							LocalData:saveBattleSnapshot({})
							self:checkMainPop()
						end,
						boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL,
					}
					GFunc.showMessageBox(params)
					return
				end
			end
		end
	end

	if self.selectedIndex ~= GConst.MainCityConst.BOTTOM_PAGE.MAIN then
		return
	end

	-- 检查是否升级
	if DataManager.PlayerData:getIfCanLevelUp() then
		ModuleManager.PlayerManager:levelUp()
		return
	end
	-- 功能解锁
    local showFuncList = ModuleManager.MaincityManager:getFuncOpenShowList()
    if showFuncList and #showFuncList > 0 and not GFunc.isShenhe() then
        ModuleManager.MaincityManager:showModuleUnlockUI(showFuncList)
        EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.REFRESH_MAIN_CITY_BOTTOM)
        return
    end

	-- 引导
	if self:checkTutorial() then
		return
	end

	-- 首充
	if ModuleManager.ActivityPopManager:checkFirstRechargePop() then
		return
	end

	-- 礼包
	-- if self:checkGift() then
	-- 	return
	-- end

	-- local actPopShowFlag = (not self.popSkipFlag) or DataManager.ActivityData:getTheDayFirstLogin()
	-- if self.selectedInedx == GConst.MainCityConst.BOTTOM_PAGE.MAIN and not GFunc.isShenhe() then
	-- 	if not DataManager.TutorialData:getIsInTutorial() and actPopShowFlag then
	-- 		if ModuleManager.ActivityPopManager:checkActivityPop() then
	-- 			return
	-- 		end
	-- 	end
	-- end
end

-- 检查引导
function MainCityUI:checkTutorial(onlyCheck)
	if (EDITOR_MODE or not Platform:getIsPublishChannel()) and LocalData:getTutorialSkip() > 0 then
		return
	end
	if DataManager.ChapterData:getMaxChapterId() == 1 then
		if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.PASS_ONE_CHAPTER, onlyCheck) then
			return true
		end
	end

	if not DataManager.TutorialData:getIsFuncTutorialFinished2(GConst.TutorialConst.TALENT_OPEN) and DataManager.TalentData:getIsOpen() then
		if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.TALENT_OPEN, onlyCheck) then
			return true
		end
	end

	if DataManager.ChapterData:getMaxChapterId() == 2 then
		if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.PASS_THREE_CHAPTER, onlyCheck) then
			return true
		end
	end

	if not DataManager.TutorialData:getIsFuncTutorialFinished2(GConst.TutorialConst.UNLOCK_SUMMON) and DataManager.SummonData:getIsOpen() then
		if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.UNLOCK_SUMMON, onlyCheck) then
			return true
		end
	end

	if not DataManager.TutorialData:getIsFuncTutorialFinished2(GConst.TutorialConst.DAILY_CHALLENGE_OPEN) and DataManager.DailyChallengeData:isOpen() then
		if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.DAILY_CHALLENGE_OPEN, onlyCheck) then
			return true
		end
	end

	-- if DataManager.DungeonData:isOpenAnyone() then
	-- 	if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.UNLOCK_DUNGEON, onlyCheck) then
	-- 		return true
	-- 	end
	-- end

	if not DataManager.TutorialData:getIsFuncTutorialFinished2(GConst.TutorialConst.UNLOCK_ARENA) and DataManager.ArenaData:isOpen() then
		if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.UNLOCK_ARENA, onlyCheck) then
			return true
		end
	end
end

-- 检查礼包(首充(作废)/入门/章节/新手/助力/成长/金币顺序)
function MainCityUI:checkGift()
	local showType = BIReport.PAY_UI_SHOW_TYPE.TRIGGER_POP
	if self.isFirstEnter then
		showType = BIReport.PAY_UI_SHOW_TYPE.LOGIN_POP
	end
	-- 审核模式不弹礼包
	if GFunc.isShenhe() then
		return
	end
	-- 引导其间不处理
	if DataManager.TutorialData:getIsHaveTutorial() then
		return
	end
	-- 非主线章节界面不处理（只在主线章节也就是主界面弹）
	if self.selectedIndex ~= GConst.MainCityConst.BOTTOM_PAGE.MAIN then
		return
	end
	-- 章节礼包
	local chapterPopUpGifts = DataManager.ShopData:getPopUpGiftByType(PayManager.PURCHARSE_TYPE.CHAPTER_GIFT)
	-- 通用礼包
	local summerGiftIds
	local beginnerGiftIds
	local levelUpGiftIds
	local firstRechargeIds
	local introductGiftIds
	local actPopUpGifts = DataManager.ShopData:getPopUpGiftByType(PayManager.PURCHARSE_TYPE.ACT_GIFT)
	if actPopUpGifts and #actPopUpGifts > 0 then
		-- 先遍历找出符合弹出的类型
		for _, actId in ipairs(actPopUpGifts) do
			local cfgInfo = DataManager.ShopData:getActGiftConfig()[actId]
			if cfgInfo then
				-- 夏日活动礼包
				if cfgInfo.type == PayManager.PURCHARSE_ACT_TYPE.ACT_SUMMER then
					if not summerGiftIds then
						summerGiftIds = {}
					end
					table.insert(summerGiftIds, actId)
				end
				-- 新手礼包
				if cfgInfo.type == PayManager.PURCHARSE_ACT_TYPE.BEGINNER_GIFT then
					if not beginnerGiftIds then
						beginnerGiftIds = {}
					end
					table.insert(beginnerGiftIds, actId)
				end
				-- 助力礼包
				if cfgInfo.type == PayManager.PURCHARSE_ACT_TYPE.LEVEL_UP_GIFT then
					if not levelUpGiftIds then
						levelUpGiftIds = {}
					end
					table.insert(levelUpGiftIds, actId)
				end
				-- 首充礼包
				if cfgInfo.type == PayManager.PURCHARSE_ACT_TYPE.FIRST_RECHARGE then
					if not firstRechargeIds then
						firstRechargeIds = {}
					end
					table.insert(firstRechargeIds, actId)
				end
				-- 入门礼包
				if cfgInfo.type == PayManager.PURCHARSE_ACT_TYPE.INTRODUCTORY_GIFT then
					if not introductGiftIds then
						introductGiftIds = {}
					end
					table.insert(introductGiftIds, actId)
				end
			end
		end
	end
	-- 特殊的 成长礼包
	local growUpPopUpGifts = DataManager.ShopData:getPopUpGiftByType(PayManager.PURCHARSE_TYPE.GROW_UP_GIFT_NEW)
	-- 按顺序弹出(首充/章节/新手/助力/成长/金币顺序)
	-- 首充礼包
	-- if firstRechargeIds and #firstRechargeIds > 0 then
	--     ModuleManager.ShopManager:triggerGiftPopUI(PayManager.PURCHARSE_TYPE.ACT_GIFT, firstRechargeIds[1])
	--     return true
	-- end
	-- 夏日礼包在最前
	if summerGiftIds and #summerGiftIds > 0 then
		ModuleManager.ShopManager:triggerGiftPopUI(PayManager.PURCHARSE_TYPE.ACT_GIFT, summerGiftIds[1], showType)
		return true
	end
	-- 入门礼包
	if introductGiftIds and #introductGiftIds > 0 then
		ModuleManager.ShopManager:triggerGiftPopUI(PayManager.PURCHARSE_TYPE.ACT_GIFT, introductGiftIds[1], showType)
		return true
	end
	---- 试玩弹窗在章节礼包前
	local heroId, chapterId = DataManager.ChapterData:getCacheTrialHeroIdAndChapterId()
	if heroId and chapterId then
		ModuleManager.ChapterManager:showHeroTrialUI(heroId, chapterId)
		return true
	end
	-- 章节礼包
	if chapterPopUpGifts and #chapterPopUpGifts > 0 then
		ModuleManager.ShopManager:triggerGiftPopUI(PayManager.PURCHARSE_TYPE.CHAPTER_GIFT, chapterPopUpGifts[1], showType)
		return true
	end
	-- 新手礼包
	if beginnerGiftIds and #beginnerGiftIds > 0 then
		ModuleManager.ShopManager:triggerGiftPopUI(PayManager.PURCHARSE_TYPE.ACT_GIFT, beginnerGiftIds[1], showType)
		return true
	end
	-- 助力礼包
	if levelUpGiftIds and #levelUpGiftIds then
		ModuleManager.ShopManager:triggerGiftPopUI(PayManager.PURCHARSE_TYPE.ACT_GIFT, levelUpGiftIds[1], showType)
		return true
	end
	-- 成长礼包
	if growUpPopUpGifts and #growUpPopUpGifts > 0 then
		ModuleManager.ShopManager:triggerGiftPopUI(PayManager.PURCHARSE_TYPE.GROW_UP_GIFT_NEW, growUpPopUpGifts[1], showType)
		return true
	end
end

function MainCityUI:getTaskIconPos()
	return self.taskBtn:getPosition()
end

function MainCityUI:getMallIconPos()
	return self.bottomBtnSpines[GConst.MainCityConst.BOTTOM_PAGE.SHOP]:getPosition()
end

function MainCityUI:getDailyChallengeIconPos()
	return self.subComps[GConst.MainCityConst.BOTTOM_PAGE.MAIN]:getDailyChallengeBtnPosition()
end

function MainCityUI:getArenaIconPos()
	return self.subComps[GConst.MainCityConst.BOTTOM_PAGE.MAIN]:getArenaBtnPosition()
end

function MainCityUI:getIdleIconPos()
	return self.subComps[GConst.MainCityConst.BOTTOM_PAGE.MAIN]:getIdleBtnPosition()
end

function MainCityUI:getSummonIconPos()
	return self.subComps[GConst.MainCityConst.BOTTOM_PAGE.MAIN]:getSummonBtnPosition()
end

function MainCityUI:getSideBarActIconPos(moduleKey)
    if self.sideBarComp and self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.MAIN then
        return self.sideBarComp:getSideBarActIconPos(moduleKey)
    end
end

function MainCityUI:getHeroComp()
	return self.subComps[GConst.MainCityConst.BOTTOM_PAGE.HERO]
end

return MainCityUI