local FundMainUI = class("FundMainUI", BaseUI)
-- FundMainUI
local BTN_WIDTH = 82
local BTN_RIGHT = 20
local BTN_SPACING = 26

function FundMainUI:showCommonBG()
	return false
end

function FundMainUI:onPressBackspace()
	self:closeUI()
end

function FundMainUI:getPrefabPath()
	return "assets/prefabs/ui/fund/fund_main_ui.prefab"
end

function FundMainUI:onClose()
	if self.subComps then
		for _, comp in pairs(self.subComps) do
			if comp.onClose then
				comp:onClose()
			end
		end
	end
	EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.MAIN_UI_CHECK_POP)

	local callback = self.callback
	self.callback = nil
	if callback then
		callback()
	end
end

function FundMainUI:ctor(param)
	if param and param.showPage then
		self.curPage = param.showPage
		self.onlyShowPage = param.onlyShowPage
		self.callback = param.callback
	else
		self:switchOtherPage()
	end
end

function FundMainUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	self.btnClose = uiMap["fund_main_ui.bottom.btn_close"]
	self.btnNode = uiMap["fund_main_ui.bottom.btns"]
	self.btnContent = uiMap["fund_main_ui.bottom.btns.viewport.content"]
	self.btns = {}
	self.txBtns = {}
	for i = 1, table.nums(GConst.ShopConst.FUND_SHOW_PAGE) do
		table.insert(self.btns, uiMap["fund_main_ui.bottom.btns.viewport.content.btn_" .. i])
		table.insert(self.txBtns, uiMap["fund_main_ui.bottom.btns.viewport.content.btn_" .. i .. ".tx_desc"])
	end
	self.compsRoot = uiMap["fund_main_ui.comps"]
	for i, btn in ipairs(self.btns) do
		self.txBtns[i]:setText(I18N:getGlobalText(I18N.GlobalConst[GConst.ShopConst.FUND_PAGE_BTN_DESC[i]]))
		btn:addClickListener(function()
			self:onChangePage(i)
		end)
	end

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

	self.btnClose:addClickListener(function()
		self:closeUI()
	end)
	self:bind(DataManager.FundLevelData, "isDirty", function()
		self:refreshRedPoint()
		if self.curPage ~= GConst.ShopConst.FUND_SHOW_PAGE.FUND then
			return
		end
		self:onRefresh()
	end)
	self:bind(DataManager.ChapterFundData, "isDirty", function()
		self:refreshRedPoint()
		if self.curPage ~= GConst.ShopConst.FUND_SHOW_PAGE.CHAPTER_FUND then
			return
		end
		self:onRefresh()
	end)
	-- self:bind(DataManager.BountyData, "isDirty", function()
	-- 	self:refreshRedPoint()
	-- end)
	-- self:bind(DataManager.BountyCollectionData, "isDirty", function()
	-- 	self:refreshRedPoint()
	-- end)
	-- self:bind(DataManager.TowerData, "isDirty", function()
	-- 	self:onRefresh()
	-- end)
	-- self:bind(DataManager.TowerFundData, "isDirty", function()
	-- 	self:onRefresh()
	-- end)
	-- self:bind(DataManager.BountyGrowthData, "isDirty", function()
	-- 	self:onRefresh()
	-- end)
	self:bind(DataManager.PaymentData, "isDirty", function()
		self:onRefresh()
	end)
end

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

	for k, id in pairs(GConst.ShopConst.FUND_SHOW_PAGE) do
		if GConst.ShopConst.FUND_COMP_NAME[id] then
			-- 预制体挂载
			local obj = uiMap[GConst.ShopConst.FUND_COMP_NAME[id]]
			obj:initPrefabHelper()
			obj:genAllChildren()
			local comp = obj:addLuaComponent(GConst.ShopConst.FUND_COMP[id])
			self.subComps[id] = comp
		elseif GConst.ShopConst.FUND_COMP_PATH[id] then
			-- 动态加载
			self:loadModule(GConst.ShopConst.FUND_COMP_PATH[id], self.compsRoot, GConst.ShopConst.FUND_COMP[id], function(comp)
				comp:getBaseObject():setActive(false)
				self.subComps[id] = comp

				-- 判断当前需要展示界面是否是刚加载完的界面，是的话刷新一下界面
				if self.curPage == id then
					self:onRefresh()
				end
			end)
		end
	end
end

function FundMainUI:updateTime()
	if self.subComps then
		for _, comp in pairs(self.subComps) do
			if comp.updateTime then
				comp:updateTime()
			end
		end
	end
end

function FundMainUI:onChangePage(page)
	if self.curPage == page then
		return
	end

	for i, comp in pairs(self.subComps) do
		if page ~= i and self.curPage == i and comp.onClose then
			comp:onClose()
		end
	end

	self.curPage = page
	self:onRefresh()
end

