function createDeck(identifier, defaultRenderableProperties)
  return {
    SlideIdentifiers = {},
    IdentifierPrefix = identifier,
    CurrentSlideIndex = 1,
    DefaultRenderableProperties = defaultRenderableProperties,
    Visible = true
  }
end

function removeDeck(deck)
  for i, identifier in pairs(deck.SlideIdentifiers) do
    openspace.removeScreenSpaceRenderable(identifier)
  end
end

function addSlide(deck, path)
  local index = #deck.SlideIdentifiers + 1
  local identifier = deck.IdentifierPrefix .. index

  local spec = {
    Type = "ScreenSpaceImageLocal",
    Identifier = identifier,
    Name = identifier,
    TexturePath = path
  }

  for key, value in pairs(deck.DefaultRenderableProperties) do
    spec[key] = value
  end

  openspace.addScreenSpaceRenderable(spec)

  deck.SlideIdentifiers[#deck.SlideIdentifiers + 1] = identifier
end

function setCurrentSlide(deck, index, interpolationDuration)
  if (interpolationDuration == nil) then
    interpolationDuration = 0
  end

  if (index < 0) then
    index = 0
  end

  if (index > #deck.SlideIdentifiers + 1) then
    index = #deck.SlideIdentifiers + 1
  end

  deck.CurrentSlideIndex = index

  if not deck.Visible then
    return
  end

  for i, identifier in pairs(deck.SlideIdentifiers) do
    local opacity = 0
    if (index == i) then
      opacity = 1
    end
    if interpolationDuration == 0 then
      openspace.setPropertyValueSingle("ScreenSpace." .. identifier .. ".Opacity", opacity)
    else
      openspace.setPropertyValueSingle("ScreenSpace." .. identifier .. ".Opacity", opacity, interpolationDuration, "QuadraticEaseOut")
    end
  end
end

function goToNextSlide(deck, interpolationDuration)
  setCurrentSlide(deck, deck.CurrentSlideIndex + 1, interpolationDuration)
end

function goToPreviousSlide(deck, interpolationDuration)
  setCurrentSlide(deck, deck.CurrentSlideIndex - 1, interpolationDuration)
end

function toggleSlides(deck, interpolationDuration)
  deck.Visible = not deck.Visible
  if deck.Visible then
    for i, identifier in pairs(deck.SlideIdentifiers) do
      local opacity = 0
      if (i == deck.CurrentSlideIndex) then
        opacity = 1
      end
      openspace.setPropertyValueSingle(
        "ScreenSpace." .. identifier .. ".Opacity", opacity,
        interpolationDuration, "QuadraticEaseOut")
    end
  else
    for i, identifier in pairs(deck.SlideIdentifiers) do
      openspace.setPropertyValueSingle(
        "ScreenSpace." .. identifier .. ".Opacity", 0,
        interpolationDuration, "QuadraticEaseOut")
    end
  end
end

asset.export("createDeck", createDeck)
asset.export("removeDeck", removeDeck)
asset.export("addSlide", addSlide)
asset.export("setCurrentSlide", setCurrentSlide)
asset.export("goToNextSlide", goToNextSlide)
asset.export("goToPreviousSlide", goToPreviousSlide)
asset.export("toggleSlides", toggleSlides)
