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

local MainCityUI = class("MainCityUI", BaseUI)

local SIDE_BAR_BORDER_OFFSET = 22
local SIDE_BAR_INTERVAL = 28
local SIDE_BAR_ARROW_INTERVAL = 20

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 BOTTOM_BTN_CELL = "app/ui/main_city/cell/bottom_btn_cell"

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

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
        if self.mainComp and self.mainComp:getCurModuleType() == GConst.MainCityConst.MAIN_MODULE.ARENA then
            self.currencyParams.itemIds[1] = GConst.ItemConst.ITEM_ID_GEM
            self.currencyParams.itemIds[2] = GConst.ItemConst.ITEM_ID_ARENA_TICKET
        else
            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
	elseif self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.HERO 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
	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

    print("当前 Lua 版本: ", _VERSION)  -- 输出：Lua 5.1
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:updateSideBarStatus()
        self:updateTime()
	end, 1)
end

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

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

function MainCityUI:refreshAllSideBars()
    self:refreshLeftBtns()
    self:refreshRightBtns()
    self:updateSideBarStatus()
end

function MainCityUI:onReshow()
    self:checkMainPop()
    self:checkSideBarOpenStatus()
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:initLeftRightBtns()
    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 -- 默认主要商品
            local subType = params.subType
            if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.SHOP then
                if self.subComps and self.subComps[GConst.MainCityConst.BOTTOM_PAGE.SHOP] then
                    self.subComps[GConst.MainCityConst.BOTTOM_PAGE.SHOP]:switchPage(page)
                    if page == GConst.ShopConst.PAGE_TYPE.MAIN and subType then
                        self.subComps[GConst.MainCityConst.BOTTOM_PAGE.SHOP]:goSubType(subType)
                    end
                end
                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]:switchPage(page)
                if page == GConst.ShopConst.PAGE_TYPE.MAIN and subType then
                    self.subComps[GConst.MainCityConst.BOTTOM_PAGE.SHOP]:goSubType(subType)
                end
            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)
    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.FormationData, "dirty", function(binder, value)
        if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.HERO then
            self.subComps[self.selectedIndex]:refresh(GConst.BattleConst.BATTLE_TYPE.STAGE)
        end
    end)

	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
        elseif self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.MAIN then
            if self.subComps[self.selectedIndex] then
                self.subComps[self.selectedIndex]:refreshStageFormaion()
            end
        end
        self:refreshBottomRp()
    end)

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

    self:bind(DataManager.BagData.ItemData, "dirty", function(binder, value)
        if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.MAIN and self.subComps[self.selectedIndex] then
            if self.subComps[self.selectedIndex] then
                self.subComps[self.selectedIndex]:refreshStageFormaion()
                self.subComps[self.selectedIndex]:refreshBtns()
            end
        end
    end)

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

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

    self:bind(DataManager.MailData, "redPoint", function()
        self:refreshSettingBtn()
	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.DailyChallengeData, "isDirty", function()
        if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.MAIN then
            if self.subComps[self.selectedIndex] then
                self.subComps[self.selectedIndex]:refreshChallenge()
            end
        end
    end)

    self:bind(DataManager.DungeonData, "isDirty", function()
        if self.selectedIndex == GConst.MainCityConst.BOTTOM_PAGE.MAIN then
            if self.subComps[self.selectedIndex] then
                self.subComps[self.selectedIndex]:refreshDungeon()
            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]:refreshArena()
            end
        end
    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, 3 do
        local cellCom = CellManager:addCellComp(uiMap["main_ui.bottom_node.bottom_btn_cell_" .. i], BOTTOM_BTN_CELL)
        cellCom:addClickListener(function()
            if i == 3 then -- 商城有开启条件
                if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.MALL, 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)
    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:switchComp()

    if oldIndex ~= selectedIndex then
        EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.CHANGE_MAIN_CITY_PAGE, self.selectedIndex)
    end

    -- 动效
    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
end

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

