-- Function that returns the string that inverts the fully qualified boolean property 'property'
local invert = function(prop)
  local escaped_property = '"' .. prop .. '"'
  return "openspace.setPropertyValueSingle(" .. escaped_property .. ", not openspace.getPropertyValue(" .. escaped_property .. "));"
end

-- Function that returns the string that increments the 'property' by the 'value'
local increment = function(prop, value)
  local v = value or 1
  local escaped_property = '"' .. prop .. '"'
  return "openspace.setPropertyValueSingle(" .. escaped_property .. ", openspace.getPropertyValue(" .. escaped_property .. ") + " .. v .. ");"
end

-- Function that returns the string that decrements the 'property' by the 'value'
local decrement = function(prop, value)
  return increment(prop, -value)
end

local fade = function(prop, value, duration)
  assert(type(prop) == "string", "prop must be a number")
  assert(type(duration) == "number", "duration must be a number")

  local escaped_property = '"' .. prop .. '"'
  return "openspace.setPropertyValueSingle(" .. escaped_property ..", " .. tostring(value) .. ", " .. tostring(duration) .. ")"
end

local fadeOut = function(prop, duration)
  return fade(prop, 0.0, duration)
end

local fadeIn = function(prop, duration)
  return fade(prop, 1.0, duration)
end

local fadeInOut = function(prop, duration)
  assert(type(prop) == "string", "prop must be a number")
  assert(type(duration) == "number", "duration must be a number")

  local escaped_property = '"' .. prop .. '"'
  -- If the value is > 0.5 fade out, otherwise fade in
  return "local v = openspace.getPropertyValue(" .. escaped_property .. "); if v <= 0.5 then " .. fadeIn(prop, duration) .. " else " .. fadeOut(prop, duration) .. " end"
end

asset.export("invert", invert)
asset.export("increment", increment)
asset.export("decrement", decrement)
asset.export("fade", fade)
asset.export("fadeIn", fadeIn)
asset.export("fadeOut", fadeOut)
asset.export("fadeInOut", fadeInOut)
