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

function ScrollRectCenter:init()
    self.centerIndex = 0
    self.cellList = {}
    self.scrollRectCenter = self.baseObject:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SCROLL_RECT_CENTER)

    -- c#层实例化后调用
    self.scrollRectCenter:AddOnInstantiateCellAction(function(gameObject)
        self:_instantiateCell(gameObject)
    end)

    self.scrollRectCenter:AddRefreshAction(function(index, objIndex)
        self:_refreshFunc(index + 1, objIndex)
    end)

    self.scrollRectCenter:AddSetSelectedAction(function(isSelected, index)
        self:_setSelectedFunc(isSelected, index)
    end)

    self.scrollRectCenter:AddOnCenterAction(function(index)
        self:_onCenterFunc(index + 1)
    end)

    self.scrollRectCenter:AddOnLeaveCenterAction(function(index)
        self:_onLeaveCenterFunc(index + 1)
    end)
end

function ScrollRectCenter:_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 ScrollRectCenter:_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 ScrollRectCenter:_setSelectedFunc(isSelected, cellIndex)
    local cell = self.cellList[cellIndex]
    if self.selectedCallback and cell then
        self.selectedCallback(isSelected, cell)
    end
end

---- c#调用 cell处于中心事件
function ScrollRectCenter:_onCenterFunc(index)
    local cell = self.cellList[index]
    if self.onCenterCallback and cell then
        self.onCenterCallback(index, cell)
    end
    self.centerIndex = index
end

---- c#调用 cell处于离开中心事件
function ScrollRectCenter:_onLeaveCenterFunc(index)
    local cell = self.cellList[index]
    if self.onLeaveCenterCallback and cell then
        self.onLeaveCenterCallback(index, cell)
    end
end

---- *********************************************************************

---- 添加init回调 (function(index) return cell end)
function ScrollRectCenter:addInitCallback(callback)
    self.initCallback = callback
end

function ScrollRectCenter: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 ScrollRectCenter:addRefreshCallback(callback)
    self.refreshCallback = callback
end

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

function ScrollRectCenter:addOnCenterCallback(callback)
    self.onCenterCallback = callback
end

function ScrollRectCenter:addOnLeaveCenterCallback(callback)
    self.onLeaveCenterCallback = callback
end

function ScrollRectCenter:addOnBeginDragCallback(callback)
    self.scrollRectCenter:AddOnBeginDragAction(callback)
end

function ScrollRectCenter:addOnEndDragCallback(callback)
    self.scrollRectCenter:AddOnEndDragAction(callback)
end

---- *********************************************************************

function ScrollRectCenter:setTotalCount(count)
    self.totalCount = count
    self.scrollRectCenter:SetTotalCount(count)
end

function ScrollRectCenter:getTotalCount()
    return self.totalCount or 0
end

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

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

---- 设置缩放的变化比率 该值越小缩放越大
function ScrollRectCenter:setScaleRatio(ratio)
    self.scrollRectCenter:SetScaleRatio(ratio)
end

---- 重新装填scrollRect (center 当前居中的index)
function ScrollRectCenter:refillCells(center)
    local center = center or 1
    self.scrollRectCenter:RefillCells(center - 1)
end

---- 刷新全部cell
function ScrollRectCenter:refreshAll()
    self.scrollRectCenter:RefreshAll()
end

---- 朝左移动一个位置
function ScrollRectCenter:moveLeft()
    self.scrollRectCenter:MoveLeft()
end

---- 朝右移动一个位置
function ScrollRectCenter:moveRight()
    self.scrollRectCenter:MoveRight()
end

---- 移动到指定位置
function ScrollRectCenter:moveTo(index, immediately)
    if immediately == nil then
        immediately = false
    end
    self.scrollRectCenter:MoveTo(index - 1, immediately)
end

function ScrollRectCenter:getCenterIndex()
    return self.centerIndex
end

function ScrollRectCenter:getCell(index)
    return self.cellList[index]
end

function ScrollRectCenter:getCellCount()
    return #self.cellList
end

function ScrollRectCenter:ResetCenterIndex()
    self.scrollRectCenter:ResetCenterIndex()
end

return ScrollRectCenter
