package com.jcwhatever.musical;

import com.jcwhatever.musical.regions.MusicRegion;
import com.jcwhatever.nucleus.scripting.IEvaluatedScript;
import com.jcwhatever.nucleus.scripting.ScriptApiInfo;
import com.jcwhatever.nucleus.scripting.api.IScriptApiObject;
import com.jcwhatever.nucleus.scripting.api.NucleusScriptApi;
import com.jcwhatever.nucleus.sounds.ResourceSound;
import com.jcwhatever.nucleus.utils.PreCon;
import com.jcwhatever.nucleus.utils.player.PlayerUtils;

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

import java.util.List;

/*
 * 
 */
@ScriptApiInfo(
        variableName = "musicalRegions",
        description = "Script API access to MusicRegions plugin.")
public class MusicScriptApi extends NucleusScriptApi {

    /**
     * Constructor.
     *
     * @param plugin The owning plugin
     */
    public MusicScriptApi(Plugin plugin) {
        super(plugin);
    }

    @Override
    public IScriptApiObject getApiObject(IEvaluatedScript script) {
        return new ApiObject();
    }

    public static class ApiObject implements IScriptApiObject {

        boolean _isDisposed;

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

        @Override
        public void dispose() {
            _isDisposed = true;
        }

        /**
         * Determine if a player is excluded from hearing a regions
         * playlist.
         *
         * @param regionName  The name of the region to check.
         * @param player      The player to check.
         */
        public boolean isExcluded(String regionName, Object player) {
            PreCon.notNullOrEmpty(regionName);
            PreCon.notNull(player);

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

            MusicRegion region = MusicalRegions.getRegionManager().get(regionName);
            PreCon.notNull(region);

            return region.isExcluded(p);
        }

        /**
         * Exclude a player from hearing a regions playlist.
         *
         * @param regionName  The name of the region to exclude the player from.
         * @param player      The player to exclude.
         */
        public void excludePlayer(String regionName, Object player) {
            PreCon.notNullOrEmpty(regionName);
            PreCon.notNull(player);

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

            MusicRegion region = MusicalRegions.getRegionManager().get(regionName);
            PreCon.notNull(region);

            region.exclude(p);
        }

        /**
         * Re-include a player to hear a regions playlist.
         *
         * @param regionName  The name of the region.
         * @param player      The player.
         */
        public void includePlayer(String regionName, Object player) {
            PreCon.notNullOrEmpty(regionName);
            PreCon.notNull(player);

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

            MusicRegion region = MusicalRegions.getRegionManager().get(regionName);
            PreCon.notNull(region);

            region.include(p);
        }

        /**
         * Get a musical regions sound source location.
         *
         * @param regionName  The name of the region to check.
         */
        public Location getSoundSource(String regionName) {
            PreCon.notNullOrEmpty(regionName);

            MusicRegion region = MusicalRegions.getRegionManager().get(regionName);
            PreCon.notNull(region);

            return region.getSoundSource();
        }

        /**
         * Get a musical regions sound volume.
         *
         * @param regionName  The name of the region to check.
         */
        public float getSoundVolume(String regionName) {
            PreCon.notNullOrEmpty(regionName);

            MusicRegion region = MusicalRegions.getRegionManager().get(regionName);
            PreCon.notNull(region);

            return region.getSoundVolume();
        }

        /**
         * Get a musical regions sounds.
         *
         * @param regionName  The name of the region to check.
         */
        public List<ResourceSound> getSounds(String regionName) {
            PreCon.notNullOrEmpty(regionName);

            MusicRegion region = MusicalRegions.getRegionManager().get(regionName);
            PreCon.notNull(region);

            return region.getSounds();
        }
    }
}

@@ -26,16 +26,72 @@
 sical;%0A%0A
+import com.jcwhatever.musical.playlists.RegionPlayList;%0A
 import c
@@ -648,12 +648,40 @@
 %0A%0A/*
+*
 %0A * 
+Musical Regions script API.
 %0A */
@@ -4587,16 +4587,328 @@
        %7D
+%0A%0A        /**%0A         * Get a playlist by name.%0A         *%0A         * @param listName  The name of the playlist.%0A         */%0A        public RegionPlayList getPlayList(String listName) %7B%0A            PreCon.notNullOrEmpty(listName);%0A%0A            return MusicalRegions.getPlayListManager().get(listName);%0A        %7D
 %0A    %7D%0A%7D

add #getPlayList to MusicScriptApi