local GiftComp = class("GiftComp", LuaComponent)

local GIFT_CELL = "app/ui/activity/act_seven_day/cell/gift_cell"

function GiftComp:init()
	local uiMap = self:getUIMap()
	uiMap["gift_comp.title_tx_1"]:setText(I18N:getGlobalText(I18N.GlobalConst.SEVEN_DAY_DESC_3))
	uiMap["gift_comp.title_tx_2"]:setText(I18N:getGlobalText(I18N.GlobalConst.SEVEN_DAY_DESC_4))
	self.scrollRect = uiMap["gift_comp.scroll_rect"]:getLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
	self.scrollRect:addInitCallback(function()
		return GIFT_CELL
	end)
	self.scrollRect:addRefreshCallback(function(index, cell)
		if self.giftList then
			cell:refresh(self.giftList[index], self.period)
		end
	end)
	self.scrollRect:clearCells()
end

function GiftComp:refresh(parentUI, day, period)
	self.day = day
	self.period = period
	self.giftList = DataManager.ActSevenDayData:getGiftList(self.day, period) or GConst.EMPTY_TABLE
	table.sort(self.giftList, function(a, b)
		local giftRemainNumA = DataManager.ActSevenDayData:getGiftRemainNum(a.id, period)
		local giftRemainNumB = DataManager.ActSevenDayData:getGiftRemainNum(b.id, period)
		if (giftRemainNumA > 0 and giftRemainNumB > 0) or (giftRemainNumA <= 0 and giftRemainNumB <= 0) then
			if a.isFree == b.isFree then
				if a.isAd == b.isAd then
					return a.id < b.id
				else
					if a.isAd then
						return true
					else
						return false
					end
				end
			else
				if a.isFree then
					return true
				else
					return false
				end
			end
		else
			return giftRemainNumA > 0
		end
	end)
	self.scrollRect:refillCells(#self.giftList)
end

return GiftComp