package uk.co.eluinhost.ultrahardcore.commands;

import java.util.List;

import org.bukkit.command.CommandSender;
import uk.co.eluinhost.commands.Command;
import uk.co.eluinhost.commands.CommandRequest;
import uk.co.eluinhost.features.IFeature;
import uk.co.eluinhost.features.FeatureManager;

public class FeatureCommand extends SimpleCommand {

    public static final String FEATURE_LIST_PERMISSION = "UHC.feature.list";
    public static final String FEATURE_TOGGLE_PERMISSION = "UHC.feature.toggle";

    /**
     * Ran on /feature
     * @param request request params
     */
    @Command(trigger = "feature",
            identifier = "FeatureCommand")
    public void onFeatureCommand(CommandRequest request){
        //TODO show syntax?
    }

    /**
     * Ran on /feature list
     * @param request request params
     */
    @Command(trigger = "list",
            identifier = "FeatureListCommand",
            minArgs = 0,
            maxArgs = 0,
            permission = FEATURE_LIST_PERMISSION,
            parentID = "FeatureCommand")
    public void onFeatureListCommand(CommandRequest request){
        CommandSender sender = request.getSender();
        List<IFeature> features = FeatureManager.getInstance().getFeatures();
        sender.sendMessage(translate("features.loaded.header").replaceAll("%amount%",String.valueOf(features.size())));
        if (features.isEmpty()) {
            sender.sendMessage(translate("features.loaded.none"));
        }
        for (IFeature feature : features) {
            String message = translate(feature.isEnabled()?"features.loaded.on":"features.loaded.off");
            sender.sendMessage(message.replaceAll("%id%",feature.getFeatureID()).replaceAll("%desc%",feature.getDescription()));
        }
    }

    /**
     * Ran on /feature on {name}
     * @param request request params
     */
    @Command(trigger = "on",
            identifier = "FeatureOnCommand",
            minArgs = 1,
            maxArgs = 1,
            permission = FEATURE_TOGGLE_PERMISSION,
            parentID = "FeatureCommand")
    public void onFeatureOnCommand(CommandRequest request){
        IFeature feature = FeatureManager.getInstance().getFeatureByID(request.getFirstArg());
        if(null == feature){
            request.getSender().sendMessage(translate("features.not_found").replaceAll("%id%",request.getFirstArg()));
            return;
        }
        if(feature.isEnabled()){
            request.getSender().sendMessage(translate("features.already_enabled"));
            return;
        }
        if(!feature.enableFeature()){
            request.getSender().sendMessage(translate("features.enabled_cancelled"));
            return;
        }
        request.getSender().sendMessage(translate("features.enabled"));
    }

    /**
     * Ran on /feature off {name}
     * @param request request params
     */
    @Command(trigger = "off",
            identifier = "FeatureOffCommand",
            minArgs = 1,
            maxArgs = 1,
            permission = FEATURE_TOGGLE_PERMISSION,
            parentID = "FeatureCommand")
    public void onFeatureOffCommand(CommandRequest request){
        IFeature feature = FeatureManager.getInstance().getFeatureByID(request.getFirstArg());
        if(null == feature){
            request.getSender().sendMessage(translate("features.not_found").replaceAll("%id%",request.getFirstArg()));
            return;
        }
        if(!feature.isEnabled()){
            request.getSender().sendMessage(translate("features.already_disabled"));
            return;
        }
        if(!feature.disableFeature()){
            request.getSender().sendMessage(translate("features.disabled_cancelled"));
            return;
        }
        request.getSender().sendMessage(translate("features.disabled"));
    }
}

@@ -1111,60 +1111,8 @@
 t)%7B%0A
-        CommandSender sender = request.getSender();%0A
     
@@ -1185,38 +1185,39 @@
 ures();%0A        
-sender
+request
 .sendMessage(tra
@@ -1344,38 +1344,39 @@
 ) %7B%0A            
-sender
+request
 .sendMessage(tra
@@ -1582,14 +1582,15 @@
     
-sender
+request
 .sen
@@ -2216,36 +2216,24 @@
     request.
-getSender().
 sendMessage(
@@ -2386,36 +2386,24 @@
     request.
-getSender().
 sendMessage(
@@ -2526,36 +2526,24 @@
     request.
-getSender().
 sendMessage(
@@ -2626,36 +2626,24 @@
     request.
-getSender().
 sendMessage(
@@ -3188,36 +3188,24 @@
     request.
-getSender().
 sendMessage(
@@ -3359,36 +3359,24 @@
     request.
-getSender().
 sendMessage(
@@ -3501,36 +3501,24 @@
     request.
-getSender().
 sendMessage(
@@ -3610,20 +3610,8 @@
 est.
-getSender().
 send

use request to send message