local ActGiftCellV1 = class("ActGiftCellV1", BaseCell)
local REWARD_CELL_COUNT = 3

local BG_NAME_1 = "act_common_bg_27" -- 售罄
local BG_NAME_2 = "act_common_bg_28" -- 正常

local BTN_NAME_1 = "common_btn_grey_1"
local BTN_NAME_2 = "common_btn_yellow_1"

function ActGiftCellV1:refresh(actId, actGiftId)
	local uiMap = self:getUIMap()
	local bg = uiMap["gift_cell.bg"]
	local descTx = uiMap["gift_cell.bg.desc_tx"]
	if not self.rewardList then
		self.rewardList = {}
		for i = 1, REWARD_CELL_COUNT do
			table.insert(self.rewardList, CellManager:addCellComp(uiMap["gift_cell.bg.reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.REWARD_CELL))
		end
	end
	local offBg = uiMap["gift_cell.bg.off_bg"]
	local offTx = uiMap["gift_cell.bg.off_bg.desc_tx"]
	local limitTx = uiMap["gift_cell.bg.limit_tx"]
	local funcBtn = uiMap["gift_cell.bg.func_btn"]
	local funcTx = uiMap["gift_cell.bg.func_btn.desc_tx"]
	local funcAdIcon = uiMap["gift_cell.bg.func_btn.ad"]
	local funcAdTx = uiMap["gift_cell.bg.func_btn.ad_tx"]
	local func2AdTx = uiMap["gift_cell.bg.func_btn.ad_2_tx"]

	self.cfg = DataManager.ActGiftData:getGiftConfig(actId, actGiftId)
	local isFree = DataManager.ActGiftData:isFreeGift(actId, actGiftId)
	local isAd = DataManager.ActGiftData:isAdGift(actId, actGiftId)
	local buyCount = DataManager.ActGiftData:getGiftBuyCount(actGiftId)
	local costItem = self.cfg.item_cost and self.cfg.item_cost[1]
	local rechargeId = self.cfg.recharge_id
	local limitCount = self.cfg.limit
	local value = self.cfg.value
	local rewards = self.cfg.reward
	local soldOut = buyCount >= limitCount

	bg:setSprite(GConst.ATLAS_PATH.UI_ACT_COMMON, (isFree or isAd) and BG_NAME_1 or BG_NAME_2)
	funcBtn:setSprite(GConst.ATLAS_PATH.COMMON, soldOut and BTN_NAME_1 or BTN_NAME_2)
	if isFree or isAd then
		local limitStr = I18N:getGlobalText(I18N.GlobalConst.GIFT_ROUTINE_DESC_8) .. "(" .. (limitCount - buyCount) .. "/" .. limitCount .. ")"
		-- limitTx:setText(soldOut and "<color=#2C6A65>" .. limitStr .. "</color>" or "<color=#6A482D>" .. limitStr .. "</color>")
		limitTx:setText("<color=#2C6A65>" .. limitStr .. "</color>")
	else
		local limitStr = I18N:getGlobalText(I18N.GlobalConst.GIFT_ROUTINE_DESC_8) .. "(" .. (limitCount - buyCount) .. "/" .. limitCount .. ")"
		-- limitTx:setText(soldOut and "<color=#2C6A65>" .. limitStr .. "</color>" or "<color=#6A482D>" .. limitStr .. "</color>")
		limitTx:setText("<color=#6A482D>" .. limitStr .. "</color>")
	end
	if soldOut then
		funcTx:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_GOGOGO_DESC_8))
		funcAdIcon:setVisible(false)
		funcAdTx:setVisible(false)
		func2AdTx:setVisible(false)
	else
		if rechargeId then
			funcTx:setText(GFunc.getFormatPrice(rechargeId))
			funcAdIcon:setVisible(false)
			funcAdTx:setVisible(false)
			func2AdTx:setVisible(false)
		else
			if isFree then
				funcTx:setText(I18N:getGlobalText(I18N.GlobalConst.FREE_DESC))
				funcAdIcon:setVisible(false)
				funcAdTx:setVisible(false)
				func2AdTx:setVisible(false)
			elseif isAd then
				funcTx:setText()
				funcAdIcon:setVisible(true)
				funcAdTx:setVisible(true)
				func2AdTx:setVisible(false)
				GFunc.setAdsSprite(funcAdIcon)
				funcAdTx:setText(I18N:getGlobalText(I18N.GlobalConst.FREE_DESC))
			elseif costItem then
				funcTx:setText()
				funcAdIcon:setVisible(true)
				funcAdIcon:setSprite(GFunc.getIconRes(costItem.id))
				if GFunc.checkCost(costItem.id, costItem.num, false) then
					funcAdTx:setVisible(true)
					func2AdTx:setVisible(false)
					funcAdTx:setText(costItem.num)
				else
					funcAdTx:setVisible(true)
					func2AdTx:setVisible(false)
					funcAdTx:setText("<color=#FF6C73>" .. costItem.num .. "</color>")
				end
			end
		end
	end
	descTx:setText(DataManager.ActGiftData:getActGiftI18NName(actGiftId))
	for i = 1, REWARD_CELL_COUNT do
		local reward = rewards and rewards[i]
		if reward then
			self.rewardList[i].baseObject:setActive(true)
			self.rewardList[i]:refreshByConfig(reward)
		else
			self.rewardList[i].baseObject:setActive(false)
		end
	end
	if value then
		offBg:setVisible(true)
		offTx:setText(value .. "%")
	else
		offBg:setVisible(false)
	end

	funcBtn:addClickListener(function()
		if not soldOut then
			if isAd then
				SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.AD_ACT_SPRINT, function()
					ModuleManager.ActSprintManager:reqGift(actGiftId)
				end)
			else
				if costItem then
					if GFunc.checkCost(costItem.id, costItem.num, true) then
						ModuleManager.ActSprintManager:reqGift(actGiftId)
					end
				else
					ModuleManager.ActSprintManager:reqGift(actGiftId)
				end
			end
		end
	end)
end

return ActGiftCellV1