function FundMainUI:switchOtherPage()
	-- if DataManager.BountyData:showRedPoint() then
	-- 	self.curPage = GConst.ShopConst.FUND_SHOW_PAGE.BOUNTY
	-- elseif DataManager.BountyCollectionData:showRedPoint() then
	-- 	self.curPage = GConst.ShopConst.FUND_SHOW_PAGE.BOUNTY_COLLECTION
	-- elseif DataManager.FundLevelData:getRedPoint() then
	-- 	self.curPage = GConst.ShopConst.FUND_SHOW_PAGE.FUND
	-- elseif DataManager.ChapterFundData:getRedPoint() then
	-- 	self.curPage = GConst.ShopConst.FUND_SHOW_PAGE.CHAPTER_FUND
	-- elseif DataManager.BountyGrowthData:getIsOpen() then
	-- 	self.curPage = GConst.ShopConst.FUND_SHOW_PAGE.BOUNTY_GROWTH
	-- end
	if DataManager.FundLevelData:getRedPoint() then
		self.curPage = GConst.ShopConst.FUND_SHOW_PAGE.FUND
	elseif DataManager.ChapterFundData:getRedPoint() then
		self.curPage = GConst.ShopConst.FUND_SHOW_PAGE.CHAPTER_FUND
	end

	if self.curPage ~= nil then
		return
	end

	for page = 1, table.nums(GConst.ShopConst.FUND_SHOW_PAGE) do
		if self:isOpen(page) then
			self.curPage = page
			break
		end
	end
end

function FundMainUI:onRefresh()
	if self.curPage == GConst.ShopConst.FUND_SHOW_PAGE.FUND then
		if DataManager.FundLevelData:getIsAllClaimed() then
			local page = self.curPage
			self:switchOtherPage()
			if page == self.curPage then
				return self:closeUI()
			else
				return self:onRefresh()
			end
		end
	end
	if self.curPage == GConst.ShopConst.FUND_SHOW_PAGE.CHAPTER_FUND then
		if DataManager.ChapterFundData:getIsAllClaimed() then
			local page = self.curPage
			self:switchOtherPage()
			if page == self.curPage then
				return self:closeUI()
			else
				return self:onRefresh()
			end
		end
	end

	local openCount = 0
	for i, btn in ipairs(self.btns) do
		if self:isOpen(i) then
			btn:setActive(true)
			openCount = openCount + 1
			if self.curPage == i then
				btn:setSprite(GConst.ShopConst.FUND_PAGE_BTN_ICON_ATLAS[i], GConst.ShopConst.FUND_PAGE_BTN_ICON_1[i])
			else
				btn:setSprite(GConst.ShopConst.FUND_PAGE_BTN_ICON_ATLAS[i], GConst.ShopConst.FUND_PAGE_BTN_ICON_2[i])
			end
		else
			btn:setActive(false)
		end
	end
	local width = BTN_RIGHT + BTN_WIDTH * openCount + BTN_SPACING * (openCount - 1) + 20
	self.btnContent:setSizeDeltaX(width)
	self.btnContent:setAnchoredPositionX(math.max(width - self.btnNode:getSizeDeltaX(), 0))

	for i, comp in pairs(self.subComps) do
		if self.curPage == i then
			comp:getBaseObject():setActive(true)
			comp:refresh()
		else
			comp:getBaseObject():setActive(false)
		end
	end

	-- 红点
	self:refreshRedPoint()

	self.btnNode:setActive(not self.onlyShowPage)
end

function FundMainUI:refreshRedPoint()
	for i, btn in ipairs(self.btns) do
		if self:hasRedPoint(i) then
			btn:addRedPoint(30, 30, 0.9)
		else
			btn:removeRedPoint()
		end
	end
end

function FundMainUI:isOpen(type)
	if type == GConst.ShopConst.FUND_SHOW_PAGE.BOUNTY then
		-- return DataManager.BountyData:isOpen()
		return false
	elseif type == GConst.ShopConst.FUND_SHOW_PAGE.BOUNTY_COLLECTION then
		-- return DataManager.BountyCollectionData:isOpen()
		return false
	elseif type == GConst.ShopConst.FUND_SHOW_PAGE.FUND then
		return DataManager.FundLevelData:getIsOpen()
	elseif type == GConst.ShopConst.FUND_SHOW_PAGE.CHAPTER_FUND then
		return DataManager.ChapterFundData:getIsOpen()
	elseif type == GConst.ShopConst.FUND_SHOW_PAGE.TOWER_BOUNTY then
		-- return DataManager.TowerData:isOpen()
		return false
	elseif type == GConst.ShopConst.FUND_SHOW_PAGE.BOUNTY_GROWTH then
		-- return DataManager.BountyGrowthData:getIsOpen()
		return false
	end
end

function FundMainUI:hasRedPoint(type)
	if type == GConst.ShopConst.FUND_SHOW_PAGE.BOUNTY then
		-- return DataManager.BountyData:showRedPoint()
		return false
	elseif type == GConst.ShopConst.FUND_SHOW_PAGE.BOUNTY_COLLECTION then
		-- return DataManager.BountyCollectionData:showRedPoint()
		return false
	elseif type == GConst.ShopConst.FUND_SHOW_PAGE.FUND then
		return DataManager.FundLevelData:getRedPoint()
	elseif type == GConst.ShopConst.FUND_SHOW_PAGE.CHAPTER_FUND then
		return DataManager.ChapterFundData:getRedPoint()
	elseif type == GConst.ShopConst.FUND_SHOW_PAGE.TOWER_BOUNTY then
		-- return DataManager.TowerFundData:getRedPoint()
		return false
	elseif type == GConst.ShopConst.FUND_SHOW_PAGE.BOUNTY_GROWTH then
		-- return DataManager.BountyGrowthData:getRedPoint()
		return false
	end
end

return FundMainUI