#
# Aura Engine (https://gitlab.servus.at/aura/engine)
#
# Copyright (C) 2017-2020 - The Aura Engine Team.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Output streaming settings
# What a mess...
# s0_encoding =
#   get_setting("ogg", "stream_0_encoding", "AURA_ENGINE_STREAM_OUTPUT_ENCODING")
# s0_bitrate =
#   int_of_string(
#     get_setting("192", "stream_0_bitrate", "AURA_ENGINE_STREAM_OUTPUT_BITRATE")
#   )
# s0_host = get_setting("", "stream_0_host", "AURA_ENGINE_STREAM_OUTPUT_HOST")
# s0_port =
#   int_of_string(
#     get_setting("0", "stream_0_port", "AURA_ENGINE_STREAM_OUTPUT_PORT")
#   )
# s0_user = get_setting("", "stream_0_user", "AURA_ENGINE_STREAM_OUTPUT_USER")
# s0_pass =
#   get_setting("", "stream_0_password", "AURA_ENGINE_STREAM_OUTPUT_PASSWORD")
# s0_mount =
#   get_setting("", "stream_0_mountpoint", "AURA_ENGINE_STREAM_OUTPUT_MOUNTPOINT")
# s0_url = get_setting("", "stream_0_url", "AURA_ENGINE_STREAM_OUTPUT_URL")
# s0_desc =
#   get_setting(
#     "", "stream_0_description", "AURA_ENGINE_STREAM_OUTPUT_DESCRIPTION"
#   )
# s0_genre = get_setting("", "stream_0_genre", "AURA_ENGINE_STREAM_OUTPUT_GENRE")
# s0_name = get_setting("", "stream_0_name", "AURA_ENGINE_STREAM_OUTPUT_NAME")
# s0_channels =
#   get_setting("", "stream_0_channels", "AURA_ENGINE_STREAM_OUTPUT_CHANNELS")

# FIXME: this should move into the list of streams
# What is this used for anyway?
s0_connected = ref("")

# s1_connected = ref('')
# s2_connected = ref('')
# s3_connected = ref('')
# s4_connected = ref('')

# number of streams
stream_count = ref(0)

# list of streams
stream_list = ref([])

def create_stream(stream) =
  let url = string(stream.url)
  enc = ref(%vorbis(stereo = true))

  # set icy_metadata accoring to the encoder (enc) to send
  # metadata via icecast
  snd_icy_metadata = ref(false)
  if
    stream.encoding == "mp3"
  then
    enc := %mp3(stereo = true)
    snd_icy_metadata := true
  end

  # register a server function for every stream
  # TODO: what is this used for?
  server.register(
    namespace="out_http_#{stream_count()}",
    "connected",
    fun (s) ->
      begin
        ignore(s)
        s0_connected()
      end
  )

  # create a list of streams to keep track of the created streams
  # if
  #   list.assoc.mem(url, stream_list())
  # then
  #   "Stream for url #{url} already exists!"
  # else
  out_stream =
    output.icecast(
      id="out_http_#{stream_count()}",
      host=stream.host,
      port=int_of_float(stream.port),
      password=stream.password,
      mount=stream.mountpoint,
      fallible=true,
      url=stream.url,
      description=stream.description,
      name=stream.name,
      genre=stream.genre,
      user=stream.user,
      send_icy_metadata=snd_icy_metadata(),
      enc(),
      output_source
    )
  stream_count := stream_count() + 1

  # append the new stream to the list of streams, the key is the url
  # if we could alter the unit (stream) with all its values we could
  # save all of this in the stream unit itself
  stream_list := [...stream_list(), (url, out_stream.shutdown)]

  print(
    "Stream: #{url}"
  )
end

# def create_stream(stream) =
#   server.register(
#     namespace="out_http_#{stream_count()}",
#     "connected",
#     fun (s) ->
#       begin
#         ignore(s)
#         s0_connected()
#       end
#   )
#   stream_to_icecast(
#     "out_http_#{stream_count()}",
#     stream.encoding,
#     int_of_string(stream.bitrate),
#     stream.host,
#     int_of_float(stream.port),
#     stream.password,
#     stream.mount,
#     stream.url,
#     stream.desc,
#     stream.genre,
#     stream.user,
#     output_source,
#     "#{stream_count()}",
#     s0_connected,
#     stream.name,
#     stream.channels
#   )
#   stream_count := stream_count() + 1
#   print(
#     "Registered stream #{stream.url}"
#   )
# end

# itterate over all stream units (read from the yaml config)
list.iter(create_stream, config.stream)