package com.arckenver.nations.listener;

import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.entity.MoveEntityEvent;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.channel.MessageChannel;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import com.arckenver.nations.ConfigHandler;
import com.arckenver.nations.DataHandler;
import com.arckenver.nations.LanguageHandler;
import com.arckenver.nations.Utils;
import com.arckenver.nations.object.Nation;
import com.arckenver.nations.object.Zone;

public class PlayerMoveListener
{
	@Listener
	public void onPlayerMove(MoveEntityEvent event, @First Player player)
	{
		if (event.getFromTransform().getLocation().getBlockX() == event.getToTransform().getLocation().getBlockX() && 
	            event.getFromTransform().getLocation().getBlockZ() == event.getToTransform().getLocation().getBlockZ())
	    {
	        return;
	    }
		if (!ConfigHandler.getNode("worlds").getNode(event.getToTransform().getExtent().getName()).getNode("enabled").getBoolean())
		{
			return;
		}
		
		Location<World> loc = event.getToTransform().getLocation();
		Nation nation = DataHandler.getNation(loc);
		Nation lastNationWalkedOn = DataHandler.getLastNationWalkedOn(player.getUniqueId());
		Zone zone = null;
		if (nation != null)
		{
			zone = nation.getZone(loc);
		}
		Zone lastZoneWalkedOn = DataHandler.getLastZoneWalkedOn(player.getUniqueId());
		if ((nation == null && lastNationWalkedOn == null) || (nation != null && lastNationWalkedOn != null && nation.getUUID().equals(lastNationWalkedOn.getUUID())))
		{
			if ((zone == null && lastZoneWalkedOn == null) || (zone != null && lastZoneWalkedOn != null && zone.getUUID().equals(lastZoneWalkedOn.getUUID())))
			{
				return;
			}
		}
		DataHandler.setLastNationWalkedOn(player.getUniqueId(), nation);
		DataHandler.setLastZoneWalkedOn(player.getUniqueId(), zone);
		
		Text.Builder builder = Text.builder("~ ").color(TextColors.GRAY);
		
		builder.append((nation == null) ? Text.of(TextColors.DARK_GREEN, LanguageHandler.IA) : Utils.nationClickable(TextColors.DARK_AQUA, nation.getName()));
		if (zone != null)
		{
			if (zone.isNamed())
			{
				builder.append(Text.of(TextColors.GRAY, " - "));
				builder.append(Utils.zoneClickable(TextColors.GREEN, zone.getName()));
			}
			if (zone.isOwned())
			{
				builder.append(Text.of(TextColors.GRAY, " - "));
				builder.append(Utils.citizenClickable(TextColors.YELLOW, DataHandler.getPlayerName(zone.getOwner())));
			}
			if (zone.isForSale())
			{
				builder.append(
						Text.of(TextColors.GRAY, " - "),
						Text.of(TextColors.YELLOW, "["),
						Utils.formatPrice(TextColors.YELLOW, zone.getPrice()),
						Text.of(TextColors.YELLOW, "]")
				);
			}
		}
		else if (nation != null && !nation.isAdmin())
		{
			builder.append(Text.of(TextColors.GRAY, " - "));
			builder.append(Utils.citizenClickable(TextColors.YELLOW, DataHandler.getPlayerName(nation.getPresident())));
		}

		builder.append(Text.of(TextColors.GRAY, " - "));
		builder.append((DataHandler.getFlag("pvp", loc)) ? Text.of(TextColors.DARK_RED, "(PvP)") : Text.of(TextColors.DARK_GREEN, "(No PvP)"));
		builder.append(Text.of(TextColors.GRAY, " ~"));
		
		player.sendMessage(builder.build());
		MessageChannel.TO_CONSOLE.send(Text.of(player.getName(), " entered area ", builder.build()));
	}
}

@@ -982,20 +982,11 @@
 & %0A%09
-            
+%09%09%09
 even
@@ -1090,23 +1090,14 @@
 ))%0A%09
-    %7B%0A%09        
+%09%7B%0A%09%09%09
 retu
@@ -1105,12 +1105,9 @@
 n;%0A%09
-    
+%09
 %7D%0A%09%09
@@ -1249,18 +1249,16 @@
 rn;%0A%09%09%7D%0A
-%09%09
 %0A%09%09Locat
@@ -2078,18 +2078,16 @@
  zone);%0A
-%09%09
 %0A%09%09Text.
@@ -2147,18 +2147,16 @@
 .GRAY);%0A
-%09%09
 %0A%09%09build
@@ -2329,198 +2329,8 @@
 %09%09%7B%0A
-%09%09%09if (zone.isNamed())%0A%09%09%09%7B%0A%09%09%09%09builder.append(Text.of(TextColors.GRAY, %22 - %22));%0A%09%09%09%09builder.append(Utils.zoneClickable(TextColors.GREEN, zone.getName()));%0A%09%09%09%7D%0A%09%09%09if (zone.isOwned())%0A%09%09%09%7B%0A%09
 %09%09%09b
@@ -2377,16 +2377,39 @@
  - %22));%0A
+%09%09%09if (zone.isOwned())%0A
 %09%09%09%09buil
@@ -2510,17 +2510,95 @@
 )));%0A%09%09%09
-%7D
+else%0A%09%09%09%09builder.append(Utils.zoneClickable(TextColors.GREEN, zone.getName()));
 %0A%09%09%09if (
@@ -2821,16 +2821,18 @@
 %5D%22)%0A%09%09%09%09
+%09%09
 );%0A%09%09%09%7D%0A
@@ -2949,32 +2949,97 @@
 %09builder.append(
+Text.of(DataHandler.getCitizenTitle(nation.getPresident()), %22 %22, 
 Utils.citizenCli
@@ -3110,24 +3110,25 @@
 esident())))
+)
 ;%0A%09%09%7D%0A%0A%09%09bui
@@ -3361,18 +3361,16 @@
 %22 ~%22));%0A
-%09%09
 %0A%09%09playe

Add president title when walking in his nation