local ChapterCell = class("ChapterCell", BaseCell)

function ChapterCell:init()
	local uiMap = self:getUIMap()
	self.txName = uiMap["chapter_cell.tx_name"]
	self.rewardNode = uiMap["chapter_cell.reward_node"]
	self.rewardNodeLayout = self.rewardNode:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT)
	self.rewardCells = {}
	for i = 1, 3 do
		table.insert(self.rewardCells, uiMap["chapter_cell.reward_node.reward_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL))
	end
	self.costNode = uiMap["chapter_cell.cost"]
	self.imgCost = uiMap["chapter_cell.cost.img_cost"]
	self.txCost = uiMap["chapter_cell.cost.tx_cost"]
	self.tag = uiMap["chapter_cell.tag"]
	self.txOff = uiMap["chapter_cell.tag.tx_off"]
	self.btnLeft = uiMap["chapter_cell.btn_left"]
	self.btnRight = uiMap["chapter_cell.btn_right"]	

	self.baseObject:addClickListener(function()
		-- local cost = DataManager.ShopData:getChapterGiftConfig(self.id).cost
		-- if not GFunc.checkCost(GFunc.getRewardId(cost), GFunc.getRewardNum(cost), true, BIReport.ITEM_GET_TYPE.SHOP_CHAPTER) then
		-- 	return
		-- end
		-- ModuleManager.ShopManager:reqBuyChapterGift(self.id)
		PayManager:purchasePackage(self.info.id, PayManager.PURCHARSE_TYPE.ACT_GIFT)
	end)
	self.btnLeft:addClickListener(function()
		if self.callback then
			self.callback(1)
		end
	end)
	self.btnRight:addClickListener(function()
		if self.callback then
			self.callback(-1)
		end
	end)
end

function ChapterCell:refresh(info, curIdx, maxIdx, callback)
	self.info = info
	self.callback = callback
	local cfg = DataManager.GiftPopData:getGiftConfig(self.info.id)
	-- self.baseObject:setSprite(GConst.ATLAS_PATH.UI_SHOP, cfg.atr_resource)
	-- self.baseObject:setSprite(GConst.ATLAS_PATH.UI_SHOP, "shop_gift_bg_3")
	self.txName:setText(I18N:getText("act_gift", self.info.id, "value")) -- I18N:getGlobalText(I18N.GlobalConst.MALL_CHAPTER_DESC_1, cfg.parameter))
	self.txCost:setText(GFunc.getFormatPrice(cfg.recharge_id))
	-- GFunc.centerImgAndTx(self.imgCost, self.txCost, 3)
	self.txOff:setText(cfg.value .. "%")
	local rewardCount = 0
	for i, cell in pairs(self.rewardCells) do
		if cfg.reward and cfg.reward[i] then
			rewardCount = rewardCount + 1
			cell:setVisible(true)
			cell:refreshByConfig(cfg.reward[i])
		else
			cell:setVisible(false)
		end
	end
	self.rewardNode:setSizeDeltaX(rewardCount * 102 + 12)
    self.rewardNodeLayout:RefreshLayout()
	self.btnLeft:setActive(maxIdx > 1 and curIdx > 1)
	self.btnRight:setActive(maxIdx > 1 and curIdx < maxIdx)
end

return ChapterCell