function MainCityUI:initLeftRightBtns()
    self.leftNode = self.uiMap["main_ui.left_node"]
    self.leftSideBar = self.uiMap["main_ui.left_node.side_bar"]
    self.leftSideBar:setAnchorMin(0, 1)
    self.leftSideBar:setAnchorMax(0, 1)
    self.leftSideBar:getTransform().pivot = BF.Vector2(0, 1)
    local addY = SafeAreaManager:getNotchScreenHeight()
    self.leftSideBar:setAnchoredPosition(17, -280 - addY)
    self.leftArrowBtn = self.uiMap["main_ui.left_node.arrow_node"]
    self.leftArrowBtn:setAnchorMin(0, 1)
    self.leftArrowBtn:setAnchorMax(0, 1)
    self.leftArrowBtn:addClickListener(function()
        self:openOrCloseLeftSideBar()
    end)
    self.leftArrowImg = self.uiMap["main_ui.left_node.arrow_node.arrow"]

    self.rightNode = self.uiMap["main_ui.right_node"]
    self.rightSideBar = self.uiMap["main_ui.right_node.side_bar"]
    self.rightSideBar:setAnchorMin(1, 1)
    self.rightSideBar:setAnchorMax(1, 1)
    self.rightSideBar:getTransform().pivot = BF.Vector2(1, 1)
    self.rightSideBar:setAnchoredPosition(-17, -280 - addY)
    self.rightArrowBtn = self.uiMap["main_ui.right_node.arrow_node"]
    self.rightArrowBtn:setAnchorMin(1, 1)
    self.rightArrowBtn:setAnchorMax(1, 1)
    self.rightArrowBtn:addClickListener(function()
        self:openOrCloseRightSideBar()
    end)
    self.rightArrowImg = self.uiMap["main_ui.right_node.arrow_node.arrow"]

    self.sideBarCellObject = self.uiMap["main_ui.cache_node.side_bar_cell"]
    local w, h = self.sideBarCellObject:fastGetSizeDelta()
    self.sideBarHeight = h
    self.sideBarMaxCount = 4
    if self.leftBarList == nil then
        self.leftBarList = {}
    end
    if self.rightBarList == nil then
        self.rightBarList = {}
    end
    if self.sideBarClassMap == nil then
        self.sideBarClassMap = {}
    end
    if self.sideBarCellMap == nil then
        self.sideBarCellMap = {}
    end
    self:recycleSideBarCells(self.leftSideBar)
    self:recycleSideBarCells(self.rightSideBar)
end

function MainCityUI:recycleSideBarCells(list)
    local childList = list:getChildList()
    if childList and #childList > 0 then
        local count = #childList
        for i = count, 1, -1 do
            local child = childList[i]
            local name = child:getName()
            if name and name ~= "" then
                child:setVisible(false)
                self.sideBarCellMap[name] = child:getLuaComponent(name)
            else
                child:destroy()
                table.remove(childList, i)
            end
        end
    end
end

function MainCityUI:addSideBarCellComp(cellClassPath)
    local prefab = CS.UnityEngine.Object.Instantiate(self.sideBarCellObject:getGameObject())
    local prefabObject = UIPrefabObject:create()
    prefabObject:initWithPrefab(GConst.EMPTY_STRING, prefab)
    prefabObject:initPrefabHelper()
    prefabObject:genAllChildren()
    prefabObject:setName(cellClassPath)
    return prefabObject:addLuaComponent(cellClassPath)
end

function MainCityUI:openOrCloseLeftSideBar()
    ModuleManager.MaincityManager:changeMainCityLeftSideBarOpenOrClose()
    if self.isShowLeftSideBarArrowRedPoint ~= nil then
        local isShowLeftSideBarArrowRedPoint = self.isShowLeftSideBarArrowRedPoint
        self.isShowLeftSideBarArrowRedPoint = nil
        self:setLeftSideBarArrowRedPoint(isShowLeftSideBarArrowRedPoint)
    end
end

