local SignMainUI = class("SignMainUI", BaseUI)

local BTN_WIDTH = 82
local BTN_RIGHT = 20
local BTN_SPACING = 26

function SignMainUI:showCommonBG()
	return false
end

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

function SignMainUI:getPrefabPath()
	return "assets/prefabs/ui/sign/sign_main_ui.prefab"
end

function SignMainUI: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 SignMainUI: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 SignMainUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	self.btnClose = uiMap["sign_main_ui.bottom.btn_close"]
	self.btnNode = uiMap["sign_main_ui.bottom.btns"]
	self.btnContent = uiMap["sign_main_ui.bottom.btns.viewport.content"]
	self.btns = {}
	self.txBtns = {}
	for i = 1, 6 do
		table.insert(self.btns, uiMap["sign_main_ui.bottom.btns.viewport.content.btn_" .. i])
		table.insert(self.txBtns, uiMap["sign_main_ui.bottom.btns.viewport.content.btn_" .. i .. ".tx_desc"])
	end
	self.compsRoot = uiMap["sign_main_ui.comps"]
	for i, btn in ipairs(self.btns) do
		self.txBtns[i]:setText(I18N:getGlobalText(I18N.GlobalConst[GConst.SignConst.SIGN_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.SignWeekData, "isDirty", function()
		self:refreshRedPoint()
	end)
	self:bind(DataManager.SignMonthData, "isDirty", function()
		self:refreshRedPoint()
	end)
end

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

	for k, id in pairs(GConst.SignConst.SIGN_SHOW_PAGE) do
		if GConst.SignConst.SIGN_COMP_NAME[id] then
			-- 预制体挂载
			local obj = uiMap[GConst.SignConst.SIGN_COMP_NAME[id]]
			obj:initPrefabHelper()
			obj:genAllChildren()
			local comp = obj:addLuaComponent(GConst.SignConst.SIGN_COMP[id])
			self.subComps[id] = comp
		elseif GConst.SignConst.SIGN_COMP_PATH[id] then
			-- 动态加载
			self:loadModule(GConst.SignConst.SIGN_COMP_PATH[id], self.compsRoot, GConst.SignConst.SIGN_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 SignMainUI:updateTime()
	if self.subComps then
		for _, comp in pairs(self.subComps) do
			if comp.updateTime then
				comp:updateTime()
			end
		end
	end
end

function SignMainUI: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 SignMainUI:switchOtherPage()
	if DataManager.SignWeekData:showRedPoint() then
		self.curPage = GConst.SignConst.SIGN_SHOW_PAGE.WEEK
	-- elseif DataManager.SignMonthData:showRedPoint() then
	-- 	self.curPage = GConst.SignConst.SIGN_SHOW_PAGE.MONTH
	end

	if self.curPage ~= nil then
		return
	end

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

function SignMainUI:onRefresh()
	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.SignConst.SIGN_PAGE_BTN_ICON_ATLAS[i], GConst.SignConst.SIGN_PAGE_BTN_ICON_1[i])
		-- 	else
		-- 		btn:setSprite(GConst.SignConst.SIGN_PAGE_BTN_ICON_ATLAS[i], GConst.SignConst.SIGN_PAGE_BTN_ICON_2[i])
		-- 	end
		-- else
			btn:setActive(false)
		-- end
	end
	local width = BTN_RIGHT + BTN_WIDTH * openCount + BTN_SPACING * (openCount - 1)
	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 SignMainUI: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 SignMainUI:isOpen(type)
	if type == GConst.SignConst.SIGN_SHOW_PAGE.WEEK then
		return DataManager.SignWeekData:isOpen()
	-- elseif type == GConst.SignConst.SIGN_SHOW_PAGE.MONTH then
	-- 	return DataManager.SignMonthData:isOpen()
	end
	return false
end

function SignMainUI:hasRedPoint(type)
	if type == GConst.SignConst.SIGN_SHOW_PAGE.WEEK then
		return DataManager.SignWeekData:showRedPoint()
	-- elseif type == GConst.SignConst.SIGN_SHOW_PAGE.MONTH then
	-- 	return DataManager.SignMonthData:showRedPoint()
	end
	return false
end

return SignMainUI