package com.untamedears.citadel.command.commands;

import static com.untamedears.citadel.Utility.getSecurityLevel;
import static com.untamedears.citadel.Utility.sendMessage;
import static com.untamedears.citadel.Utility.setMultiMode;

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

import com.untamedears.citadel.Citadel;
import com.untamedears.citadel.GroupManager;
import com.untamedears.citadel.PlacementMode;
import com.untamedears.citadel.SecurityLevel;
import com.untamedears.citadel.command.PlayerCommand;
import com.untamedears.citadel.entity.Faction;
import com.untamedears.citadel.entity.PlayerState;
import com.untamedears.citadel.entity.ReinforcementMaterial;

/**
 * User: JonnyD
 * Date: 7/18/12
 * Time: 11:57 PM
 */
public class FortifyCommand extends PlayerCommand {

	public FortifyCommand() {
		super("Fority Mode");
		setDescription("Toggle fortification mode");
		setUsage("/ctfortify §8[security-level]");
		setArgumentRange(0, 2);
		setIdentifiers(new String[] {"ctfortify", "ctf"});
	}

	public boolean execute(CommandSender sender, String[] args) {
		Player player = (Player) sender;
		PlayerState state = PlayerState.get(player);
		
		String secLevel = null;
		String groupName = null;
		if(args.length != 0){
			secLevel = args[0];
			if(args.length == 2){
				groupName = args[1];
			}
		}
		
		if(secLevel != null && secLevel.equalsIgnoreCase("group")){
                        Faction group;
                        if(!(groupName == null) && !(groupName.isEmpty()) && !(groupName.equals(""))){
                            GroupManager groupManager = Citadel.getGroupManager();
                            group = groupManager.getGroup(groupName);
                        } else if (state.getMode() == PlacementMode.FORTIFICATION && state.getSecurityLevel() == SecurityLevel.GROUP) {
                            /* Default to current faction */
                            group = state.getFaction();
                        } else {
                            sender.sendMessage(new StringBuilder().append("§cYou must specify a group in group fortification mode").toString());
                            sender.sendMessage(new StringBuilder().append("§cUsage:§e ").append("/ctfortify §8group <group-name>").toString());
                            return true;
                        }
			if(group == null){
				sendMessage(sender, ChatColor.RED, "Group doesn't exist");
				return true;
			}
			String senderName = sender.getName();
			if(!group.isFounder(senderName) && !group.isModerator(senderName)){
				sendMessage(sender, ChatColor.RED, "Invalid permission to use this group");
				return true;
			}
			if(group.isPersonalGroup()){
				sendMessage(sender, ChatColor.RED, "You cannot share your default group");
				return true;
			}
			state.setFaction(group);
		} else {
			state.setFaction(Citadel.getMemberManager().getMember(player).getPersonalGroup());
		}
		
		SecurityLevel securityLevel = getSecurityLevel(args, player);
        if (securityLevel == null) return false;

        ReinforcementMaterial material = ReinforcementMaterial.get(player.getItemInHand().getType());
        if (state.getMode() == PlacementMode.FORTIFICATION) {
            // Only change material if a valid reinforcement material in hand and not current reinforcement
            if (material != null && material != state.getReinforcementMaterial()) {
                // Switch reinforcement materials without turning off and on again
                state.reset();
                state.setFortificationMaterial(material);
            }
            setMultiMode(PlacementMode.FORTIFICATION, securityLevel, args, player, state);
        } else {
            if (material == null) {
                sendMessage(sender, ChatColor.YELLOW, "Invalid reinforcement material %s", player.getItemInHand().getType().name());
            } else {
                state.setFortificationMaterial(material);
                setMultiMode(PlacementMode.FORTIFICATION, securityLevel, args, player, state);
            }
        }
        
        return true;
	}

}

@@ -1418,27 +1418,140 @@
 %09%7D%0D%0A
-%09%09%0D%0A%09%09
+                %0D%0A                SecurityLevel securityLevel = getSecurityLevel(args, player);%0D%0A                
 if
+ 
 (sec
+urity
 Level 
-!
+=
 = nu
@@ -1561,41 +1561,243 @@
 && s
-ecLevel.equalsIgnoreCase(%22group%22)
+tate.getMode() == PlacementMode.FORTIFICATION) %7B%0D%0A                    securityLevel = state.getSecurityLevel();%0D%0A                %7D%0D%0A                if (securityLevel == null) return false;%0D%0A%09%09%0D%0A%09%09if(securityLevel == SecurityLevel.GROUP
 )%7B%0D%0A
@@ -3348,127 +3348,8 @@
 %09%09%7D%0D
-%0A%09%09%0D%0A%09%09SecurityLevel securityLevel = getSecurityLevel(args, player);%0D%0A        if (securityLevel == null) return false;%0D
 %0A%0D%0A 

Set security level, not just string used for check