local GoldCell = class("GoldCell", BaseCell)

function GoldCell:init()
	local uiMap = self.baseObject:genAllChildren()
	self.bg = uiMap["gold_cell.bg"]
    self.icon = uiMap["gold_cell.icon"]
    self.nameText = uiMap["gold_cell.num"]
    self.costNode = uiMap["gold_cell.cost"]
	self.costImg = uiMap["gold_cell.cost.icon"]
	self.costText = uiMap["gold_cell.cost.num"]
    self.adNode = uiMap["gold_cell.ad"]
	self.adImg = uiMap["gold_cell.ad.image"]
	self.adText = uiMap["gold_cell.ad.desc"]
	self.descText = uiMap["gold_cell.desc"]
	self.sellOutText = uiMap["gold_cell.sell_out"]

    self.sellOutText:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_20)) -- 已购买
end

function GoldCell:refresh(id, cfgInfo)
	local idleTime = cfgInfo.idel_time
	local cost = cfgInfo.cost
	local adMaxTimes = cfgInfo.daily or 0
	local goldNum = DataManager.ShopData:getCommonDailyCoinNum(id)

	self.icon:setSprite(GConst.ATLAS_PATH.SHOP, GConst.ShopConst.COIN_ICON_NAME[id])

	local hasDaily = adMaxTimes > 0 
	local adBought = DataManager.ShopData:getCommonDailyCoinAdBuyCount() -- 金币礼包AD购买次数
	local isAd = hasDaily and adMaxTimes > adBought
	if isAd then -- 免费广告
		self.adNode:setVisible(true)
		self.costNode:setVisible(false)

		GFunc.setAdsSprite(self.adImg)
		local adLeftCount = adMaxTimes - adBought
		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.costNode:setVisible(true)

		self.costText:setText(tostring(cost.num))
		GFunc.centerImgAndTx(self.costImg, self.costText, 5)
		self.sellOutText:setVisible(false)

		self:getBaseObject():removeRedPoint()
	end
	self.nameText:setText(GFunc.num2Str(goldNum))
	
	self.descText:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_14)) -- 金币礼包

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

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

function GoldCell:onClickGift(id, isAd)
	local cfg = DataManager.ShopData:getMallGoldConfig()
	local cfgInfo = cfg[id]
	if isAd then
		local adMaxTimes = cfgInfo and cfgInfo.daily or 0
		local bought = DataManager.ShopData:getCommonDailyCoinAdBuyCount() -- 金币礼包购买次数
		local adLeftCount = adMaxTimes - bought
		if adLeftCount > 0 then
			SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.MALL_GOLD, function ()
				ModuleManager.ShopManager:buyMallIdleCoin(id, isAd)
			end)
		end
	else
		local costs = cfgInfo and cfgInfo.cost or {}
		if not GFunc.checkCost(costs.id, costs.num, true) then
			return
		end
		-- 二次提示
		local params = {
			content = I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_33), -- 是否使用钻石刷新每日特惠?
			boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL,
			okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
			okFunc = function()
				ModuleManager.ShopManager:buyMallIdleCoin(id, isAd)
			end
		}
		GFunc.showMessageBox(params)
	end
end

return GoldCell