local GiftCell = class("GiftCell", BaseCell)

function GiftCell:init()
	local uiMap = self:getUIMap()

	self.tagBg = uiMap["gift_cell.tag_bg"]
	self.tagTx = uiMap["gift_cell.tag_bg.tag_tx"]
	self.claimBtn = uiMap["gift_cell.claim_btn"]
	self.claimBtnAdImg = uiMap["gift_cell.claim_btn.ad_img"]
	self.claimBtnTx = uiMap["gift_cell.claim_btn.text"]
	self.claimBtnTxBlue = uiMap["gift_cell.claim_btn.text_blue"]
	self.claimBtnTxGray = uiMap["gift_cell.claim_btn.text_gray"]
	self.limitTx = uiMap["gift_cell.limit_tx"]
	self.claimBtnIcon = uiMap["gift_cell.claim_btn.icon"]
	self.claimBtnNumTx = uiMap["gift_cell.claim_btn.num_tx"]
	self.titleTx = uiMap["gift_cell.title_tx"]
	self.discountBg = uiMap["gift_cell.discount_bg"]
	self.discountBgTx = uiMap["gift_cell.discount_bg.desc_tx"]

	self.rewardCells = {}
	for i = 1, 4 do
		self.rewardCells[i] = uiMap["gift_cell.reward_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
	end

	self.claimBtn:addClickListener(function()
		if self.info == nil then
			return
		end
		if self.info.cfg.parameter_pro and self.info.cfg.parameter_pro[1] == 1 then -- 免费
			ModuleManager.ActSevenDayManager:onGiftBuy(self.info.id)
		elseif self.info.cfg.item_cost then -- 道具购买
			if not GFunc.checkCost(self.info.cfg.item_cost[1].id, self.info.cfg.item_cost[1].num, true) then
				return
			end
			local params = {}
			params.content = I18N:getGlobalText(I18N.GlobalConst.MALL_DESC_13)
			params.boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL
			params.showToday = GConst.MESSAGE_BOX_SHOW_TODAY.ACT_SEVEN_DAY_GIFT
			params.okFunc = function()
				ModuleManager.ActSevenDayManager:onGiftBuy(self.info.id)
			end
			GFunc.showMessageBox(params)
		elseif self.info.cfg.recharge_id == nil then -- 广告
			SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.AD_ACT_SEVEN_DAY_GIFT, function ()
				ModuleManager.ActSevenDayManager:onGiftBuy(self.info.id)
			end)
		elseif self.info.cfg.recharge_id then -- 正常购买
			ModuleManager.ActSevenDayManager:onGiftBuy(self.info.id)
		end
	end)
end