function MainCityUI:refreshLeftBtns()
    self:clearSideBarList(self.leftBarList)
    local list = GConst.MainCityConst.LEFT_SIDE_BARS
    if #list <= 0 then
        return
    end
    local isClose = ModuleManager.MaincityManager:getIsMainCityLeftSideBarClose()
    for k, v in ipairs(list) do
        local CellClass = self.sideBarClassMap[v]
        if CellClass == nil then
            CellClass = require(v)
            self.sideBarClassMap[v] = CellClass
        end
        if CellClass:checkIsOpen() then
            local cell = self.sideBarCellMap[v]
            if cell == nil then
                cell = self:addSideBarCellComp(v)
                cell:getBaseObject():setParent(self.leftSideBar, false)
                cell:getBaseObject():setAnchorMin(0, 1)
                cell:getBaseObject():setAnchorMax(0, 1)
            end
            cell:initWithParentUI(self, v, true)
            table.insert(self.leftBarList, cell)
        end
    end
    local sideBarShowCount = #self.leftBarList
    if sideBarShowCount <= 0 then
        self.leftNode:setAnchoredPositionX(GConst.NOT_VISIBLE_POS)
        return
    end
    self.leftNode:setAnchoredPositionX(0)
    local y = -SIDE_BAR_BORDER_OFFSET - self.sideBarHeight/2
    local x = 60
    local maxCount = self.sideBarMaxCount
    local minY = y
    local bgSizeX = 112
    local otherColumn = false
    if isClose then -- 只显示3个
        local minCount = GConst.MainCityConst.SIDE_BAR_MIN_COUNT
        for i = 1, minCount do
            local sideBarBtn = self.leftBarList[i]
            if i <= sideBarShowCount then
                sideBarBtn:setAnchoredPositionX(x)
                sideBarBtn:setAnchoredPositionY(y)
                sideBarBtn:refresh()
                sideBarBtn:setActive(true)
                sideBarBtn:setVisible(true)
                y = y - self.sideBarHeight - SIDE_BAR_INTERVAL
                if minY > y then
                    minY = y
                end
            end
        end
        for i = minCount + 1, sideBarShowCount do
            self.leftBarList[i]:refresh()
            self.leftBarList[i]:setActive(true)
            self.leftBarList[i]:setVisible(false)
        end
    else
        for k, v in ipairs(self.leftBarList) do
            if maxCount == 0 then -- 换行
                y = -SIDE_BAR_BORDER_OFFSET - self.sideBarHeight/2
                x = x + 110
                bgSizeX = 228
                otherColumn = true
            end

            maxCount = maxCount - 1
            v:setAnchoredPositionX(x)
            v:setAnchoredPositionY(y)
            v:refresh()
            v:setActive(true)
            v:setVisible(true)
            y = y - self.sideBarHeight - SIDE_BAR_INTERVAL
            if minY > y then
                minY = y
            end
        end
    end
    if sideBarShowCount <= GConst.MainCityConst.SIDE_BAR_MIN_COUNT then -- 小于3个不显示箭头
        y = y + self.sideBarHeight / 2
        if minY > y then
            minY = y
        end
        self.leftSideBar:setSizeDeltaX(bgSizeX)
        self.leftSideBar:setSizeDeltaY(-minY)
        self.leftArrowBtn:stopClickAnimation()
        self.leftArrowBtn:setVisible(false)
    else
        local arrowHeight = self.leftArrowBtn:getRectHeight()
        y = y + self.sideBarHeight/2 - SIDE_BAR_ARROW_INTERVAL - arrowHeight
        if minY > y then
            minY = y
        end
        self.leftSideBar:setSizeDeltaX(bgSizeX)
        self.leftSideBar:setSizeDeltaY(-minY)
        self.leftArrowImg:setLocalScale(1, isClose and -1 or 1, 1)
        self.leftArrowBtn:setVisible(true)
        self.leftArrowBtn:setAnchoredPositionY(self.leftSideBar:fastGetAnchoredPositionY() + minY + arrowHeight/2 + SIDE_BAR_BORDER_OFFSET)
        if otherColumn then
            self.leftArrowBtn:setAnchoredPositionX(133)
        else
            self.leftArrowBtn:setAnchoredPositionX(73)
        end
    end
end

