#
# 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/>.



# Builds a metadata object from data passed as JSON
def build_metadata(s, json_string) =
    let json.parse (data : {
            show_name: string,
            show_id: int,
            timeslot_id: int,
            playlist_id: int,
            playlist_item: string,
            track_type: int,
            track_start: string,
            track_duration: int,
            track_title: string,
            track_album: string,
            track_artist: string,
        }) = json_string
    log("Received JSON to set track metadata on channel \
        '#{source.id(s)}' to:\n #{data}")

    [
        ("show_name", data.show_name),
        ("show_id", "#{data.show_id}"),
        ("timeslot_id", "#{data.timeslot_id}"),
        ("playlist_id", "#{data.playlist_id}"),
        ("playlist_item", "#{data.playlist_item}"),
        ("track_type", "#{data.track_type}"),
        ("track_start", data.track_start),
        ("track_duration", "#{data.track_duration}"),
        ("track_title", data.track_title),
        ("track_album", data.track_album),
        ("track_artist", data.track_artist),
    ]
end


# Reads the track duration
#   a.) when available from the file
#   b.) as a fallback from the meta field "track_duration"
#
# Returns
#   (int) duration in seconds
def get_meta_track_duration(meta) =
    track_duration = int_of_float(request.duration(meta["filename"]))
    if track_duration != -1 then
        track_duration
    else
        int_of_string(meta["track_duration"])
    end
end


# Posts a playlog to the Engine API
def post_playlog(api_url, data) =
    json_data = json()

    json_data.add("log_source", int_of_string(list.assoc("log_source", data)))
    json_data.add("show_name", list.assoc("show_name", data))
    json_data.add("show_id", int_of_string(list.assoc("show_id", data)))
    json_data.add("timeslot_id", int_of_string(list.assoc("timeslot_id", data)))
    json_data.add("playlist_id", int_of_string(list.assoc("playlist_id", data)))
    json_data.add("track_type", int_of_string(list.assoc("track_type", data)))
    json_data.add("track_start", list.assoc("track_start", data))
    json_data.add("track_duration", list.assoc("track_duration", data))
    json_data.add("track_title", list.assoc("track_title", data))
    json_data.add("track_album", list.assoc("track_album", data))
    json_data.add("track_artist", list.assoc("track_artist", data))
    if list.assoc("track_num", data) != "" then
        json_data.add("track_num", int_of_string(list.assoc("track_num", data)))
    end

    playlog = json.stringify(json_data)
    log("Posting playlog to '#{api_url}': #{playlog}")
    headers = [("Content-Type","application/json")]
    result = http.post(headers=headers, data="#{playlog}", "#{api_url}")

    if result.status_code < 400 then
        log("Successfully posted playlog to Engine API.")
    else
        log("ERROR during playlog POST: #{result.status_code} | #{result.status_message}")
    end
end


# Evaluate the type of source
#
# Returns
#   "fallback", "queue", "stream", "analog_in", "unknown_source"
def eval_source_type(source_id) =
    type_mapping = [
        ("fallback_folder", "fallback"),
        ("fallback_playlist", "fallback"),
        ("in_filesystem_0", "queue"),
        ("in_filesystem_0", "queue"),
        ("in_http_0", "stream"),
        ("in_http_1", "stream"),
        ("linein_0", "analog_in"),
        ("linein_1", "analog_in"),
        ("linein_2", "analog_in"),
        ("linein_3", "analog_in"),
        ("linein_4", "analog_in")
    ]
    let source_type = list.assoc(
        default="unknown_source",
        source_id,
        type_mapping
    )
    source_type
end


# Evaluates the track type based on the given:
#   a.) "meta.track_type" passed as annotation, and if not available on
#   b.) "engine_current_track_type" passed via server function
#   c.) "meta.source" and if not available on
#   d.) configured default track type setting
#
# Returns:
#   0=QUEUE/FILE, 1=STREAM, 2=LIVE ANALOG, 3=PLAYLIST
#
def eval_track_type(meta_track_type, meta_source) =
    type_mapping = [
        ("fallback_folder", "0"),
        ("fallback_playlist", "3"),
        ("in_filesystem_0", "0"),
        ("in_filesystem_0", "0"),
        ("in_http_0", "1"),
        ("in_http_1", "1"),
        ("linein_0", "2"),
        ("linein_1", "2"),
        ("linein_2", "2"),
        ("linein_3", "2"),
        ("linein_4", "2")
    ]

    track_type = list.assoc(
        default=engine_default_track_type,
        meta_source,
        type_mapping
    )

    if meta_track_type != "" then
        meta_track_type
    elsif track_type != "" then
        track_type
    else
        engine_default_track_type
    end
end