function GiftCell:refresh(info, period)
	self.info = info
	local giftRemainNum = DataManager.ActSevenDayData:getGiftRemainNum(info.id, period)
	self.titleTx:setText(I18N:getText("act_gift", info.id, "value"))
	self.limitTx:setText(I18N:getGlobalText(I18N.GlobalConst.GIFT_ROUTINE_DESC_8) .. giftRemainNum .. "/" .. info.cfg.limit)

	if info.cfg.recharge_id or info.cfg.item_cost then
		self.baseObject:setSprite(GConst.ATLAS_PATH.UI_ACT_SEVENDAY, "act_sevenday_bg_5")
	else
		self.baseObject:setSprite(GConst.ATLAS_PATH.UI_ACT_SEVENDAY, "act_sevenday_bg_4")
	end

	if info.cfg.recommend then
		self.tagBg:setActive(true)
		self.titleTx:setAnchoredPositionX(88)
		if info.cfg.recommend == 1 then
			self.tagTx:setText(I18N:getGlobalText(I18N.GlobalConst.GIFT_ROUTINE_DESC_16))
		else
			self.tagTx:setText("<color=#FFEB11>" .. I18N:getGlobalText(I18N.GlobalConst.GIFT_ROUTINE_DESC_19) .. "</color>")
		end
	else
		self.tagBg:setActive(false)
		self.titleTx:setAnchoredPositionX(24)
	end

	GFunc.setAdsSprite(self.claimBtnAdImg)
	local isClaimAll = giftRemainNum <= 0
	if isClaimAll then
		self.claimBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_1")
		self.claimBtn:setTouchEnable(false)
		self.claimBtnTxGray:setVisible(true)
		self.claimBtnTxGray:setText(I18N:getGlobalText(I18N.GlobalConst.GIFT_ROUTINE_DESC_10))
		self.claimBtnTxGray:setSizeDeltaX(120)
		self.claimBtnTxGray:setAnchoredPositionX(0)
		self.claimBtnTxBlue:setVisible(false)
		self.claimBtnTx:setVisible(false)
		self.claimBtnAdImg:setActive(false)
		self.claimBtnIcon:setActive(false)
		self.claimBtnNumTx:setActive(false)
	else
		self.claimBtn:setTouchEnable(true)
		if info.cfg.parameter_pro and info.cfg.parameter_pro[1] == 1 then -- 免费
			self.claimBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_blue_1")
			self.claimBtnAdImg:setActive(false)
			self.claimBtnIcon:setActive(false)
			self.claimBtnNumTx:setActive(false)
			self.claimBtnTxBlue:setVisible(true)
			self.claimBtnTxBlue:setText(I18N:getGlobalText(I18N.GlobalConst.FREE_DESC))
			self.claimBtnTxBlue:setSizeDeltaX(120)
			self.claimBtnTxBlue:setAnchoredPositionX(0)
			self.claimBtnTx:setVisible(false)
			self.claimBtnTxGray:setVisible(false)
			self.claimBtn:addRedPoint(67, 28, 0.9)
		elseif info.cfg.item_cost then -- 道具购买
			self.claimBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_yellow_1")
			self.claimBtnIcon:setSprite(GFunc.getIconRes(info.cfg.item_cost[1].id))
			self.claimBtnAdImg:setActive(false)
			self.claimBtnIcon:setActive(true)
			self.claimBtnNumTx:setActive(true)
			self.claimBtnTx:setVisible(false)
			self.claimBtnTxGray:setVisible(false)
			self.claimBtnTxBlue:setVisible(false)
			self.claimBtnNumTx:setText(info.cfg.item_cost[1].num)
			GFunc.centerImgAndTx(self.claimBtnIcon, self.claimBtnNumTx, 0, nil, -6)
			self.claimBtn:removeRedPoint()
		elseif info.cfg.recharge_id == nil then -- 广告
			self.claimBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_blue_1")
			self.claimBtnAdImg:setActive(true)
			self.claimBtnIcon:setActive(false)
			self.claimBtnNumTx:setActive(false)
			self.claimBtnTxBlue:setText(I18N:getGlobalText(I18N.GlobalConst.FREE_DESC))
			self.claimBtnTxBlue:setSizeDeltaX(80)
			self.claimBtnTxBlue:setAnchoredPositionX(18)
			self.claimBtnTxBlue:setVisible(true)
			self.claimBtnTx:setVisible(false)
			self.claimBtnTxGray:setVisible(false)
			self.claimBtn:addRedPoint(67, 28, 0.9)
		elseif info.cfg.recharge_id then -- 付费购买
			self.claimBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_yellow_1")
			self.claimBtnAdImg:setActive(false)
			self.claimBtnIcon:setActive(false)
			self.claimBtnNumTx:setActive(false)
			self.claimBtnTx:setText(GFunc.getFormatPrice(info.cfg.recharge_id))
			self.claimBtnTx:setSizeDeltaX(120)
			self.claimBtnTx:setAnchoredPositionX(0)
			self.claimBtnTx:setVisible(true)
			self.claimBtnTxGray:setVisible(false)
			self.claimBtnTxBlue:setVisible(false)
			self.claimBtn:removeRedPoint()
		end
	end

	-- -- 红点
	-- if not isClaimAll and not self.cfg.free and not self.cfg.item_cost and not self.cfg.recharge_id then
	-- 	self.claimBtn:addRedPoint(67, 28, 0.9)
	-- else
	-- 	self.claimBtn:removeRedPoint()
	-- end

	for i = 1, 4 do
		if info.cfg.reward and info.cfg.reward[i] then
			self.rewardCells[i]:setVisible(true)
			self.rewardCells[i]:refreshByConfig(info.cfg.reward[i], isClaimAll, isClaimAll)
		else
			self.rewardCells[i]:setVisible(false)
		end
	end

	if info.cfg.value then
		self.discountBg:setActive(true)
		self.discountBgTx:setText(info.cfg.value .. "%")
	else
		self.discountBg:setActive(false)
	end
end

return GiftCell