local LevelSellCell = class("LevelSellCell", BaseCell)
local LEVEL_CELL = "app/ui/shop/cell/level_cell"
local BASE_CELL_HEIGHT = 320

function LevelSellCell:init()
	local uiMap = self.baseObject:genAllChildren()
	self.scrollRectObj = uiMap["level_sell_cell.scroll_rect"]
    self.contentObj = uiMap["level_sell_cell.scroll_rect.viewport.content"]
    self.scrollRect = self.scrollRectObj:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRect:addInitCallback(function()
        return LEVEL_CELL
    end)
    self.scrollRect:addRefreshCallback(function(idx, cell)
        if self.actGiftCfg and self.actGiftCfg[idx] then
            cell:refresh(idx, self.actGiftCfg[idx])
        end
    end)
    self.scrollRect:clearCells()

	self.cellHeight = BASE_CELL_HEIGHT
end

function LevelSellCell:refresh()
    local actIds = DataManager.ShopData:getLevelUpGiftActIds()
    self.actGiftCfg = {}
    local actGiftCfg = ConfigManager:getConfig("act_gift")
    for i = 1, #actIds do
        local cfgInfo = clone(actGiftCfg[actIds[i]]) 
        cfgInfo.id = actIds[i]
        table.insert(self.actGiftCfg, cfgInfo)
    end
    self.scrollRect:refillCells(#self.actGiftCfg)

    self:getBaseObject():setSizeDeltaY(self:getCellHeight())
    self.scrollRectObj:setSizeDeltaY(self:getCellHeight())
end

function LevelSellCell:getCellHeight()
    local cellCount = self.actGiftCfg and #self.actGiftCfg or 0
	return BASE_CELL_HEIGHT * cellCount
end

function LevelSellCell:getIsOpen()
    return #DataManager.ShopData:getLevelUpGiftActIds() > 0
end

function LevelSellCell:setVisible(visible)
	self.baseObject:setVisible(visible)
end

return LevelSellCell