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



#####################################
#            FUNCTIONS              #
#####################################

# Clear all requests in the queue
def clear_queue(s) =
    q = s.queue()
    s.set_queue([])
    s.skip()
    list.iter(request.destroy, q)
end


#####################################
#           QUEUE SOURCES           #
#####################################

# Create Sources
input_queue_raw_0 = request.queue(id="in_queue_0")
input_queue_raw_1 = request.queue(id="in_queue_1")

# Apply ReplayGain normalization, if metadata available
input_queue_0 = amplify(id="in_queue_0", 1., override="replay_gain", input_queue_raw_0)
input_queue_1 = amplify(id="in_queue_1", 1., override="replay_gain", input_queue_raw_1)
# See https://github.com/savonet/liquidsoap/discussions/2537
# input_queue_0 = replaygain(input_queue_0)
# input_queue_1 = replaygain(input_queue_1)

# Add Event Handlers
input_queue_0.on_metadata(on_metadata_notification)
input_queue_1.on_metadata(on_metadata_notification)

#####################################
#          SERVER FUNCTIONS         #
#####################################

# Clear Default Queue A
server.register(namespace=source.id(input_queue_0),
        description="Skip & clear all items from queue A.",
        usage="clear",
        "clear",

    fun (s) ->
        begin
            ignore(s)
            clear_queue(input_queue_raw_0)
            "OK"
        end
    )

# Clear Default Queue B
server.register(namespace=source.id(input_queue_1),
        description="Skip & clear all items from queue B.",
        usage="clear",
        "clear",

    fun (s) ->
        begin
            ignore(s)
            clear_queue(input_queue_raw_1)
            "OK"
        end
    )

# Seek Filesystem Queue A
server.register(namespace = source.id(input_queue_0),
    description="FFWD given amount of seconds",
    usage = "roll <duration in seconds>",
    "roll",

    fun (t) ->
        begin
            log("Rolling #{t} seconds")
            t = float_of_string(default=0.,t)
            ret = source.seek(input_queue_0, t)
            "OK"
        end
    )

# Seek Filesystem Queue B
server.register(namespace = source.id(input_queue_1),
    description="FFWD given amount of seconds",
    usage = "roll <duration in seconds>",
    "roll",

    fun (t) ->
        begin
            log("Rolling #{t} seconds")
            t = float_of_string(default=0.,t)
            ret = source.seek(input_queue_1, t)
            "OK"
        end
    )