package markehme.factionsplus.Cmds;

import markehme.factionsplus.FactionsPlus;

import org.bukkit.ChatColor;

import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions;
//import com.massivecraft.factions.struct;
import com.massivecraft.factions.cmd.FCommand;
import com.massivecraft.factions.struct.Permission;

public class CmdToggleState extends FCommand {
	
	public CmdToggleState() {
		super();
		
		this.aliases.add("toggle");
		this.aliases.add("togglestate");
		
		//this.requiredArgs.add("state");
		this.optionalArgs.put("faction", "faction");
		
		this.permission = Permission.HELP.node;
		this.disableOnLock = false;
		
		senderMustBePlayer = true;
		senderMustBeMember = true;
		
		this.setHelpShort("changes the Faction between peaceful");
		
	}
	
	// /f togglestate <faction>
	
	
	public void perform() {
		
		String factionToggling = this.argAsString(0);
		Faction factiont;
		
		if(!FactionsPlus.permission.has(sender, "factionsplus.togglestate.use")) {
			sender.sendMessage(ChatColor.RED + "No permission!");
			return;
		}
		
		if (factionToggling != null) {
			if(!FactionsPlus.permission.has(sender, "factionsplus.togglestate.others")) {
				sender.sendMessage(ChatColor.RED + "No permission!");
				return;
			}
			
			factiont = Factions.i.get(factionToggling);			
			
			Boolean authallow = false;
			
			if(FactionsPlus.config.getBoolean("leadersCanToggleState")) {
				if(fme.getRole().toString().contains("admin") || fme.getRole().toString().contains("LEADER")) { // 1.6.x
					authallow = true;
				}
			}
			
			if(FactionsPlus.config.getBoolean("officersCanToggleState")) {
				if(fme.getRole().toString().contains("mod") || fme.getRole().toString().contains("OFFICER")) {
					authallow = true;
				}
			}

			if(FactionsPlus.config.getBoolean("membersCanToggleState")) {
				authallow = true;
			}
			
			if(!authallow) {
				sender.sendMessage(ChatColor.RED + "Sorry, your ranking is not high enough to do that!");
				return;
			}
			
		} else {
			factiont = fme.getFaction();
		}
		
		if(factiont.isPeaceful()) {
			if(payForCommand(FactionsPlus.config.getInt("economy_costToToggleUpPeaceful"), "set faction to unpeaceful", "setting faction to unpeaceful")) {
				
				
				if(FactionsPlus.FactionsVersion.startsWith("1.7")) {
					//factiont.setFlag(com.massivecraft.factions.struct.FFlag.PEACEFUL, false);
				} else {
					factiont.setPeaceful(true);
				}
				
				sender.sendMessage("You have toggle the Faction to Peaceful!");
			}
		} else {
			if(payForCommand(FactionsPlus.config.getInt("economy_costToToggleDownPeaceful"), "set faction to peaceful", "setting faction to peaceful")) {
				if(FactionsPlus.FactionsVersion.startsWith("1.7")) {
					//factiont.setFlag(com.massivecraft.factions.struct.FFlag.PEACEFUL, true);
				} else {
					factiont.setPeaceful(false);
				}
				sender.sendMessage("You have removed peaceful status from the Faction!");
			}
			
		}
		
	}
}
@@ -2120,21 +2120,119 @@
 %09%7D%0D%0A%09%09%0D%0A
+%09%09//TODO: investigate if using %22peaceful%22 from Factions will bypass the pay for toggling states%0D%0A
 %09%09if(
+!
 factiont
@@ -2245,24 +2245,81 @@
 ceful()) %7B%0D%0A
+%09%09%09//if faction wasn't already peaceful, then we set it%0D%0A
 %09%09%09if(payFor
@@ -2381,32 +2381,35 @@
 eUpPeaceful%22), %22
+to 
 set faction to u
@@ -2399,34 +2399,32 @@
  set faction to 
-un
 peaceful%22, %22sett
@@ -2411,32 +2411,36 @@
  to peaceful%22, %22
+for 
 setting faction 
@@ -2446,10 +2446,8 @@
  to 
-un
 peac
@@ -2600,20 +2600,19 @@
 ACEFUL, 
-fals
+tru
 e);%0D%0A%09%09%09
@@ -2708,16 +2708,17 @@
 e toggle
+d
  the Fac
@@ -2753,24 +2753,76 @@
 %09%09%7D else %7B%0D%0A
+%09%09%09//faction was peaceful, we now remove this flag%0D%0A
 %09%09%09if(payFor
@@ -2898,35 +2898,45 @@
 ), %22
-set faction to peaceful%22, %22
+to remove the peaceful status%22, %22for 
 sett
@@ -2942,32 +2942,34 @@
 ting faction to 
+un
 peaceful%22)) %7B%0D%0A%09
@@ -3090,35 +3090,36 @@
 FFlag.PEACEFUL, 
-tru
+fals
 e);%0D%0A%09%09%09%09%7D else 

fix for toggles peaceful also sync-ed the commented 1.7 lines with the 1.6 lines