local ActBaseUI = class("ActBaseUI", BaseUI)

local ACT_PAGE_BTNS_COMP = "app/ui/activity/common/comp/act_page_btns_comp"

function ActBaseUI:ctor(params)
	params = params or {}
	self.page = params.page or 1
	self.ActConst = self:getActConst()
end

function ActBaseUI:onClose()
	if self.subComps then
		for _, comp in pairs(self.subComps) do
			if comp.onClose then
				comp:onClose()
			end
		end
	end
	self.subComps = nil
end

function ActBaseUI:onLoadRootComplete()
	self:_display()
	self:_addListeners()
	self:_bind()
	self:initComps()

	self:updateCurrencyBar()

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

function ActBaseUI:_display()
	local uiMap = self.root:genAllChildren()
    self.compsRoot = uiMap["act_main_ui.sub_comps"]
	self.closeBtn = uiMap["act_main_ui.bottom.close_btn"]
	-- self.titleBg = uiMap["act_main_ui.title_bg"]
	self.titleTx = uiMap["act_main_ui.top.title_tx"]
	self.helpBtn = uiMap["act_main_ui.title_bg.help_btn"]
	self.timeTx = uiMap["act_main_ui.top.time_bg.time_tx"]
	self.pageBtnsCompObj = uiMap["act_main_ui.bottom.act_page_btns_comp"]
	self.disableBg = uiMap["act_main_ui.disable_bg"]
	self.disableBg:setActive(false)

	self.pageBtns = {}
	for k, idx in pairs(self:getActConst().BOTTOM_PAGE) do
		self.pageBtns[idx] = uiMap["act_main_ui.bottom.btns.viewport.content.page_btn_" .. idx]
		uiMap["act_main_ui.bottom.btns.viewport.content.page_btn_" .. idx .. ".text"]:setText(self:getCellName(idx))
	end
end

function ActBaseUI:_addListeners()
	self.closeBtn:addClickListener(function()
		self:closeUI()
	end)

	if self.helpBtn then
		self.helpBtn:addClickListener(function()
			if self.getHelpStr then
				local params = {}
				params.desc = self:getHelpStr()
				ModuleManager.TipsManager:showHelpTips(params)
			end
		end)
	end

	for i, btn in ipairs(self.pageBtns) do
		btn:addClickListener(function()
			self:refreshPage(i)
		end, GConst.CLICK_SOUND.NONE)
	end

	self:addEventListener(EventManager.CUSTOM_EVENT.CHANGE_MAIN_CITY_PAGE, function(params)
		if not self:isClosed() then
			self:closeUI()
		end
	end)

	-- self:addEventListener(EventManager.CUSTOM_EVENT.ACT_GAME_OPT, function(params)
	-- 	if self.subComps[self:getActConst().BOTTOM_PAGE.GAME] then
	-- 		self.subComps[self:getActConst().BOTTOM_PAGE.GAME]:settlementGame()
	-- 	end
	-- 	self:refreshPageBtns()
	-- end)

	-- self:addEventListener(EventManager.CUSTOM_EVENT.ACT_TURNTABLE, function(params)
	-- 	if not self:isClosed() and self.page == self:getActConst().BOTTOM_PAGE.TURNTABLE then
	-- 		self.subComps[self.page]:playAni(params)
	-- 	end
	-- end)
	-- self:addEventListener(EventManager.CUSTOM_EVENT.ACT_BIG_REWARD_SHOW, function(params)
	-- 	if self:isClosed() then
	-- 		return
	-- 	end
	-- 	if self.page ~= self:getActConst().BOTTOM_PAGE.TURNTABLE then
	-- 		return
	-- 	end
	-- 	local rewards = DataManager[self:getActDataType()]:getBigRewards()
	-- 	if rewards == nil or #rewards == 0 then
	-- 		return
	-- 	end
	-- 	ModuleManager.ActivityManager:showBigRewardUI(self, rewards)
	-- end)
	-- self:addEventListener(EventManager.CUSTOM_EVENT.ACT_TRIGGER_GIFT, function()
	-- 	if not self:isClosed() and self.page == self:getActConst().BOTTOM_PAGE.TURNTABLE then
	-- 		self.subComps[self.page]:updateTriggerGift()
	-- 	end
    -- end)
	-- self:addEventListener(EventManager.CUSTOM_EVENT.ACT_TURNTABLE_RESET, function()
	-- 	if not self:isClosed() and self.page == self:getActConst().BOTTOM_PAGE.TURNTABLE then
	-- 		self.subComps[self.page]:updateTurntable()
	-- 	end
    -- end)

	self:addEventListener(EventManager.CUSTOM_EVENT.CHANGE_ACTIVITY_PAGE, function(params)
		self:refreshPage(params)
	end)
end

function ActBaseUI:_bind()
	self:bind(DataManager.PaymentData, "isDirty", function()
		self.subComps[self.page]:refresh(self, self:getActDataType())
		self:onRefresh()
	end)
	self:bind(DataManager.ActivityData, "isDirty", function()
		self.subComps[self.page]:refresh(self, self:getActDataType())
		self:onRefresh()
	end)
	-- self:bind(DataManager.ActBountyData, "isDirty", function()
	-- 	self.subComps[self.page]:refresh(self, self:getActDataType())
	-- 	self:onRefresh()
	-- end)
	self:bind(DataManager.ActTaskData, "isDirty", function()
		self.subComps[self.page]:refresh(self, self:getActDataType())
		self:onRefresh()
	end)
	self:bind(DataManager[self:getActDataType()], "isDirty", function()
		if self.page == self:getActConst().BOTTOM_PAGE.GAME then
			self.subComps[self.page]:refreshStepRewardScrollrect()
		else
			self.subComps[self.page]:refresh(self, self:getActDataType())
		end
		self:refreshPageBtns()
	end)
