openspace.printWarning("Warning asset_helper has been deprecated and will be removed in future verions");

local tableLength = function(table)
  local count = 0
  for _ in pairs(table) do count = count + 1 end
  return count
end


local registerSpiceKernels = function (spiceAsset, kernels)
  spiceAsset.onInitialize(function ()
    for i, kernel in ipairs(kernels) do
      openspace.spice.loadKernel(kernel)
    end
  end)
  spiceAsset.onDeinitialize(function ()
    for i = #kernels, 1, -1 do
      local kernel = kernels[i]
      openspace.spice.unloadKernel(kernel) 
    end
  end)
end

local registerSceneGraphNodes = function (sceneAsset, nodes, override)
  override = override or false
  if not override then
    if tableLength(nodes) == 0 then
      openspace.printWarning(sceneAsset.filePath .. ": Register function was called with an empty node list. Pass 'true' as third argument to silence this warning")
      return
    end
  end


  sceneAsset.onInitialize(function ()
    for i, node in ipairs(nodes) do
      openspace.addSceneGraphNode(node)
    end
  end)
  sceneAsset.onDeinitialize(function ()
    for i = #nodes, 1, -1 do
      node = nodes[i]
      openspace.removeSceneGraphNode(node.Identifier)
    end
  end)
end


local registerScreenSpaceRenderables = function (sceneAsset, renderables, override)
  override = override or false
  if not override then
    if tableLength(renderables) == 0 then
      openspace.printWarning(sceneAsset.filePath .. ": Register function was called with an empty node list. Pass 'true' as third argument to silence this warning")
      return
    end
  end

  sceneAsset.onInitialize(function ()
    for i, node in ipairs(renderables) do
      openspace.addScreenSpaceRenderable(node)
    end
  end)
  sceneAsset.onDeinitialize(function ()
    for i = #renderables, 1, -1 do
      renderable = renderables[i]
      openspace.removeScreenSpaceRenderable(renderable.Identifier)
    end
  end)
end

local registerDashboardItems = function (dashboardAsset, items)
  dashboardAsset.onInitialize(
    function ()
      for i, item in ipairs(items) do
        openspace.dashboard.addDashboardItem(item)
      end
    end
  )
  dashboardAsset.onDeinitialize(function ()
    for i = #items, 1, -1 do
      local item = items[i]
      openspace.dashboard.removeDashboardItem(item.Identifier)
    end
  end)
end

local registerSceneGraphNodesAndExport = function (sceneAsset, nodes, override)
  override = override or false
  if not override then
    if tableLength(nodes) == 0 then
      openspace.printWarning(sceneAsset.filePath .. ": Register function was called with an empty node list. Pass 'true' as third argument to silence this warning")
      return
    end
  end

  for i, node in ipairs(nodes) do
    if not node.Identifier then
      openspace.printError("Could not load asset as Identifier was missing")
    end
  end

  sceneAsset.onInitialize(function ()
    for i, node in ipairs(nodes) do
      openspace.addSceneGraphNode(node)
    end
  end)
  sceneAsset.onDeinitialize(function ()
    for i = #nodes, 1, -1 do
      local node = nodes[i]
      openspace.removeSceneGraphNode(node.Identifier)
    end
  end)

  for i, node in ipairs(nodes) do
    sceneAsset.export(node.Identifier, node)
  end
end

local requireAll = function (sceneAsset, directory)
  function string.ends(String,End)
    return End=='' or string.sub(String,-string.len(End))==End
  end

  local result = {}

  local files = openspace.walkDirectoryFiles(sceneAsset.localResource('') .. directory, true)
  for _, file in pairs(files) do
    if file:ends('.asset') then
      openspace.printDebug("Requiring: " .. file:sub(file:find(directory), -7))
      local exports = sceneAsset.require(file:sub(1, -7))
      table.insert(result, exports)
    end
  end

  return result
end

local getDefaultLightSources = function (solarSystemBarycenterIdentifier)
  local sourceList = {
    {
      Type = "SceneGraphLightSource",
      Identifier = "Sun",
      Node = solarSystemBarycenterIdentifier,
      Intensity = 1.0
    },
    {
      Identifier = "Camera",
      Type = "CameraLightSource",
      Intensity = 0.5
    }
  }
  return sourceList
end


local createModelPart = function (parent, sunLightSourceNode, models, geometry, texture, performShading)
  local lightSources = {}
  if performShading then
    lightSources[1] = {
        Type = "SceneGraphLightSource",
        Identifier = "Sun",
        Node = sunLightSourceNode,
        Intensity = 1.0
    }
  end
  return {
    Identifier = parent .. "-" .. geometry,
    Parent = parent,
    Renderable = {
      Type = "RenderableModel",
      GeometryFile =  models .. "/" .. geometry .. ".obj",
      LightSources = lightSources,
      PerformShading = performShading,
      DisableFaceCulling = true 
    },
    GUI = {
      Hidden = true
    }
  }
end

asset.export("registerSceneGraphNodes", registerSceneGraphNodes)
asset.export("registerSceneGraphNodesAndExport", registerSceneGraphNodesAndExport)
asset.export("registerScreenSpaceRenderables", registerScreenSpaceRenderables)
asset.export("registerSpiceKernels", registerSpiceKernels)
asset.export("registerDashboardItems", registerDashboardItems)
asset.export("requireAll", requireAll)
asset.export("getDefaultLightSources", getDefaultLightSources)
asset.export("createModelPart", createModelPart)
