local PrivilegeMainUI = class("PrivilegeMainUI", BaseUI)

function PrivilegeMainUI:showCommonBG()
	return false
end

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

function PrivilegeMainUI:getCurrencyParams()
	local params = {}
	params.showType = GConst.CURRENCY_TYPE.HORIZONTAL
	params.itemIds = {
		GConst.ItemConst.ITEM_ID_GOLD,
        GConst.ItemConst.ITEM_ID_GEM,
        GConst.ItemConst.ITEM_ID_VIT,
	}
	return params, true
end

function PrivilegeMainUI:getPrefabPath()
	return "assets/prefabs/ui/privilege/privilege_main_ui.prefab"
end

function PrivilegeMainUI:onClose()
	EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.MAIN_UI_CHECK_POP)
end

function PrivilegeMainUI:ctor(param)
	if param and param.showPage then
		self.showPage = param.showPage
	end
end

function PrivilegeMainUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	self.btnClose = uiMap["privilege_main_ui.bottom.btn_close"]

	self.scrollrectNode = uiMap["privilege_main_ui.scrollrect"]
	self.content = uiMap["privilege_main_ui.scrollrect.viewport.content"]
	self.freeDailyRewards = uiMap["privilege_main_ui.scrollrect.viewport.content.bg.free_daily_rewards"]
	self.adCard = uiMap["privilege_main_ui.scrollrect.viewport.content.bg.ad_card"]
	self.monthlyCard = uiMap["privilege_main_ui.scrollrect.viewport.content.bg.monthly_card"]
	-- self.lifetimeCard = uiMap["privilege_main_ui.scrollrect.viewport.content.bg.lifetime_card"]
	-- self.challegeCard = uiMap["privilege_main_ui.scrollrect.viewport.content.bg.challege_card"]

	self.btnAll = uiMap["privilege_main_ui.bottom.btn_all"]
	self.btnAll:addClickListener(function()
		ModuleManager.PrivilegeCardManager:getPurchaseCardDailyReward(0)
	end)
	self.btnAllTx = uiMap["privilege_main_ui.bottom.btn_all.text"]
	self.btnAllTx:setText(I18N:getGlobalText(I18N.GlobalConst.ONE_KEY_GET_DESC))
	self.btnAllGrey = uiMap["privilege_main_ui.bottom.btn_all_grey"]
	self.btnAllGreyTx = uiMap["privilege_main_ui.bottom.btn_all_grey.text"]
	self.btnAllGreyTx:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_DONE))

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

	self.btnClose:addClickListener(function()
		self:closeUI()
	end)
	self:bind(DataManager.PrivilegeCardData, "isDirty", function()
		self:onRefresh()
		self:updateCurrencyBar()
	end)
	self:bind(DataManager.PaymentData, "isDirty", function()
		self:onRefresh()
	end)
	if self.page then
		BIReportV2:postOperation(BIReportV2.OPERATION_UI_NAME["PRIVILEGE_CARD_" .. self.page])
	else
		BIReportV2:postOperation(BIReportV2.OPERATION_UI_NAME.PRIVILEGE_CARD)
	end
end

function PrivilegeMainUI:updateTime()
	if self.monthlyCardComp then
		self.monthlyCardComp:updateTime()
	end
	if self.challegeCardComp then
		self.challegeCardComp:updateTime()
	end
end

function PrivilegeMainUI:onRefresh()
	local height = -20
	height = self:updateFreeDailyRewards(height)
	height = self:updateAdCard(height)
	height = self:updateMonthlyCard(height)
	-- height = self:updateLifetimeCard(height)
	-- height = self:updateChallegeCard(height)
	height = height - 50

	self.content:setSizeDeltaY(math.abs(height))

	local scrollHeight = self.scrollrectNode:getRectHeight()
	local maxPosY = math.abs(height) - scrollHeight

	if self.beginPosY then
		self.content:setAnchoredPositionY(math.min(maxPosY, self.beginPosY))
	end
	self.beginPosY = nil
	self.showPage = nil

	local haveUnclaimedRewards = false
	if not DataManager.PrivilegeCardData:getIsClaimedFreeDailyReward() then
		haveUnclaimedRewards = true
	end
	if DataManager.PrivilegeCardData:getIsMonthlyCardOpen() then
		if DataManager.PrivilegeCardData:getIsMonthlyCardActive() then
			local remainTime = DataManager.PrivilegeCardData:getCardRemainTime(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD)
			if remainTime > 0 then
				local isClaimed = DataManager.PrivilegeCardData:getIsClaimedMonthlyCardDailyReward()
				if not isClaimed then
					haveUnclaimedRewards = true
				end
			end
		end
	end
	-- if DataManager.PrivilegeCardData:getIsLifetimeCardOpen() then
	-- 	local isActive = DataManager.PrivilegeCardData:getIsLifetimeCardActive()
	-- 	if isActive then
	-- 		local isClaimed = DataManager.PrivilegeCardData:getIsClaimedLifetimeCardDailyReward()
	-- 		if not isClaimed then
	-- 			haveUnclaimedRewards = true
	-- 		end
	-- 	end
	-- end
	self.btnAll:setActive(haveUnclaimedRewards)
	if haveUnclaimedRewards then
		self.btnAll:addRedPoint(96, 36, 1)
	else
		self.btnAll:removeRedPoint()
	end
	self.btnAllGrey:setActive(not haveUnclaimedRewards)