end

function ActBaseUI:refreshPage(page)
	-- if self.page == page then
	-- 	return
	-- end
	self.page = page
	self:onRefresh()
end

function ActBaseUI:initComps()
	if self.subComps then
		return
	end
	local uiMap = self.root:genAllChildren()
	self.subComps = {}

	for k, id in pairs(self:getActConst().BOTTOM_PAGE) do
		if self:getActConst().COMP_NAME[id] then
			-- 预制体挂载
			self.subComps[id] = CellManager:addCellComp(uiMap[self:getActConst().COMP_NAME[id]], self:getActConst().COMP[id])
			if self.subComps[id].setParentUI then
				self.subComps[id]:setParentUI(self, self:getActDataType())
			end
		elseif self:getActConst().COMP_PATH[id] then
			-- 动态加载
			self:loadModule(self:getActConst().COMP_PATH[id], self.compsRoot, self:getActConst().COMP[id], function(comp)
				if self.subComps == nil then
					return
				end
				comp:getBaseObject():setActive(false)
				self.subComps[id] = comp
				if self.subComps[id].setParentUI then
					self.subComps[id]:setParentUI(self, self:getActDataType())
				end
			end)
		end
	end

	if self:getActConst().BOTTOM_PAGE.GAME then
		local order = self._baseRootCanvas.sortingOrder
		self.subComps[self:getActConst().BOTTOM_PAGE.GAME]:getBaseObject():setActive(true)
		local uiHelper = self.subComps[self:getActConst().BOTTOM_PAGE.GAME]:getGameObject():GetComponent(GConst.TYPEOF_UNITY_CLASS.BF_UI_HELPER)
		if uiHelper then
			uiHelper:SetSortingOrder(order + 1)
		end
	end
end

function ActBaseUI:updateTime()
	if DataManager[self:getActDataType()]:getRemainTime() <= 0 then
		self:closeUI()
		return
	end

	local remainTime = DataManager[self:getActDataType()]:getRemainTime()
	if self.cacheRemainTime ~= remainTime then
		self.cacheRemainTime = remainTime
		if remainTime > 0 then
			self.timeTx:setText(I18N:getGlobalText(I18N.GlobalConst.TIME_END_DESC_1, Time:formatNumTimeStr(remainTime)))
		end
	end

	for i,v in ipairs(self.subComps) do
		if v.updateTime then
			v:updateTime()
		end
	end

	if self.subComps[self:getActConst().BOTTOM_PAGE.TURNTABLE] and self.subComps[self:getActConst().BOTTOM_PAGE.TURNTABLE].updateAni then
		self.subComps[self:getActConst().BOTTOM_PAGE.TURNTABLE]:updateAni()
	end
end

function ActBaseUI:onRefresh()
	if not DataManager[self:getActDataType()]:getIsOpen() then
		return
	end

	self:refreshPanel()
	self:refreshPageBtns()

	-- self.titleBg:setActive(self.page == self:getActConst().BOTTOM_PAGE.TURNTABLE)
	self.titleTx:setText(self:getCellName(self.page))
	if self.helpBtn then
		self.helpBtn:setActive(self.page == self:getActConst().BOTTOM_PAGE.GAME)
	end
end

function ActBaseUI:refreshPanel()
	if self.subComps == nil then
		return
	end

	for i, comp in ipairs(self.subComps) do
		comp.baseObject:setActive(i == self.page)
		if i == self.page then
			comp:refresh(self, self:getActDataType())
		end
	end
end

function ActBaseUI:refreshPageBtns()
	if self.pageBtnInfos == nil then
		self.pageBtnInfos = {}
		for k, idx in pairs(self:getActConst().BOTTOM_PAGE) do
			self.pageBtnInfos[idx] = {}
		end
	end
	for k, idx in pairs(self:getActConst().BOTTOM_PAGE) do
		local atlas = GConst.ATLAS_PATH.UI_ACT_COMMON
		local icon = self:getPageBtnName(idx, self.page == idx)
		local desc = self:getCellName(idx)
		local redPoint = DataManager[self:getActDataType()]:getRpByPage(idx)
		local callback = function()
			self:refreshPage(idx)
		end

		self.pageBtnInfos[idx].atlas = atlas
		self.pageBtnInfos[idx].icon = icon
		self.pageBtnInfos[idx].desc = desc
		self.pageBtnInfos[idx].redPoint = redPoint
		self.pageBtnInfos[idx].callback = callback
	end

	if self.pageBtnsCompObj then
		self.pageBtnsComp = self.pageBtnsCompObj:addLuaComponent(ACT_PAGE_BTNS_COMP)
		self.pageBtnsComp:refresh(self.pageBtnInfos)
	end

	for i, btn in ipairs(self.pageBtns) do
		btn:setActive(self.pageBtnsComp == nil)

		local atlas = GConst.ATLAS_PATH.UI_ACT_COMMON
		if self.page == i then
			btn:setSprite(atlas, self:getPageBtnName(i, true))
		else
			btn:setSprite(atlas, self:getPageBtnName(i, false))
		end

		if DataManager[self:getActDataType()]:getRpByPage(i) then
			btn:addRedPoint(32, 20, 1)
		else
			btn:removeRedPoint()
		end
	end
	self:updateCurrencyBar()
end

function ActBaseUI:disableUITouchSelf()
	self.disableBg:setActive(true)
end

function ActBaseUI:enableUITouchSelf()
	self.disableBg:setActive(false)
end

function ActBaseUI:showTitle(show)
	self.titleBg:setActive(show)
end

function ActBaseUI:showHelp(show)
	self.helpBtn:setActive(show)
end

function ActBaseUI:getCellName(page)
end

return ActBaseUI