local DailyCell = class("DailyCell", BaseCell)

function DailyCell:init()
	local uiMap = self:getUIMap()
	self.content = uiMap["daily_cell.content"]
	self.txName = uiMap["daily_cell.content.tx_name"]
	self.rewardCell = uiMap["daily_cell.content.reward_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
	self.txLimit = uiMap["daily_cell.content.tx_limit"]
	self.costNode = uiMap["daily_cell.content.cost"]
	self.imgCost = uiMap["daily_cell.content.cost.img_cost"]
	self.txCost = uiMap["daily_cell.content.cost.tx_cost"]
	self.tag = uiMap["daily_cell.content.tag"]
	self.txOff = uiMap["daily_cell.content.tag.tx_off"]
	self.imgEmpty = uiMap["daily_cell.content.img_empty"]
	self.txLock = uiMap["daily_cell.content.tx_lock"]
end

function DailyCell:refresh(id)
	self.id = id

	local reward = DataManager.ShopData:getDailyReward(self.id)
	if reward then
		self.rewardCell:refreshByConfig(reward)
		if reward.type == GConst.REWARD_TYPE_NUM.ITEM then
			self.txName:setText(I18N:getConfig("item")[reward.id].name)
		elseif reward.type == GConst.REWARD_TYPE_NUM.EQUIP then
			self.txName:setText(I18N:getConfig("equip")[reward.id].name)
		end
	end

	local cfg = DataManager.ShopData:getDailyConfig(self.id)
	local maxCount = DataManager.ShopData:getDailyCount(self.id)
	local remainCount = math.max(0, DataManager.ShopData:getDailyRemainCount(self.id))
	local adCount = cfg.limit_ad or 0
	local cost = DataManager.ShopData:getDailyCost(self.id)

	self.txLimit:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_GOGOGO_DESC_5, remainCount, maxCount))

	if remainCount <= 0 then
		-- 告罄
		self.imgEmpty:setActive(true)
		self.baseObject:setTouchEnable(false)
		self.baseObject:removeRedPoint()
		self.imgCost:setActive(false)
		self.txCost:setText("<color=#FF8F8F>" .. I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_20) .. "</color>")
		self.txCost:setAnchoredPositionX(0)
		self.txCost:setSizeDeltaX(150)
	elseif not cost or cost.num <= 0 then
		self.imgEmpty:setActive(false)
		self.baseObject:setTouchEnable(true)
		if remainCount > adCount then
			self.baseObject:addRedPoint(84, 118, 1.2)
			-- 纯免费
			self.imgCost:setActive(false)
			self.txCost:setText(I18N:getGlobalText(I18N.GlobalConst.FREE_DESC))
			self.txCost:setAnchoredPositionX(0)
		else
			if SDKManager:isAdLoaded() then
				self.baseObject:addRedPoint(84, 118, 1.2)
			else
				self.baseObject:removeRedPoint()
			end

			-- 广告购买
			self.imgCost:setActive(true)
			self.imgCost:setSizeDelta(45, 45)
			GFunc.setAdsSprite(self.imgCost)

			self.txCost:setText("(" .. remainCount .. "/" .. adCount .. ")")
			GFunc.centerImgAndTx(self.imgCost, self.txCost, 0)
		end
		

		self.baseObject:addClickListener(function()
			if remainCount > adCount then -- 纯免费
				self:onBuy()
			elseif remainCount > 0 then -- 广告免费
				SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.AD_DAILY_SHOP, function ()
					self:onBuy()
				end)
			else -- 付费
				self:onBuy()
			end
		end)
	else
		-- 道具购买
		self.imgEmpty:setActive(false)
		self.baseObject:setTouchEnable(true)
		self.baseObject:removeRedPoint()

		self.imgCost:setActive(true)
		self.imgCost:setSizeDelta(60, 60)
		self.imgCost:setSprite(GFunc.getIconResByReward(cost))
		self.txCost:setText(GFunc.getRewardNum(cost))
		GFunc.centerImgAndTx(self.imgCost, self.txCost, 0)

		self.baseObject:addClickListener(function()
			local remainCount = math.max(0, DataManager.ShopData:getDailyRemainCount(self.id))
			if remainCount == 0 then
				return
			end
			if not GFunc.checkCost(GFunc.getRewardId(cost), GFunc.getRewardNum(cost), true) then
				return
			end
			if GFunc.getRewardId(cost) == GConst.ItemConst.ITEM_ID_GEM then
				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_DAILY
				params.okFunc = function()
					self:onBuy()
				end
				GFunc.showMessageBox(params)
			else
				self:onBuy()
			end
		end)
	end

	local locked = DataManager.ShopData:getDailyLocked(self.id)
	if locked then
		self.baseObject:setTouchEnable(false)
		self.txLock:setText(locked or "")
		self.baseObject:removeRedPoint()

		self.txName:setActive(false)
		self.rewardCell:getBaseObject():setActive(false)
		self.costNode:setActive(false)
		self.txLimit:setActive(false)
		self.tag:setActive(false)
	else
		self.baseObject:setTouchEnable(true)
		self.txLock:setText("")

		self.txName:setActive(true)
		self.rewardCell:getBaseObject():setActive(true)
		self.costNode:setActive(true)
		self.txLimit:setActive(true)

		local idx = DataManager.ShopData:getDailyIdx(self.id)
		if cfg.value and cfg.value[idx] and cfg.value[idx] ~= 0 then
			self.tag:setActive(true)
			self.txOff:setText(cfg.value[idx] .. "%")
		else
			self.tag:setActive(false)
		end
	end
end

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

function DailyCell:onBuy()
	ModuleManager.ShopManager:reqBuyDailyGift(self.id)
end

return DailyCell