function MainCityUI:openOrCloseRightSideBar()
    ModuleManager.MaincityManager:changeMainCityRightSideBarOpenOrClose()
    if self.isShowRightSideBarArrowRedPoint ~= nil then
        local isShowRightSideBarArrowRedPoint = self.isShowRightSideBarArrowRedPoint
        self.isShowRightSideBarArrowRedPoint = nil
        self:setRightSideBarArrowRedPoint(isShowRightSideBarArrowRedPoint)
    end
end

function MainCityUI:refreshRightBtns()
    self:clearSideBarList(self.rightBarList)
    local list = GConst.MainCityConst.RIGHT_SIDE_BARS
    if #list <= 0 then
        return
    end
    local isClose = ModuleManager.MaincityManager:getIsMainCityRightSideBarClose()
    for k, v in ipairs(list) do
        local CellClass = self.sideBarClassMap[v]
        if CellClass == nil then
            CellClass = require(v)
            self.sideBarClassMap[v] = CellClass
        end
        if CellClass:checkIsOpen() then
            local cell = self.sideBarCellMap[v]
            if cell == nil then
                cell = self:addSideBarCellComp(v)
                cell:getBaseObject():setParent(self.rightSideBar, false)
                cell:getBaseObject():setAnchorMin(1, 1)
                cell:getBaseObject():setAnchorMax(1, 1)
            end
            cell:initWithParentUI(self, v, false)
            table.insert(self.rightBarList, cell)
        end
    end
    local sideBarShowCount = #self.rightBarList
    if #self.rightBarList <= 0 then
        self.rightNode:setAnchoredPositionX(GConst.NOT_VISIBLE_POS)
        return
    end
    self.rightNode:setAnchoredPositionX(0)
    local y = -SIDE_BAR_BORDER_OFFSET - self.sideBarHeight/2
    local x = -60
    local maxCount = self.sideBarMaxCount
    local minY = y
    local bgSizeX = 112
    local otherColumn = false
    if isClose then -- 只显示一个
        local minCount = GConst.MainCityConst.SIDE_BAR_MIN_COUNT
        for i = 1, minCount do
            local sideBarBtn = self.rightBarList[i]
            if i <= sideBarShowCount then
                sideBarBtn:setAnchoredPositionX(x)
                sideBarBtn:setAnchoredPositionY(y)
                sideBarBtn:refresh()
                sideBarBtn:setActive(true)
                sideBarBtn:setVisible(true)
                y = y - self.sideBarHeight - SIDE_BAR_INTERVAL
                if minY > y then
                    minY = y
                end
            end
        end
        for i = minCount + 1, sideBarShowCount do
            self.rightBarList[i]:refresh()
            self.rightBarList[i]:setActive(true)
            self.rightBarList[i]:setVisible(false)
        end
    else
        for k, v in ipairs(self.rightBarList) do
            if maxCount == 0 then -- 换行
                y = -SIDE_BAR_BORDER_OFFSET - self.sideBarHeight/2
                x = x - 110
                bgSizeX = 228
                otherColumn = true
            end
            maxCount = maxCount - 1
            v:setAnchoredPositionX(x)
            v:setAnchoredPositionY(y)
            v:refresh()
            v:setActive(true)
            v:setVisible(true)
            y = y - self.sideBarHeight - SIDE_BAR_INTERVAL
            if minY > y then
                minY = y
            end
        end
    end
    if sideBarShowCount <= GConst.MainCityConst.SIDE_BAR_MIN_COUNT then
        y = y + self.sideBarHeight / 2
        y = y + self.sideBarHeight / 2
        if minY > y then
            minY = y
        end
        self.rightSideBar:setSizeDeltaX(bgSizeX)
        self.rightSideBar:setSizeDeltaY(-minY)
        self.rightArrowBtn:stopClickAnimation()
        self.rightArrowBtn:setVisible(false)
    else
        local arrowHeight = self.rightArrowBtn:getRectHeight()
        y = y + self.sideBarHeight / 2 - SIDE_BAR_ARROW_INTERVAL - arrowHeight
        if minY > y then
            minY = y
        end
        self.rightSideBar:setSizeDeltaX(bgSizeX)
        self.rightSideBar:setSizeDeltaY(-minY)
        self.rightArrowImg:setLocalScale(1, isClose and -1 or 1, 1)
        self.rightArrowBtn:setVisible(true)
        self.rightArrowBtn:setAnchoredPositionY(self.rightSideBar:fastGetAnchoredPositionY() + minY + arrowHeight/2 + SIDE_BAR_BORDER_OFFSET)
        if otherColumn then
            self.rightArrowBtn:setAnchoredPositionX(-133)
        else
            self.rightArrowBtn:setAnchoredPositionX(-73)
        end
    end
