local CommonAdUI = class("CommonAdUI", BaseUI)

function CommonAdUI:ctor(params)
	params = params or {}

	self.content = params.content or ""
	self.okFunc = params.okFunc
	self.showToday = params.showToday
end

function CommonAdUI:isFullScreen()
    return false
end

function CommonAdUI:showCommonBG()
    return false
end

function CommonAdUI:getPrefabPath()
    return "assets/prefabs/ui/common/common_ad_ui.prefab"
end

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

function CommonAdUI:onLoadRootComplete()
	self:addEventListener(EventManager.CUSTOM_EVENT.NO_AD_ACTIVE, function(params)
		if not self:isClosed() then
			self:hideAdNode()
		end
	end)

	if self.showToday then -- 今天内不在提示
		local time = LocalData:getMessageBoxShowTodayTime(self.showToday)
		local todayTime = Time:getBeginningOfServerToday()
		if time > 0 and time == todayTime then
			local value = LocalData:getMessageBoxShowTodayValue(self.showToday)
			if value == 1 then
				if self.okFunc then
					self.okFunc()
				end
				self:closeUI()
				return
			else
				-- 不作处理
			end
		end
	end

	local selected = false
	local uiMap = self.root:genAllChildren()

	uiMap["message_box.title_bg_img.title_text"]:setText(I18N:getGlobalText(I18N.GlobalConst.MESSAGE_BOX_TITLE))
	uiMap["message_box.title_bg_img.bg.content_tx"]:setText(self.content)
	uiMap["message_box.content.close_btn"]:addClickListener(function()
		if self.showToday and selected then
			local todayTime = Time:getBeginningOfServerToday()
			LocalData:setMessageBoxShowTodayTime(self.showToday, todayTime)
			LocalData:setMessageBoxShowTodayValue(self.showToday, 0)
		end
		self:closeUI()
	end)
	uiMap["message_box.content.ad_btn"]:addClickListener(function()
		if self.showToday and selected then
			local todayTime = Time:getBeginningOfServerToday()
			LocalData:setMessageBoxShowTodayTime(self.showToday, todayTime)
			LocalData:setMessageBoxShowTodayValue(self.showToday, 1)
		end
		if self.okFunc then
			self.okFunc()
		end
		self:closeUI()
	end)
	uiMap["common_ad_ui.content.cancel_btn"]:addClickListener(function()
		if self.showToday and selected then
			local todayTime = Time:getBeginningOfServerToday()
			LocalData:setMessageBoxShowTodayTime(self.showToday, todayTime)
			LocalData:setMessageBoxShowTodayValue(self.showToday, 0)
		end
		self:closeUI()
	end)

	if self.showToday then -- 今天内不在提示
		uiMap["message_box.title_bg_img.bg"]:setSizeDeltaY(216)
		uiMap["message_box.title_bg_img.today"]:setVisible(true)
		uiMap["message_box.title_bg_img.today.check.select"]:setVisible(false)

		local checkUI = uiMap["message_box.title_bg_img.today.check"]
		checkUI:addClickListener(function()
			selected = not selected
			Logger.logHighlight("%s", selected)
			uiMap["message_box.title_bg_img.today.check.select"]:setVisible(selected)
		end)

		local textUI = uiMap["message_box.title_bg_img.today.text"]
		textUI:setText(I18N:getGlobalText(I18N.GlobalConst.CONFIRM_IGNORE))

		GFunc.centerImgAndTx(checkUI, textUI, 5)
	else
		uiMap["message_box.title_bg_img.bg"]:setSizeDeltaY(246)
		uiMap["message_box.title_bg_img.today"]:setVisible(false)
	end

	-- 广告提示
	uiMap["common_ad_ui.content.cancel_btn.tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_CANCEL))
	uiMap["message_box.content.ad_btn.tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.AD_FREE_DESC_9))

	if DataManager.PlayerData:getNoAdFuncOpen() and not DataManager.PlayerData:getNoAdActive() then
		uiMap["message_box.no_ad_node"]:setVisible(true)
		GFunc.setAdsSprite(uiMap["message_box.content.ad_btn.icon"])
		uiMap["message_box.no_ad_node.tx_ad"]:setText(I18N:getGlobalText(I18N.GlobalConst.ADS_DESC_15))
		uiMap["message_box.no_ad_node.tx_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ADS_DESC_16))
		uiMap["message_box.no_ad_node.btn_buy"]:addClickListener(function()
			ModuleManager.PrivilegeCardManager:buyAdCard()
		end)
		uiMap["message_box.no_ad_node.btn_buy.tx_desc"]:setText(DataManager.PlayerData:getNoAdPrice())
	else
		self:hideAdNode()
	end	
end

function CommonAdUI:hideAdNode()
	local uiMap = self.root:genAllChildren()
	uiMap["message_box.no_ad_node"]:setVisible(false)
end

function CommonAdUI:onClose()
	self.okFunc = nil
	self.showToday = nil
end

return CommonAdUI