local UIPrefabObject = require "app/bf/unity/uiprefab_object"
local ScrollRectOld = class("ScrollRect", LuaComponent)

function ScrollRectOld:init()
    self.cellList = {} --所有cell

    self.scrollRectOld = self.baseObject:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SCROLL_RECT_BASE_OLD)
	self.scrollRectOld:AddOnInstantiateCellAction(function(gameObject)
        self:_instantiateCell(gameObject)
    end)
    self.scrollRectOld:AddRefreshAction(function(index, cellIndex) self:_refreshFunc(index + 1, cellIndex) end)
    self.scrollRectOld:AddSetSelectedAction(function(isSelected, index) self:_setSelectedFunc(isSelected, index) end)
end

function ScrollRectOld:_instantiateCell(gameObject)
    if self:isDestroyed() then
        ResourceManager:destroyPrefab(gameObject)
        return
    end
    local prefabObject = UIPrefabObject:create()
    prefabObject:initWithGameObject(gameObject)
    prefabObject:initPrefabHelper()
    prefabObject:genAllChildren()
    self.baseObject:addChildList(prefabObject)

    local cell = prefabObject:addLuaComponent(self.initCallback())
    if self.initFinishCallback then
        self.initFinishCallback(cell)
    end
    table.insert(self.cellList, cell)
end

---- c#调用 cell刷新事件
function ScrollRectOld:_refreshFunc(index, cellIndex)
    local cell = self.cellList[cellIndex]
    if self.refreshCallback and cell then
        cell:setIndex(index)
        self.refreshCallback(index, cell)
    end
end

---- c#调用 cell选中事件
function ScrollRectOld:_setSelectedFunc(isSelected, cellIndex)
    local cell = self.cellList[cellIndex]
    if self.selectedCallback and cell then
        self.selectedCallback(isSelected, cell)
    end
end

---- 添加init回调 (function(index) return path, cellComp end)
function ScrollRectOld:addInitCallback(callback)
    self.initCallback = callback
end

---- 添加loadCell完成回调
function ScrollRectOld:addInitFinishCallback(callback)
    if self.cellList then
        for k, v in ipairs(self.cellList) do
            callback(v)
        end
    end
    self.initFinishCallback = callback
end

---- 添加refresh回调 (function(index, cell) end)
function ScrollRectOld:addRefreshCallback(callback)
    self.refreshCallback = callback
end

---- 添加选中回调 (function(isSelected, cell) end)
function ScrollRectOld:addSelectedCallback(callback)
    self.selectedCallback = callback
end

---- 设置scroll rect 总数量
function ScrollRectOld:setTotalCount(count)
    self.scrollRectOld:SetTotalCount(count)
end

function ScrollRectOld:getTotalCount()
    return self.scrollRectOld:GetTotalCount()
end

---- 添加一个cell
function ScrollRectOld:addCell()
    self.scrollRectOld:AddCell()
end

---- 刷新所有 调用当前显示cells的refresh方法
function ScrollRectOld:refreshAll()
    self.scrollRectOld:RefreshAll()
end

------- 立即 重新装填scrollRect (index为置顶cell的index即数据在数组中的index)
function ScrollRectOld:refillCells(index)
    local index = index or 0
    self.scrollRectOld:RefillCells(index)
end
---- 立即 倒序重新装填scrollRect (offset = totalcount - 想要置底的cellindex) 只支持ContentConstraintCount == 1
function ScrollRectOld:refillCellsFromEnd(index)
    local index = index or 0
    self.scrollRectOld:RefillCellsFromEnd(index)
end

---- 分帧 重新装填scrollRect (index为置顶cell的index即数据在数组中的index)
function ScrollRectOld:refillCellsForFrame(index, intervalFrame)
    local index = index or 0
    local intervalFrame = intervalFrame or 3
    self.scrollRectOld:RefillCellsForFrame(intervalFrame, index)
end

---- 分帧 倒序重新装填scrollRect (offset = totalcount - 想要置底的cellindex) 只支持ContentConstraintCount == 1
function ScrollRectOld:refillCellsFromEndFrame(offset, intervalFrame)
    local offset = offset or 0
    local intervalFrame = intervalFrame or 3
    self.scrollRectOld:RefiilCellsFromEndForFrame(intervalFrame, offset)
end

---- 获取当前选中的index
function ScrollRectOld:getSelectedIndex()
    return self.scrollRectOld:GetSelectedIndex() + 1
end

---- 设置当前选中的index
function ScrollRectOld:setSelected(selectIndex)
    self.scrollRectOld:SetSelected(selectIndex - 1)
end

---- remove 索引为index cell
function ScrollRectOld:removeCell(index, alignFlag)
    if alignFlag == nil then
        alignFlag = true
    end
    self.scrollRectOld:RemoveCell(index - 1, alignFlag)
end

---- 插入一个cell
function ScrollRectOld:insertCell(index, alignFlag)
    if alignFlag == nil then
        alignFlag = true
    end
    self.scrollRectOld:InsertCell(index - 1, alignFlag)
end

---- 滑动到索引为index的cell
function ScrollRectOld:scrollToCell(index, speed, alignFlag)
    if alignFlag == nil then
        alignFlag = true
    end
    self.scrollRectOld:ScrollToCell(index - 1, speed, alignFlag)
end

---- 滑动到索引为index的cell 立即 可以在一开始直接调用也会refill
function ScrollRectOld:scrollToCellImmediately(index, alignFlag)
    if alignFlag == nil then
        alignFlag = true
    end
    self.scrollRectOld:ScrollToCellImmediately(index - 1, alignFlag)
end

---- 清除scroll rect
function ScrollRectOld:clearCells()
    self.scrollRectOld:ClearCells()
end

function ScrollRectOld:getScrollRect()
    return self.scrollRectOld
end

function ScrollRectOld:isFullContent()
    local scrollHeight = self.baseObject:getComponent(GConst.TYPEOF_UNITY_CLASS.RECTTRANSFORM).rect.height
    local contentHeight = self.scrollRectOld.content.rect.height
    return scrollHeight < contentHeight
end

return ScrollRectOld