end

function MainCityUI:clearSideBarList(sideBarList)
    local count = #sideBarList
    for i = 1, count do
        local cell = table.remove(sideBarList)
        cell:setActive(false)
        cell:setVisible(false)
        self.sideBarCellMap[cell:getCellPath()] = cell
    end
end

function MainCityUI:setSideBarVisible(visible)
    self.leftNode:setVisible(visible)
    self.rightNode:setVisible(visible)
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
    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:getIsOpen()
	if not isOpen or not self.mainComp:getCurModule():isShowBounty() 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:getIsShowRedPoint()
    if showRedPoint then
        self.taskBtn:addRedPoint(38, 34, 0.7)
    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:clearPostFlag()
                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)
        self:setSideBarVisible(false)
    end
end

-- 切换主界面活动模块
function MainCityUI:switchMainCompModule(moduleKey)
    if self.selectedIndex ~= GConst.MainCityConst.BOTTOM_PAGE.MAIN then
        return
    end
    self.mainComp:refreshModule(ModuleManager.MaincityManager:getCurModule())
    if ModuleManager.MaincityManager:getCurModule() == GConst.MainCityConst.MAIN_MODULE.CHAPTER then
        self:checkMainPop()
    end
    local module = self.mainComp:getCurModule()
    if module then
        self:setTopNodeVisible(module:isShowTopNode())
        self:setSideBarVisible(module:isShowSideBar())
        self:refreshBounty()
        UIManager:updateBarsState(self)
    end
end

function MainCityUI:updateTime()
    if self.subComps[self.selectedIndex] and self.subComps[self.selectedIndex].refreshTime then
        self.subComps[self.selectedIndex]:refreshTime()
    end
    self.inSideBarUpdate = true
    if self.leftBarList then
        for k, v in ipairs(self.leftBarList) do
            v:updateTime()
        end
    end
    if self.waitRefreshSideBar then
        self:refreshLeftBtns()
        self.waitRefreshSideBar = false
    end
    if self.rightBarList then
        for k, v in ipairs(self.rightBarList) do
            v:updateTime()
        end
    end
    self.inSideBarUpdate = false
    if self.waitRefreshSideBar then
        self:refreshRightBtns()
        self.waitRefreshSideBar = false
    end

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

function MainCityUI:updateSideBarStatus()
    if self.leftBarList then
        local minCount = GConst.MainCityConst.SIDE_BAR_MIN_COUNT
        for i = 1, minCount do
            local sideBarBtn = self.leftBarList[i]
            if sideBarBtn then
                if sideBarBtn:getIsShowRedPoint() then
                    sideBarBtn:showRedPoint()
                else
                    sideBarBtn:hideRedPoint()
                end
            end
        end
        local sideBarCount = #self.leftBarList
        if sideBarCount > minCount then
            local count = 0
            for i = minCount + 1, sideBarCount do
                if self.leftBarList[i]:getIsShowRedPoint() then
                    self.leftBarList[i]:showRedPoint()
                    count = count + 1
                else
                    self.leftBarList[i]:hideRedPoint()
                end
            end
            if count > 0 then
                self:setLeftSideBarArrowRedPoint(true)
            else
                self:setLeftSideBarArrowRedPoint(false)
            end
        else
            self:setLeftSideBarArrowRedPoint(false)
        end
    end
    if self.rightBarList then
        local minCount = GConst.MainCityConst.SIDE_BAR_MIN_COUNT
        for i = 1, minCount do
            local sideBarBtn = self.rightBarList[i]
            if sideBarBtn then
                if sideBarBtn:getIsShowRedPoint() then
                    sideBarBtn:showRedPoint()
                else
                    sideBarBtn:hideRedPoint()
                end
            end
        end
        local sideBarCount = #self.rightBarList
        if sideBarCount > minCount then
            local count = 0
            for i = minCount + 1, sideBarCount do
                if self.rightBarList[i]:getIsShowRedPoint() then
                    self.rightBarList[i]:showRedPoint()
                    count = count + 1
                else
                    self.rightBarList[i]:hideRedPoint()
                end
            end
            if count > 0 then
                self:setRightSideBarArrowRedPoint(true)
            else
                self:setRightSideBarArrowRedPoint(false)
            end
        else
            self:setRightSideBarArrowRedPoint(false)
        end
    end
