local GoldCell = class("GoldCell", BaseCell)

function GoldCell:init()
	local uiMap = self:getUIMap()
	self.content = uiMap["gold_cell.content"]
	self.img = uiMap["gold_cell.content.img"]
	self.txReward = uiMap["gold_cell.content.tx_reward"]
	self.txDesc = uiMap["gold_cell.content.desc.tx_desc"]
	self.costNode = uiMap["gold_cell.content.cost"]
	self.imgCost = uiMap["gold_cell.content.cost.img_cost"]
	self.txCost = uiMap["gold_cell.content.cost.tx_cost"]

	self.baseObject:addClickListener(function()
		if DataManager.ShopData:getGoldBuyRemainCountByFree(self.id) > 0 then
			-- 免费
			ModuleManager.ShopManager:reqBuyGoldGift(self.id)
			
		elseif DataManager.ShopData:getGoldBuyRemainCountByAd(self.id) > 0 then
			-- 广告免费
			SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.AD_SHOP_GOLD, function ()
				ModuleManager.ShopManager:reqBuyGoldGift(self.id)
			end)
		else
			-- 付费
			local cost = DataManager.ShopData:getGoldConfig(self.id).cost
			if not GFunc.checkCost(GFunc.getRewardId(cost), GFunc.getRewardNum(cost), true) then
				return
			end

			local params = {}
			params.content = I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_33)
			params.boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL
			params.showToday = GConst.MESSAGE_BOX_SHOW_TODAY.SHOP_GOLD
			params.okFunc = function()
				ModuleManager.ShopManager:reqBuyGoldGift(self.id)
			end
			GFunc.showMessageBox(params)
		end
	end)
end

function GoldCell:refresh(id)
	self.id = id
	local cfg = DataManager.ShopData:getGoldConfig(self.id)
	local reward = cfg.gold

	self.img:setSprite(GConst.ATLAS_PATH.UI_SHOP, "shop_species_" .. self.id)
	self.txDesc:setText(I18N:getText("mall_gold", self.id, "name"))
	self.txReward:setText(GFunc.getRewardNum(reward))

	local totalFreeCount = DataManager.ShopData:getBuyGoldFreeCount(self.id)
	local remainFreeCount = DataManager.ShopData:getGoldBuyRemainCountByFree(self.id)
	local totalAdCount = DataManager.ShopData:getBuyGoldAdCount(self.id)
	local remainAdCount = DataManager.ShopData:getGoldBuyRemainCountByAd(self.id)
	if remainFreeCount > 0 then
		-- 免费
		self.baseObject:addRedPoint(80, 120, 1.2)
		self.imgCost:setActive(false)
		self.txCost:setText(I18N:getGlobalText(I18N.GlobalConst.FREE_DESC) .. "(" .. remainFreeCount .. "/" .. totalFreeCount .. ")")
		self.txCost:setAnchoredPositionX(0)
	elseif remainAdCount > 0 then
		-- 广告免费
		if SDKManager:isAdLoaded() then
			self.baseObject:addRedPoint(80, 120, 1.2)
		else
			self.baseObject:removeRedPoint()
		end

		self.imgCost:setActive(true)
		GFunc.setAdsSprite(self.imgCost)
		self.txCost:setText("(" .. remainAdCount .. "/" .. totalAdCount .. ")")
		GFunc.centerImgAndTx(self.imgCost, self.txCost, 3)
	else
		-- 付费
		local cost = cfg.cost
		self.baseObject:removeRedPoint()
		self.imgCost:setActive(true)
		self.imgCost:setSprite(GFunc.getIconResByReward(cost))
		self.txCost:setText(GFunc.getRewardNum(cost))
		GFunc.centerImgAndTx(self.imgCost, self.txCost, 3)
	end
end

return GoldCell