/*
 * This file is part of NucleusFramework for Bukkit, licensed under the MIT License (MIT).
 *
 * Copyright (c) JCThePants (www.jcwhatever.com)
 *
 * 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.
 */


package com.jcwhatever.nucleus.scripting.api;

import com.jcwhatever.nucleus.sounds.SoundSettings;
import com.jcwhatever.nucleus.utils.player.PlayerUtils;
import com.jcwhatever.nucleus.scripting.IEvaluatedScript;
import com.jcwhatever.nucleus.scripting.ScriptApiInfo;
import com.jcwhatever.nucleus.sounds.ResourceSound;
import com.jcwhatever.nucleus.sounds.SoundManager;
import com.jcwhatever.nucleus.utils.PreCon;

import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;


@ScriptApiInfo(
        variableName = "sounds",
        description = "Provide scripts with API access to resource sounds.")
public class ScriptApiSounds extends NucleusScriptApi {

    private ApiObject _api;

    /**
     * Constructor. Automatically adds variable to script.
     *
     * @param plugin The owning plugin
     */
    public ScriptApiSounds(Plugin plugin) {
        super(plugin);
    }

    @Override
    public IScriptApiObject getApiObject(IEvaluatedScript script) {
        if (_api == null)
            _api = new ApiObject(getPlugin());

        return _api;
    }

    public static class ApiObject implements IScriptApiObject {

        private final Plugin _plugin;

        ApiObject(Plugin plugin) {
            _plugin = plugin;
        }

        @Override
        public boolean isDisposed() {
            return false;
        }

        @Override
        public void dispose() {
            // do nothing
        }

        /**
         * Play a resource sound to a player.
         *
         * @param player     The player.
         * @param soundName  The name of the resource sound.
         */
        public boolean play(Object player, String soundName) {
            PreCon.notNull(player);
            PreCon.notNullOrEmpty(soundName);

            Player p = PlayerUtils.getPlayer(player);
            PreCon.notNull(p);

            ResourceSound sound = SoundManager.getSound(soundName);
            if (sound == null)
                return false;

            SoundManager.playSound(_plugin, p, sound, new SoundSettings(500.0f, p.getLocation()));
            return true;
        }

        /**
         * Play a resource sound to a player at the specified coordinates
         * and with the specified volume.
         *
         * @param player     The player.
         * @param soundName  The name of the resource sound.
         * @param x          The X coordinates.
         * @param y          The Y coordinates.
         * @param z          The Z coordinates.
         * @param volume     The volume.
         */
        public boolean playAt(Object player, String soundName, int x, int y, int z, double volume) {
            PreCon.notNull(player);
            PreCon.notNullOrEmpty(soundName);

            Player p = PlayerUtils.getPlayer(player);
            PreCon.notNull(p);

            Location location = new Location(p.getWorld(), x, y, z);

            ResourceSound sound = SoundManager.getSound(soundName);
            if (sound == null)
                return false;

            SoundManager.playSound(_plugin, p, sound, new SoundSettings((float)volume, location));
            return false;
        }

        /**
         * Play a resource sound to a player at the specified coordinates
         * and with the specified volume.
         *
         * @param player     The player.
         * @param soundName  The name of the resource sound.
         * @param location   The location to play at.
         * @param volume     The volume.
         */
        public boolean playLocation(Object player, String soundName, Location location, double volume) {
            PreCon.notNull(player);
            PreCon.notNullOrEmpty(soundName);
            PreCon.notNull(location);

            Player p = PlayerUtils.getPlayer(player);
            PreCon.notNull(p);

            ResourceSound sound = SoundManager.getSound(soundName);
            if (sound == null)
                return false;

            SoundManager.playSound(_plugin, p, sound, new SoundSettings((float)volume, location));
            return false;
        }
    }

}

@@ -1271,116 +1271,8 @@
 i;%0A%0A
-import com.jcwhatever.nucleus.sounds.SoundSettings;%0Aimport com.jcwhatever.nucleus.utils.player.PlayerUtils;%0A
 impo
@@ -1380,16 +1380,63 @@
 piInfo;%0A
+import com.jcwhatever.nucleus.sounds.PlayList;%0A
 import c
@@ -1564,20 +1564,128 @@
 eus.
-utils.PreCon
+sounds.SoundSettings;%0Aimport com.jcwhatever.nucleus.utils.PreCon;%0Aimport com.jcwhatever.nucleus.utils.player.PlayerUtils
 ;%0A%0Ai
@@ -1778,16 +1778,68 @@
 lugin;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 %0A@Script
@@ -5430,16 +5430,885 @@
        %7D
+%0A%0A        /**%0A         * Create a new playlist.%0A         *%0A         * @param location    The location the sound is played from.%0A         * @param soundNames  The names of the resource sounds to play.%0A         */%0A        public PlayList createPlayList(Location location, String... soundNames) %7B%0A%0A            List%3CResourceSound%3E sounds = new ArrayList%3C%3E(soundNames.length);%0A%0A            for (String soundName : soundNames) %7B%0A                ResourceSound sound = SoundManager.getSound(soundName);%0A                if (sound == null)%0A                    throw new RuntimeException(%22The sound %22 + soundName + %22 was not found.%22);%0A%0A                sounds.add(sound);%0A            %7D%0A%0A            PlayList playList = new PlayList(_plugin);%0A%0A            playList.addSounds(sounds);%0A            playList.getSettings().addLocations(location);%0A%0A            return playList;%0A        %7D
 %0A    %7D%0A%0A

add #createPlayList method to ScriptApiSounds