end

function MainCityUI:setLeftSideBarArrowRedPoint(isShow)
    if self.isShowLeftSideBarArrowRedPoint == isShow then
        return
    end
    if isShow then
        if ModuleManager.MaincityManager:getIsMainCityLeftSideBarClose() then
            self.leftArrowBtn:addRedPoint(19, 10, 0.6)
        else
            self.leftArrowBtn:removeRedPoint()
        end
    else
        self.leftArrowBtn:removeRedPoint()
    end
end

function MainCityUI:setRightSideBarArrowRedPoint(isShow)
    if self.isShowRightSideBarArrowRedPoint == isShow then
        return
    end
    if isShow then
        if ModuleManager.MaincityManager:getIsMainCityRightSideBarClose() then
            self.rightArrowBtn:addRedPoint(19, 10, 0.6)
        else
            self.rightArrowBtn:removeRedPoint()
        end
    else
        self.rightArrowBtn:removeRedPoint()
    end
end

function MainCityUI:closeSideBarBtn(isLeft)
    if self.inSideBarUpdate then
        self.waitRefreshSideBar = true
    else
        self.waitRefreshSideBar = false
        if isLeft then
            self:refreshLeftBtns()
        else
            self:refreshRightBtns()
        end
    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, 0.7)
    else
        heroRpObj:removeRedPoint()
    end
    local shopRpObj = uiMap["main_ui.bottom_node.icons.ui_spine_obj_3.rp_node"]
    if DataManager.ShopData:getRp() then
        shopRpObj:addRedPoint(0, 0, 0.7)
    else
        shopRpObj:removeRedPoint()
    end
end

function MainCityUI:refreshSettingBtn()
    if (DataManager.MailData:getIsOpen() and DataManager.MailData:getRedPoint()) or DataManager.AIHelperData:getRp() then
        self.settingbtn:addRedPoint(40, 40, 0.7)
    else
        self.settingbtn:removeRedPoint()
    end
end

function MainCityUI:checkSideBarOpenStatus()
    -- 检查目前没开的侧边栏功能是否有可以开启的
    for k, CellClass in pairs(self.sideBarClassMap) do
        local cell = self.sideBarCellMap[k]
        if cell and cell:getIsActive() then
            if not CellClass:checkIsOpen() then
                return self:refreshAllSideBars()
            end
        else
            if CellClass:checkIsOpen() then
                return self:refreshAllSideBars()
            end
        end
    end
end

