local ChapterSellCell = class("ChapterSellCell", BaseCell)
local CHAPTER_CELL = "app/ui/shop/cell/chapter_cell"
local BASE_CELL_HEIGHT = 320

function ChapterSellCell:init()
	local uiMap = self.baseObject:genAllChildren()
	self.scrollRectObj = uiMap["chapter_sell_cell.scroll_rect"]
    self.contentObj = uiMap["chapter_sell_cell.scroll_rect.viewport.content"]
    self.scrollRect = self.scrollRectObj:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRect:addInitCallback(function()
        return CHAPTER_CELL
    end)
    self.scrollRect:addRefreshCallback(function(idx, cell)
        if self.actChapterCfg and self.actChapterCfg[idx] then
            cell:refresh(idx, self.actChapterCfg[idx], #self.actChapterCfg, 
            function (add)
                self.contentObj:addAnchoredPosition(add*720)
            end)
        end
    end)
    self.scrollRect:clearCells()

	self.cellHeight = BASE_CELL_HEIGHT
end

function ChapterSellCell:refresh()
    local actIdList = DataManager.ShopData:getActChapterStoreCanBuyActIds()
    self.actChapterCfg = {}
    local actChapterCfg = DataManager.ShopData:getActChapterStoreConfig()
    for i = 1, #actIdList do
        local cfgInfo = clone(actChapterCfg[actIdList[i]]) 
        cfgInfo.id = actIdList[i]
        table.insert(self.actChapterCfg, cfgInfo)
    end
    local count = #self.actChapterCfg
    self.scrollRect:refillCells(count)
    self.contentObj:setAnchoredPositionX(0)
    self.scrollRect:moveToIndex(count)
end

function ChapterSellCell:getCellHeight()
	return BASE_CELL_HEIGHT -- 章节礼包横向展开
end

function ChapterSellCell:getIsOpen()
    -- 有可购买的礼包 才打开
    return #DataManager.ShopData:getActChapterStoreCanBuyActIds() > 0
end

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

return ChapterSellCell