local ActSevenDayUI = class("ActSevenDayUI", BaseUI)

local TASK_COMP = "app/ui/activity/act_seven_day/component/task_comp"
local GIFT_COMP = "app/ui/activity/act_seven_day/component/gift_comp"
local DAY_COMP = "app/ui/activity/act_seven_day/component/day_comp"

function ActSevenDayUI:getPeriod()
	return 1
end

function ActSevenDayUI:isFullScreen()
	return false
end

function ActSevenDayUI:onPressBackspace()
	self:onClickClose()
end

function ActSevenDayUI:ctor()
end

function ActSevenDayUI:getPrefabPath()
	return "assets/prefabs/ui/activity/act_sevenday/act_sevenday_ui.prefab"
end

function ActSevenDayUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	uiMap["act_sevenday_ui.d_menu.d_menu_1.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.TASK_DESC_1))
	uiMap["act_sevenday_ui.d_menu.d_menu_1.desc_tx_choose"]:setText(I18N:getGlobalText(I18N.GlobalConst.TASK_DESC_1))
	uiMap["act_sevenday_ui.d_menu.d_menu_2.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.GIFT_DESC_4))
	uiMap["act_sevenday_ui.d_menu.d_menu_2.desc_tx_choose"]:setText(I18N:getGlobalText(I18N.GlobalConst.GIFT_DESC_4))
	if self:getPeriod() == 1 then
		uiMap["act_sevenday_ui.bg.title_img.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_SEVENDAY))
	else
		uiMap["act_sevenday_ui.bg.title_img.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_SEVENDAY_2))
	end
	self.timeTx = uiMap["act_sevenday_ui.bg.time_bg.time_tx"]
	self.timeBg = uiMap["act_sevenday_ui.bg.time_bg"]
	uiMap["act_sevenday_ui.bg.time_bg.help"]:addClickListener(function()
		local params = {
			desc = I18N:getGlobalText(I18N.GlobalConst.ACT_SEVENDAY_HELP)
		}
		ModuleManager.TipsManager:showHelpTips(params)
	end)

	self:initComps()
	self:_display()
	self:_addListeners()
	self:_bind()

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

function ActSevenDayUI:_display()
	local page = 1
	if DataManager.ActSevenDayData:hasTaskRp(self:getPeriod()) then
		page = 1
	end
	self:switchComp(page)
	self:refreshTitle()
end

function ActSevenDayUI:_addListeners()
	local uiMap = self.root:genAllChildren()
	uiMap["act_sevenday_ui.mask"]:addClickListener(function()
		self:onClickClose()
	end)
	uiMap["act_sevenday_ui.close_btn"]:addClickListener(function()
		self:onClickClose()
	end)

	self:addEventListener(EventManager.CUSTOM_EVENT.CHANGE_MAIN_CITY_PAGE, function()
		self:closeUI()
	end)
end

function ActSevenDayUI:onClickClose()
	self:closeUI()
	EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.MAIN_UI_CHECK_POP)
end

function ActSevenDayUI:_bind()
	self:bind(DataManager.ActSevenDayData, "isDirty", function()
		if not self.day or not self.page then
			return
		end
		if self:getMaxShowDay() < self.day then
			self.day = self:getMaxShowDay()
		end
		self:refreshTitle()
		self:refreshBottoms()
		self:refreshDayComp(self.day)
		self.subComps[self.page]:refresh(self, self.day, self:getPeriod())
	end)
	self:bind(DataManager.PlayerData, "isDirty", function()
		if not self.day or not self.page then
			return
		end
		if self:getMaxShowDay() < self.day then
			self.day = self:getMaxShowDay()
		end
		self:refreshTitle()
		self:refreshBottoms()
		self:refreshDayComp(self.day)
		self.subComps[self.page]:refresh(self, self.day, self:getPeriod())
	end)
	self:bind(DataManager.PaymentData, "isDirty", function()
		if not self.day or not self.page then
			return
		end
		if self:getMaxShowDay() < self.day then
			self.day = self:getMaxShowDay()
		end
		self:refreshTitle()
		self:refreshBottoms()
		self:refreshDayComp(self.day)
		self.subComps[self.page]:refresh(self, self.day, self:getPeriod())
	end)
end

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

	local taskComp = uiMap["act_sevenday_ui.task_comp"]
	taskComp:initPrefabHelper()
	taskComp:genAllChildren()
	self.subComps[1] = taskComp:addLuaComponent(TASK_COMP)

	local giftComp = uiMap["act_sevenday_ui.gift_comp"]
	giftComp:initPrefabHelper()
	giftComp:genAllChildren()
	self.subComps[2] = giftComp:addLuaComponent(GIFT_COMP)

	local dayComp = uiMap["act_sevenday_ui.r_menu"]
	dayComp:initPrefabHelper()
	dayComp:genAllChildren()
	self.dayComp = dayComp:addLuaComponent(DAY_COMP)
end

function ActSevenDayUI:refreshTitle()
end

function ActSevenDayUI:refreshBottoms()
	if not self.bottomBtns then
		local uiMap = self.root:genAllChildren()
		self.bottomBtns = {}
		self.bgs = {}
		self.hlBgs = {}
		self.txs = {}
		self.hlTxs = {}
		self.rps = {}
		for i = 1, 2 do
			local prefix = "act_sevenday_ui.d_menu.d_menu_" .. i
			self.bottomBtns[i] = uiMap[prefix]
			self.bottomBtns[i]:addClickListener(function()
				AudioManager:playEffect(AudioManager.EFFECT_ID.UI_BTN_SWITCH_2)
				self:switchComp(i)
			end)
			self.bgs[i] = uiMap[prefix .. ".d_menu"]
			self.hlBgs[i] = uiMap[prefix .. ".d_menu_choose"]
			self.txs[i] = uiMap[prefix .. ".desc_tx"]
			self.hlTxs[i] = uiMap[prefix .. ".desc_tx_choose"]
			self.rps[i] = uiMap[prefix .. ".point_img"]
		end
	end

	for index = 1, 2 do
		local select = index == self.page
		self.hlBgs[index]:setVisible(select)
		self.txs[index]:setVisible(not select)
		self.bgs[index]:setVisible(not select)
		self.hlTxs[index]:setVisible(select)
		if index == 1 then
			self.rps[index]:setVisible(DataManager.ActSevenDayData:hasTaskRp(self:getPeriod()))
		elseif index == 2 then
			self.rps[index]:setVisible(DataManager.ActSevenDayData:hasGiftRp(self:getPeriod()))
		end
	end
end

function ActSevenDayUI:switchComp(page, force)
	if self.page == page and not force then
		return
	end
	self.page = page
	local day = self:getMaxShowDay()
	day = self:refreshDayComp() or day
	self.day = day
	for index, comp in ipairs(self.subComps) do
		comp:getBaseObject():setActive(index == page)
		comp:refresh(self, day, self:getPeriod())
	end
	self:refreshBottoms()

	if self:getPeriod() == 1 then -- 第一期
		if self.page == 1 then
			BIReportV2:postOperation(BIReportV2.OPERATION_UI_NAME.ACT_SEVENDAY_TASK)
		else
			BIReportV2:postOperation(BIReportV2.OPERATION_UI_NAME.ACT_SEVENDAY_GIFT)
		end
	else
		if self.page == 1 then
			BIReportV2:postOperation(BIReportV2.OPERATION_UI_NAME.ACT_SEVENDAY_TASK_2)
		else
			BIReportV2:postOperation(BIReportV2.OPERATION_UI_NAME.ACT_SEVENDAY_GIFT_2)
		end
	end
end

function ActSevenDayUI:refreshDayComp(targetDay)
	local maxShowDay = self:getMaxShowDay()
	local dataInfo = {}
	local minRpDay = maxShowDay
	for day = 1, 7 do
		local over = false
		local rp = false
		if day <= maxShowDay then
			over = true
			if self.page == 1 then
				local tasks = DataManager.ActSevenDayData:getTaskIdsByDay(day, self:getPeriod())
				if tasks then
					for _, id in ipairs(tasks) do
						if not DataManager.ActSevenDayData:isTaskReceived(id) then
							over = false
						end

						if DataManager.ActSevenDayData:canClaimTask(id) then
							rp = true
							if minRpDay > day then
								minRpDay = day
							end
							break
						end
					end

				end
			elseif self.page == 2 then
				rp = DataManager.ActSevenDayData:hasGiftDayRp(day, self:getPeriod())
				over = DataManager.ActSevenDayData:getIsGiftAllOverDay(day, self:getPeriod())
				if rp and minRpDay > day then
					minRpDay = day
				end
			end
		end

		table.insert(dataInfo, {
			over = over,
			rp = rp,
			func = function(selectDay)
				self.day = selectDay
				self.subComps[self.page]:refresh(self, selectDay, self:getPeriod())
			end
		})
	end
	targetDay = targetDay or minRpDay
	self.dayComp:refresh(self, dataInfo, targetDay, maxShowDay, self:getPeriod())
	return minRpDay
end

function ActSevenDayUI:getMaxShowDay()
	local day = DataManager.ActSevenDayData:getActDay(self:getPeriod())
	if day <= 0 or day >= 7 then
		day = 7
	end
	return day
end

function ActSevenDayUI:updateTime()
	local cd = DataManager.ActSevenDayData:getRemainTime(self:getPeriod())
	local timeStr = GConst.EMPTY_STRING
	local remainDay = -1
	if cd <= 0 then
		timeStr = I18N:getGlobalText(I18N.GlobalConst.ADVENTURE_CLOSED)
	else
		remainDay = cd // 86400
		timeStr = I18N:getGlobalText(I18N.GlobalConst.TIME_END_DESC_1, GFunc.getTimeStr(cd))
	end
	self.timeTx:setText(timeStr)
	if remainDay >= 0 and self.remainDay ~= remainDay then
		self.remainDay = remainDay
		local width = self.timeTx:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredWidth
		self.timeBg:setSizeDeltaX(width + 74)
	end
end

return ActSevenDayUI