local DailyMainCell = class("DailyMainCell", BaseCell)

function DailyMainCell:init()
	local uiMap = self:getUIMap()
	self.dailyCells = {}
	for i = 1, 6 do
		table.insert(self.dailyCells, uiMap["daily_cell.cell_" .. i]:addLuaComponent("app/ui/shop/cell/daily_cell"))
	end
	self.refreshNode = uiMap["daily_cell.refresh_node"]
	self.txTime = uiMap["daily_cell.refresh_node.tx_time"]
	-- 广告刷新
	self.btnRefreshAd = uiMap["daily_cell.refresh_node.btn_refresh_ad"]
	self.imgAd = uiMap["daily_cell.refresh_node.btn_refresh_ad.img_ad"]
	self.txAd = uiMap["daily_cell.refresh_node.btn_refresh_ad.tx_ad"]
	self.txTimeAd = uiMap["daily_cell.refresh_node.btn_refresh_ad.tx_time"]
	-- 免费刷新/钻石刷新
	self.btnRefresh = uiMap["daily_cell.refresh_node.btn_refresh"]
	self.imgCost = uiMap["daily_cell.refresh_node.btn_refresh.img_cost"]
	self.txCost = uiMap["daily_cell.refresh_node.btn_refresh.tx_cost"]
	self.txTimeCost = uiMap["daily_cell.refresh_node.btn_refresh.tx_time"]
	self.txFree = uiMap["daily_cell.refresh_node.btn_refresh.tx_free"]

	self:updateTime()
	self.sid = self.txTime:scheduleGlobal(function ()
		self:updateTime()
	end, 1)

	self.btnRefresh:addClickListener(function()
		if DataManager.ShopData:getDailyFreeRefreshRemainCount() > 0 then
			-- 可以免费刷新
			self:onRefreshDaily(false)
		elseif DataManager.ShopData:getDailyCostRefreshRemainCount() > 0 then
			-- 可以道具刷新
			local cost = DataManager.ShopData:getDailyRefreshCost()
			if not GFunc.checkCost(GFunc.getRewardId(cost), GFunc.getRewardNum(cost), true) then
				return
			end
			self:onRefreshDaily(false)
		end
	end)

	self.btnRefreshAd:addClickListener(function()
		if DataManager.ShopData:getDailyAdRefreshRemainCount() <= 0 then
			return
		end
		SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.AD_DAILY_SHOP_REFRESH, function()
			self:onRefreshDaily(true)
		end)
	end)
end

function DailyMainCell:updateTime()
	local remainTime = DataManager.ShopData:getDailyRefreshAutoTime()
	self.txTime:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_22, remainTime))
	if DataManager.ShopData:getDailyAdRefreshRemainCount() <= 0 then
		self.txTimeAd:setText(remainTime)
	end
end

function DailyMainCell:refresh()
	for idx, cell in pairs(self.dailyCells) do
		cell:refresh(idx)
	end

	-- 广告
	if DataManager.ShopData:getDailyAdRefreshRemainCount() > 0 then
		self.btnRefreshAd:setTouchEnable(true)
		GFunc.setGrey(self.btnRefreshAd:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE), false)
		GFunc.setAdsSprite(self.imgAd, false)
		self.txTimeAd:setText("(" .. DataManager.ShopData:getDailyAdRefreshRemainCount() .. "/" .. DataManager.ShopData:getDailyRefreshAdCount() .. ")")
		self.btnRefreshAd:addRedPoint(80, 25, 0.8)
	else
		self.btnRefreshAd:setTouchEnable(false)
		GFunc.setGrey(self.btnRefreshAd:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE), true)
		GFunc.setAdsSprite(self.imgAd, true)
		self.btnRefreshAd:removeRedPoint()
	end
	self.txAd:setText(I18N:getGlobalText(I18N.GlobalConst.STR_REFRESH))
	GFunc.centerImgAndTx(self.imgAd, self.txAd, 3)

	-- 免费/钻石
	if DataManager.ShopData:getDailyFreeRefreshRemainCount() > 0 then
		-- 可以免费刷新
		self.btnRefresh:setTouchEnable(true)
		GFunc.setGrey(self.btnRefresh:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE), false)
		self.imgCost:setActive(false)
		self.txCost:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_PVP_DESC_10))
		self.txCost:setAnchoredPositionX(0)
		self.txTimeCost:setText("(" .. DataManager.ShopData:getDailyFreeRefreshRemainCount() .. "/" .. DataManager.ShopData:getDailyRefreshFreeCount() .. ")")
		self.txFree:setText("")
		self.btnRefresh:addRedPoint(80, 25, 0.8)
	elseif DataManager.ShopData:getDailyCostRefreshRemainCount() > 0 then
		-- 可以道具刷新
		self.btnRefresh:setTouchEnable(true)
		GFunc.setGrey(self.btnRefresh:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE), false)
		local cost = DataManager.ShopData:getDailyRefreshCost()
		self.imgCost:setActive(true)
		self.imgCost:setSprite(GFunc.getIconResByReward(cost))
		self.txCost:setText(GFunc.getRewardNum(cost))
		GFunc.centerImgAndTx(self.imgCost, self.txCost, 3)
		self.txTimeCost:setText("(" .. DataManager.ShopData:getDailyCostRefreshRemainCount() .. "/" .. DataManager.ShopData:getDailyRefreshCostCount() .. ")")
		self.txFree:setText("")
		self.btnRefresh:removeRedPoint()
	else
		-- 没刷新次数
		self.btnRefresh:setTouchEnable(false)
		GFunc.setGrey(self.btnRefresh:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE), true)
		self.imgCost:setActive(false)
		self.txCost:setText("")
		self.txTimeCost:setText("")
		self.txFree:setText(I18N:getGlobalText(I18N.GlobalConst.STR_REFRESH))
		self.btnRefresh:removeRedPoint()
	end
end

function DailyMainCell:onRefreshDaily(isAd)
	ModuleManager.ShopManager:reqRefreshDailyGift(isAd)
end

return DailyMainCell