package com.jcwhatever.musical.commands.regions;

import com.jcwhatever.musical.Lang;
import com.jcwhatever.musical.MusicalRegions;
import com.jcwhatever.musical.regions.MusicRegion;
import com.jcwhatever.musical.regions.RegionManager;
import com.jcwhatever.nucleus.commands.AbstractCommand;
import com.jcwhatever.nucleus.commands.CommandInfo;
import com.jcwhatever.nucleus.commands.arguments.CommandArguments;
import com.jcwhatever.nucleus.commands.arguments.LocationResponse;
import com.jcwhatever.nucleus.commands.exceptions.CommandException;
import com.jcwhatever.nucleus.managed.language.Localizable;
import com.jcwhatever.nucleus.utils.text.TextUtils;

import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

@CommandInfo(
        command="setsource",
        staticParams={"regionName", "location"},
        description="Change the location a musical regions sound is played from.",
        paramDescriptions = {
                "regionName= The name of the region.",
                "location= Type 'center' to place the location at the regions center. " +
                        "Otherwise specify specific location: {LOCATION}"
        })

public class SetSourceSubCommand extends AbstractCommand {

    @Localizable static final String _REGION_NOT_FOUND =
            "A musical region with the name '{0: region name}' was not found.";

    @Localizable static final String _INVALID_WORLD =
            "The specified location must be in the same world as the region.";

    @Localizable static final String _SUCCESS =
            "Musical region '{0: region name}' sound source location updated to '{1: location}'.";

    @Override
    public void execute(CommandSender sender, CommandArguments args) throws CommandException {

        String regionName = args.getName("regionName");

        RegionManager regionManager = MusicalRegions.getRegionManager();
        final MusicRegion region = regionManager.get(regionName);
        if (region == null) {
            tellError(sender, Lang.get(_REGION_NOT_FOUND, regionName));
            return; // finish
        }

        if (args.getString("location").equalsIgnoreCase("center")) {
            setSource(sender, region, region.getCenter());
        }
        else {

            args.getLocation(sender, "location", new LocationResponse() {
                @Override
                public void onLocationRetrieved(Player p, Location result) {
                    setSource(p, region, result);
                }
            });
        }
    }

    private void setSource(CommandSender sender, MusicRegion region, Location location) {

        if (location.getWorld() == null || !location.getWorld().equals(region.getWorld())) {
            tellError(sender, Lang.get(_INVALID_WORLD));
            return;
        }

        region.setSoundSource(location);

        tellSuccess(sender, Lang.get(_SUCCESS, region.getName(),
                TextUtils.formatLocation(location, true)));
    }
}

@@ -453,24 +453,25 @@
 guments.
+I
 Location
 Response
@@ -462,24 +462,23 @@
 Location
-Response
+Handler
 ;%0Aimport
@@ -2327,24 +2327,25 @@
 n%22, new 
+I
 Location
 Response
@@ -2340,16 +2340,15 @@
 tion
-Response
+Handler
 () %7B
@@ -2430,16 +2430,21 @@
 Player p
+layer
 , Locati
@@ -2487,16 +2487,21 @@
 Source(p
+layer
 , region

rename LocationResponse to ILocationHandler