-- 弹窗优先级： 升级>功能弹窗>英雄解锁弹窗>礼包弹窗>引导
function MainCityUI:checkMainPop()
    -- 续关
    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:getOverOfServerToday(snapshotTime) >= Time:getOverOfServerToday() 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({})
                        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 ModuleManager.MaincityManager:getCurModule() ~= GConst.MainCityConst.MAIN_MODULE.CHAPTER then -- 当前不是关卡界面不触发
        return
    end

    -- 检查是否升级
    if DataManager.PlayerData:getIfCanLevelUp() then
        ModuleManager.PlayerManager:levelUp()
        return
    end
    -- 检查功能弹窗
    if DataManager.PlayerData:getIfCanShowModuleUnlock() and not GFunc.isShenhe() then
        ModuleManager.MaincityManager:showModuleUnlockUI()
        return
    end
    -- 是否是否有英雄解锁弹窗
    if DataManager.HeroData:getIfCanShowHeroUnlock() then
        local list = DataManager.HeroData:getHeroChapterUnlockList()
        DataManager.HeroData:markShowHeroUnlock()
        if list and #list > 0 then
            ModuleManager.HeroManager:showHeroUnlockUI(list)
            return
        end
    end

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

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

    -- 圆月活动
    local showType = DataManager.FullMoonData:getNeedShowActPanel()
    if showType then
        ModuleManager.FullMoonManager:showActMainUI(showType)
        return
    end

    -- 金猪满了后下次进主城要弹出来
    if DataManager.GoldPigData:getPopFlag() and not DataManager.TutorialData:getIsInTutorial() then
        DataManager.GoldPigData:markPop()
        local showType = BIReport.PAY_UI_SHOW_TYPE.TRIGGER_POP
        if self.isFirstEnter then
            showType = BIReport.PAY_UI_SHOW_TYPE.LOGIN_POP
        end
        ModuleManager.ActivityManager:showGoldPigUI(showType)
        return
    end

    if DataManager.ActBossRushData:getIsOpen() and not DataManager.TutorialData:getIsInTutorial()  then -- 弹窗
        if not DataManager.ActBossRushData:getMarkPoped() then
            if ModuleManager.ActBossRushManager:showPopUI() then
                return
            end
        end
    end
end

-- 检查引导
function MainCityUI:checkTutorial(onlyCheck)
    if DataManager.ChapterData:getMaxChapterId() == 1 then
        if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.PASS_ONE_CHAPTER, onlyCheck) then
            return true
        end
    end

    if DataManager.ChapterData:boxCanGet(2, 1) then
        if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.TWO_CHAPTER_BOX_CAN_GOT, 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 DataManager.DailyChallengeData:isOpen() then
        if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.UNLOCK_DAILY_CHALLENGE, 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 DataManager.ArenaData:isOpen() then
        if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.UNLOCK_ARENA, onlyCheck) then
            return true
        end
    end

    if DataManager.DungeonData:isOpen(ModuleManager.MODULE_KEY.EQUIP_WEAPON) then
        if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.WEAPON_DUNGEON, onlyCheck) then
            return true
        end
    end

    if DataManager.DungeonData:isOpen(ModuleManager.MODULE_KEY.EQUIP_ARMOR) then
        if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.ARMOR_DUNGEON, onlyCheck) then
            return true
        end
    end

    if DataManager.DungeonData:isOpen(ModuleManager.MODULE_KEY.RUNES_OPEN) then
        if ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.RUNE_DUNGEON, 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 or self.subComps[self.selectedIndex]:getCurModuleType() ~= GConst.MainCityConst.MAIN_MODULE.CHAPTER 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]:getDailyChallengeIconPos()
end

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

function MainCityUI:getArenaIconPos()
    return self:getLeftOrRightEntrancePos(GConst.MainCityConst.MAIN_MODULE.ARENA)
end

function MainCityUI:getSideBarActIconPos(moduleKey)
    for name, cell in pairs(self.leftBarList) do
        if moduleKey == cell:getModuleKey() then
            return cell:getBaseObject():getPosition()
        end
    end
    for name, cell in pairs(self.rightBarList) do
        if moduleKey == cell:getModuleKey() then
            return cell:getBaseObject():getPosition()
        end
    end
    return nil
end

function MainCityUI:isInSideBarLeft(moduleKey)
    for name, cell in pairs(self.leftBarList) do
        if moduleKey == cell:getModuleKey() then
            return true
        end
    end
    return false
end

function MainCityUI:isInSideBarRight(moduleKey)
    for name, cell in pairs(self.rightBarList) do
        if moduleKey == cell:getModuleKey() then
            return true
        end
    end
    return false
end

function MainCityUI:isActivSideBarModule(moduleKey)
    for name, cell in pairs(self.leftBarList) do
        if moduleKey == cell:getModuleKey() then
            return cell:getIsActive() and cell:getIsVisible()
        end
    end
    for name, cell in pairs(self.rightBarList) do
        if moduleKey == cell:getModuleKey() then
            return cell:getIsActive() and cell:getIsVisible()
        end
    end
    return nil
end

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

return MainCityUI