/**
* Copyright (c) 2016 Aleksi 'Allexit' Talarmo
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var Jukebox;
(function (Jukebox) {
    /**
    * The Jukebox Player is a program which when run sets up a virtual stereo
    * so you can hear the sounds played by stdio's sound-methods!
    */
    class Player {
        constructor() {
            this.soundCounter = 0;
            this.loadedSounds = {};
            this.soundChannels = {};
        }
        create(terminal, stdio, args) {
            this.terminal = terminal;
            stdio.setAudioOutput(this);
        }
        enable() { }
        disable() { }
        onClose() {
            return true;
        }
        play(channel, sound) {
            let ch = channel | 0;
            if (!(ch in this.soundChannels)) {
                // Create a new channel
                let audio = $(document.createElement("audio"));
                audio.attr("autoplay", "true");
                audio.appendTo($(this.terminal.selector));
                this.soundChannels[ch] = {
                    element: audio,
                    sound: undefined
                };
            }
            let currChannel = this.soundChannels[ch];
            let oldSound = currChannel.sound;
            if (sound !== undefined) {
                if (!(sound.soundRefrence in this.loadedSounds)) {
                    console.error("Unable to find sound refrence " + sound.soundRefrence);
                    return;
                }
                sound.onEndCallback = sound.onEndCallback || (() => { });
                sound.onStartCallback = sound.onStartCallback || (() => { });
                if (oldSound) {
                    currChannel.element.prop("currentTime", "0");
                }
                currChannel.sound = sound;
            }
            if (oldSound != currChannel.sound) {
                currChannel.element.empty();
                currChannel.element.trigger("pause");
                if (oldSound) {
                    oldSound.onEndCallback();
                }
                if (currChannel.sound !== undefined) {
                    currChannel.element.append(this.loadedSounds[currChannel.sound.soundRefrence]);
                }
            }
            if (currChannel.sound && currChannel.element) {
                currChannel.element.prop("volume", Math.min(currChannel.sound.volume, 1));
                if (currChannel.sound.loops) {
                    currChannel.element.attr("loop", "true");
                }
                else {
                    currChannel.element.removeAttr("loop");
                }
                currChannel.sound.onStartCallback();
                currChannel.element.trigger("play");
            }
        }
        pause(channel) {
            let ch = channel | 0;
            if (!(ch in this.soundChannels)) {
                return;
            }
            this.soundChannels[ch].sound.onEndCallback();
            this.soundChannels[ch].element.trigger("pause");
        }
        stop(channel) {
            let ch = channel | 0;
            if (!(ch in this.soundChannels)) {
                return;
            }
            this.soundChannels[ch].sound.onEndCallback();
            this.soundChannels[ch].element.prop("currentTime", "0");
            this.soundChannels[ch].element.trigger("pause");
        }
        loadSound(path, onready) {
            let id = this.soundCounter++ + "";
            let source = $(document.createElement("source"));
            source.attr("src", path);
            source.ready(() => { if (onready) {
                onready(id);
            } });
            source.appendTo($(this.terminal.selector));
            this.loadedSounds[id] = source;
            return "";
        }
    }
    Jukebox.Player = Player;
    function soundGuard(o) {
        return o.soundRefrence !== undefined;
    }
})(Jukebox || (Jukebox = {}));
