local GemCell = class("GemCell", BaseCell)

function GemCell:init()
	local uiMap = self.baseObject:genAllChildren()
	self.bg = uiMap["gem_cell.bg"]
    self.icon = uiMap["gem_cell.icon"]
    self.nameText = uiMap["gem_cell.num"]
    self.priceText = uiMap["gem_cell.price"]
	self.doubleNode = uiMap["gem_cell.double_bg"]
    self.doubleDesc = uiMap["gem_cell.double_bg.desc"]
    self.doubleText = uiMap["gem_cell.double_bg.num"] 
    self.doubleImg = uiMap["gem_cell.double_bg.icon"]
    self.doubleOriginText = uiMap["gem_cell.double_bg.origin_num"]
	self.doubleOriginLine = uiMap["gem_cell.double_bg.line"]
    self.adNode = uiMap["gem_cell.ad"]
	self.adImg = uiMap["gem_cell.ad.image"]
	self.adText = uiMap["gem_cell.ad.desc"]
	self.sellOutText = uiMap["gem_cell.sell_out"]

    self.sellOutText:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_20))
end

function GemCell:refresh(id, cfgInfo)
	local rechargeId = cfgInfo.recharge_id 
   	local reward = cfgInfo.reward[1]
	local limit = cfgInfo.limit or 0
	local adMaxTimes = cfgInfo.daily or 0
	local bought = DataManager.ShopData:getGiftBoughtNum(PayManager.PURCHARSE_TYPE.MALL_TREASURE, id)
		
	local isFree = rechargeId == nil
	local hasDoubleTimes = bought < limit
	local leftDoubleTimes = limit - bought
	
	self.icon:setSprite(GConst.ATLAS_PATH.SHOP, GConst.ShopConst.GEM_ICON_NAME[id])
	if isFree then -- 免费广告
		self.adNode:setVisible(true)
		self.priceText:setVisible(false)
		self.doubleNode:setVisible(false)

		GFunc.setAdsSprite(self.adImg)
		local adLeftCount = adMaxTimes - bought
		if adLeftCount > 0 then
			self.adText:setText(I18N:getGlobalText(I18N.GlobalConst.STR_FREE) .. "(" .. tostring(adLeftCount) .. ")") -- 免费(x)
			GFunc.centerImgAndTx(self.adImg, self.adText, 5)
			self.sellOutText:setVisible(false)

			self:getBaseObject():addRedPoint(100, 145, 0.6)
		else
			self.adNode:setVisible(false)
			self.sellOutText:setVisible(true)

			self:getBaseObject():removeRedPoint()
		end
	else -- 付费
		self.adNode:setVisible(false)
		self.priceText:setVisible(true)

		if hasDoubleTimes then -- 有双倍效果
			self.doubleNode:setVisible(true)
			self.doubleDesc:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_21, tostring(leftDoubleTimes))) -- 剩余次数:{0}
			self.doubleText:setText("+" .. tostring(reward.num * 2))
			-- GFunc.centerImgAndTx(self.doubleImg, self.doubleText, 5)
			self.doubleOriginText:setText(reward.num)
			self.doubleOriginLine:setVisible(true)

			self.doubleDesc:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO):ForceMeshUpdate()
			local txW = self.doubleDesc:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).renderedWidth
			local setWidth = txW + 30
			local curWidth = self.doubleNode:getSizeDeltaX()
			if setWidth > curWidth then
				self.doubleNode:setSizeDeltaX(setWidth)
			end
		else
			self.doubleNode:setVisible(false)
			self.doubleOriginLine:setVisible(false)
		end
		self.sellOutText:setVisible(false)
	end
	self.nameText:setText(reward.num)
	self.priceText:setText(GFunc.getFormatPrice(rechargeId))

    self:addClickListener(function()
		self:onClickGift(id, isFree)
	end)
end

function GemCell:onClickGift(id, isAd)
	if isAd then
		local cfgInfo = DataManager.ShopData:getMallTreasureConfig()[id]
		local adMaxTimes = cfgInfo.daily or 0
		local bought = DataManager.ShopData:getGiftBoughtNum(PayManager.PURCHARSE_TYPE.MALL_TREASURE, id)
		local adLeftCount = adMaxTimes - bought
		if adLeftCount > 0 then
			SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.MALL_TREASURE, function ()
				PayManager:purchasePackage(id, PayManager.PURCHARSE_TYPE.MALL_TREASURE)
			end)
		end
	else
		PayManager:purchasePackage(id, PayManager.PURCHARSE_TYPE.MALL_TREASURE)
	end
end

function GemCell:setVisible(visible)
	self.baseObject:setVisible(visible)
end

return GemCell