end

function PrivilegeMainUI:updateFreeDailyRewards(offsetY)
	if self.showPage == GConst.ShopConst.PRIVILEGE_SHOW_PAGE.FREE_DAILY_REWARDS then
		self.beginPosY = math.abs(offsetY)
	end

	self.freeDailyRewards:setAnchoredPositionY(offsetY)
	offsetY = offsetY - 250

	local cell = self.freeDailyRewards:addLuaComponent(GConst.ShopConst.PRIVILEGE_COMP.FREE_DAILY_REWARDS)
	cell:refresh()

	return offsetY
end

function PrivilegeMainUI:updateAdCard(offsetY)
	self.adCard:setActive(false)
	if not DataManager.PrivilegeCardData:getIsAdCardOpen() then
		return offsetY
	end
	self.adCard:setActive(true)
	if self.showPage == GConst.ShopConst.PRIVILEGE_SHOW_PAGE.CARD_AD then
		self.beginPosY = math.abs(offsetY)
	end

	self.adCard:setAnchoredPositionY(offsetY)
	offsetY = offsetY - 440

	local cell = self.adCard:addLuaComponent(GConst.ShopConst.PRIVILEGE_COMP.CARD_AD)
	cell:refresh()

	return offsetY
end

function PrivilegeMainUI:updateMonthlyCard(offsetY)
	self.monthlyCard:setActive(false)
	if not DataManager.PrivilegeCardData:getIsMonthlyCardOpen() then
		return offsetY
	end
	self.monthlyCard:setActive(true)
	if self.showPage == GConst.ShopConst.PRIVILEGE_SHOW_PAGE.MONTHLY_CARD then
		self.beginPosY = math.abs(offsetY)
	end

	self.monthlyCard:setAnchoredPositionY(offsetY)
	offsetY = offsetY - 450

	self.monthlyCardComp = self.monthlyCard:addLuaComponent(GConst.ShopConst.PRIVILEGE_COMP.MONTHLY_CARD)
	self.monthlyCardComp:refresh()

	return offsetY
end

-- function PrivilegeMainUI:updateLifetimeCard(offsetY)
-- 	self.lifetimeCard:setActive(false)
-- 	if not DataManager.PrivilegeCardData:getIsLifetimeCardOpen() then
-- 		return offsetY
-- 	end
-- 	self.lifetimeCard:setActive(true)
-- 	if self.showPage == GConst.ShopConst.PRIVILEGE_SHOW_PAGE.LIFETIME_CARD then
-- 		self.beginPosY = math.abs(offsetY)
-- 	end

-- 	self.lifetimeCard:setAnchoredPositionY(offsetY)
-- 	offsetY = offsetY - 472

-- 	local cell = self.lifetimeCard:addLuaComponent(GConst.ShopConst.PRIVILEGE_COMP.LIFETIME_CARD)
-- 	cell:refresh()

-- 	return offsetY
-- end

-- function PrivilegeMainUI:updateChallegeCard(offsetY)
-- 	self.challegeCard:setActive(false)
-- 	if not DataManager.PrivilegeCardData:getIsChallengeCardOpen() then
-- 		return offsetY
-- 	end
-- 	self.challegeCard:setActive(true)
-- 	if self.showPage == GConst.ShopConst.PRIVILEGE_SHOW_PAGE.CHALLEGE_CARD then
-- 		self.beginPosY = math.abs(offsetY)
-- 	end

-- 	self.challegeCard:setAnchoredPositionY(offsetY)
-- 	offsetY = offsetY - 420

-- 	self.challegeCardComp = self.challegeCard:addLuaComponent(GConst.ShopConst.PRIVILEGE_COMP.CHALLEGE_CARD)
-- 	self.challegeCardComp:refresh()

-- 	return offsetY
-- end

function PrivilegeMainUI:refreshRedPoint()
end

function PrivilegeMainUI:isOpen(type)
end

function PrivilegeMainUI:hasRedPoint(type)
end

return PrivilegeMainUI