local BulletViewUtil = {}
local BulletViewLuaArray, BulletViewLuaArrayAccess, CreateStraightLuaArray, CreateStraightLuaArrayAccess, BulletViewFacade
local loadedCount = 0
local viewNameMap = {}
local viewNameId = 0
local animationCurveMap = {}
local animationCurveId = 0
local INVALID_HANDLE = -1
local lastUpdateStraightFrame = 0

function BulletViewUtil.InitView()
  if BulletViewLuaArray == nil then
    BulletViewLuaArray = LuaCSharpArray.New(1024)
    BulletViewLuaArrayAccess = BulletViewLuaArray:GetCSharpAccess()
    CreateStraightLuaArray = LuaCSharpArray.New(32)
    CreateStraightLuaArrayAccess = CreateStraightLuaArray:GetCSharpAccess()
    BulletViewFacade = CS.PVEBattleLogic.Bullet.BulletViewFacade
    BulletViewFacade.InitBulletViewArrayAccess(BulletViewLuaArrayAccess, CreateStraightLuaArrayAccess)
  end
end

function BulletViewUtil.ClearData()
  if BulletViewFacade ~= nil then
    BulletViewFacade.ClearAll()
  end
end

function BulletViewUtil.UnInitView()
  if BulletViewLuaArray ~= nil then
    BulletViewLuaArray:DestroyCSharpAccess()
    BulletViewLuaArray = nil
    BulletViewLuaArrayAccess = nil
    CreateStraightLuaArray:DestroyCSharpAccess()
    CreateStraightLuaArray = nil
    CreateStraightLuaArrayAccess = nil
    BulletViewFacade.UnInitBulletViewArrayAccess()
    BulletViewFacade.ClearAll()
    BulletViewFacade = nil
  end
end

function BulletViewUtil.CheckLoadedCount()
  BulletViewFacade.CheckLoaded()
  loadedCount = BulletViewLuaArray[1]
  return loadedCount
end

function BulletViewUtil.GetLoadedObjId(index)
  if index <= loadedCount then
    local idIndex = index * 2
    local cdIndex = index * 2 + 1
    return BulletViewLuaArray[idIndex], BulletViewLuaArray[cdIndex]
  end
  return 0
end

function BulletViewUtil.UpdateStraightCount(deltaTime)
  local curFrame = Time.frameCount
  if lastUpdateStraightFrame == curFrame then
    deltaTime = 0
  else
    lastUpdateStraightFrame = curFrame
  end
  local res = BulletViewFacade.UpdateStraightLuaArray(deltaTime)
  if not res then
    BulletViewLuaArray[1] = 0
  end
  loadedCount = BulletViewLuaArray[1]
  return loadedCount
end

function BulletViewUtil.GetStraight(index)
  if index <= loadedCount then
    index = index * 2
    local objId = BulletViewLuaArray[index]
    local lifeEnd = BulletViewLuaArray[index + 1]
    return objId, lifeEnd
  end
end

function BulletViewUtil.ResizeBulletViewLuaArray(size)
  if BulletViewLuaArray ~= nil then
    BulletViewLuaArray[size] = 0
    return true
  end
  return false
end

function BulletViewUtil.GetBulletViewNameId(viewName)
  local id = viewNameMap[viewName]
  if id ~= nil then
    return id
  end
  viewNameId = viewNameId + 1
  id = viewNameId
  viewNameMap[viewName] = id
  BulletViewFacade.SyncNameId(viewName, id)
  return id
end

function BulletViewUtil.GetAnimationCurveId(animationCurve)
  local id = animationCurveMap[animationCurve]
  if id ~= nil then
    return id
  end
  animationCurveId = animationCurveId + 1
  id = animationCurveId
  animationCurveMap[animationCurve] = id
  BulletViewFacade.SyncAnimationCurve(animationCurve, id)
  return id
end

function BulletViewUtil.CreateStraightView(viewName, objId, startPosX, startPosY, startPosZ, rotY, targetLayerMask, inertiaVelocityX, inertiaVelocityY, inertiaVelocityZ, lifeTime, bulletScale, colliderRadius, noCollision, duration, spiral_loops, spiral_radius, speed, curve, colliderType, defaultDotCD)
  local viewId = BulletViewUtil.GetBulletViewNameId(viewName)
  local noCollisionValue = noCollision and 1 or 0
  local curveId = BulletViewUtil.GetAnimationCurveId(curve)
  local colliderTypeValue = colliderType and 1 or 0
  CreateStraightLuaArray[1] = viewId
  CreateStraightLuaArray[2] = objId
  CreateStraightLuaArray[3] = startPosX
  CreateStraightLuaArray[4] = startPosY
  CreateStraightLuaArray[5] = startPosZ
  CreateStraightLuaArray[6] = rotY
  CreateStraightLuaArray[7] = targetLayerMask
  CreateStraightLuaArray[8] = inertiaVelocityX
  CreateStraightLuaArray[9] = inertiaVelocityY
  CreateStraightLuaArray[10] = inertiaVelocityZ
  CreateStraightLuaArray[11] = lifeTime
  CreateStraightLuaArray[12] = bulletScale
  CreateStraightLuaArray[13] = colliderRadius
  CreateStraightLuaArray[14] = noCollisionValue
  CreateStraightLuaArray[15] = duration
  CreateStraightLuaArray[16] = spiral_loops
  CreateStraightLuaArray[17] = spiral_radius
  CreateStraightLuaArray[18] = speed
  CreateStraightLuaArray[19] = curveId
  CreateStraightLuaArray[20] = colliderTypeValue
  CreateStraightLuaArray[21] = defaultDotCD
  return BulletViewFacade.CreateBulletViewStraightLuaArray()
end

function BulletViewUtil.SyncStraightGatlingViewData(data)
  local viewId = BulletViewUtil.GetBulletViewNameId(data.bulletEffect)
  local curveId = BulletViewUtil.GetAnimationCurveId(data.curve)
  local colliderTypeValue = 1
  local noCollisionValue = data.noCollision and 1 or 0
  CreateStraightLuaArray[1] = viewId
  CreateStraightLuaArray[2] = data.targetLayerMask
  CreateStraightLuaArray[3] = data.inertiaVelocityX
  CreateStraightLuaArray[4] = data.inertiaVelocityY
  CreateStraightLuaArray[5] = data.inertiaVelocityZ
  CreateStraightLuaArray[6] = data.lifeTime
  CreateStraightLuaArray[7] = data.bulletScale
  CreateStraightLuaArray[8] = data.colliderRadius
  CreateStraightLuaArray[9] = noCollisionValue
  CreateStraightLuaArray[10] = data.duration
  CreateStraightLuaArray[11] = data.speed
  CreateStraightLuaArray[12] = curveId
  CreateStraightLuaArray[13] = colliderTypeValue
  BulletViewFacade.SyncStraightGatlingViewDataLuaArray()
end

function BulletViewUtil.CreateStraightGatlingView(objId, ownerHandle)
  CreateStraightLuaArray[1] = objId
  CreateStraightLuaArray[2] = ownerHandle
  local viewHandle = BulletViewFacade.CreateStraightGatlingViewLuaArray()
  if viewHandle > INVALID_HANDLE then
    return viewHandle, CreateStraightLuaArray[1], CreateStraightLuaArray[2], CreateStraightLuaArray[3], CreateStraightLuaArray[4]
  end
  return viewHandle, 0, 0, 0, 0
end

return ConstClass("BulletViewUtil", BulletViewUtil)
