local BaseTips = require "app/ui/tips/base_tips"
local RewardBox = class("RewardBox", BaseTips)

local CELL_WIDTH = 130
local CELL_NUM = 5

function RewardBox:ctor(params)
	self.params = params or {}
	self.params.rewards = params.rewards or {}
	self.customTitleStr = params.customTitleStr
	self.callback = params.callback
end

function RewardBox:getPrefabPath()
	return "assets/prefabs/ui/common/reward_box.prefab"
end

function RewardBox:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	self.scrollView = uiMap["reward_box.scroll_rect"]
	self.scrollRect = self.scrollView:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
	self.scrollRect:addInitCallback(function()
		return GConst.TYPEOF_LUA_CLASS.REWARD_CELL
	end)
	self.scrollRect:addRefreshCallback(function(index, cell)
		cell:refresh(self.params.rewards[index])
	end)
	self.scrollRect:clearCells()

	self.root:addClickListener(function()
		self:closeUI()
		if self.callback then
			self.callback()
		end
	end)

	self.titleTx = uiMap["reward_box.reward_title"]
	self.titleTx:setText(self.customTitleStr or I18N:getGlobalText(I18N.GlobalConst.REWARD_DESC))
	uiMap["reward_box.continue_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.CLICK_CLOSE_DESC))

	self:_refreshScrollRect()
	uiMap["reward_box.effect_node.ui_spine_obj"]:playAnim("idle", false, false)
end

function RewardBox:_refreshScrollRect()
	if #self.params.rewards <= 5 then
		self.scrollView:setSizeDeltaX(#self.params.rewards*CELL_WIDTH)
		self.scrollRect:setPerLineNum(#self.params.rewards)
		self.scrollRect:refillCells(#self.params.rewards, true)
	else
		self.scrollView:setSizeDeltaX(CELL_WIDTH*5)
		self.scrollRect:setPerLineNum(CELL_NUM)
		self.scrollRect:refillCells(#self.params.rewards, true)
	end
end

return RewardBox