package com.lenis0012.bukkit.ls;

import org.apache.commons.lang.RandomStringUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerPreLoginEvent;
import org.bukkit.event.player.PlayerPreLoginEvent.Result;
import org.bukkit.event.player.PlayerQuitEvent;

import com.lenis0012.bukkit.ls.util.StringUtil;

@SuppressWarnings("deprecation")
public class LoginListener implements Listener {
	private LoginSecurity plugin;
	public LoginListener(LoginSecurity i) { this.plugin = i; }
	
	@EventHandler (priority = EventPriority.MONITOR)
	public void onPlayerJoin(PlayerJoinEvent event) {
		final Player player = event.getPlayer();
		final String name = player.getName().toLowerCase();
		
		if(!player.getName().equals(StringUtil.cleanString(player.getName()))) {
			player.kickPlayer("Invalid username!");
			return;
		}
		
		plugin.playerJoinPrompt(player, name);
	}
	
	@EventHandler (priority = EventPriority.LOWEST)
	public void onPlayerPreLogin(PlayerPreLoginEvent event) {
		String name = event.getName();
		//Check if the player is already online
		for(Player player : Bukkit.getServer().getOnlinePlayers()) {
			String pname = player.getName().toLowerCase();
			if(plugin.AuthList.containsKey(pname))
				continue;
			
			if(pname.equalsIgnoreCase(name)) {
				event.setResult(Result.KICK_OTHER);
				event.setKickMessage("A player with this name is already online!");
			}
		}
	}
	
	@EventHandler
	public void onPlayerQuit(PlayerQuitEvent event) {
		Player player = event.getPlayer();
		String name = player.getName().toLowerCase();
		String ip = player.getAddress().getAddress().toString();
		
		if(!plugin.data.isRegistered(name))
			plugin.data.updateIp(name, ip);
		
		if(plugin.sesUse && !plugin.AuthList.containsKey(name) && plugin.data.isRegistered(name))
			plugin.thread.getSession().put(name, plugin.sesDelay);
	}
	
	@EventHandler
	public void onPlayerMove(PlayerMoveEvent event) {
		Player player = event.getPlayer();
		String name = player.getName().toLowerCase();
		
		if(plugin.AuthList.containsKey(name))
			player.teleport(event.getFrom());	
	}
	
	@EventHandler
	public void onBlockPlace(BlockPlaceEvent event) {
		Player player = event.getPlayer();
		String name = player.getName().toLowerCase();
		
		if(plugin.AuthList.containsKey(name))
			event.setCancelled(true);
	}
	
	@EventHandler
	public void onBlockBreak(BlockBreakEvent event) {
		Player player = event.getPlayer();
		String name = player.getName().toLowerCase();
		
		if(plugin.AuthList.containsKey(name))
			event.setCancelled(true);
	}
	
	@EventHandler
	public void onPlayerDropItem(PlayerDropItemEvent event) {
		Player player = event.getPlayer();
		String name = player.getName().toLowerCase();
		
		if(plugin.AuthList.containsKey(name))
			event.setCancelled(true);
	}
	
	@EventHandler
	public void onPlayerPickupItem(PlayerPickupItemEvent event) {
		Player player = event.getPlayer();
		String name = player.getName().toLowerCase();
		
		if(plugin.AuthList.containsKey(name))
			event.setCancelled(true);
	}
	
	@EventHandler
	public void onPlayerChat(PlayerChatEvent chat){
		Player player = chat.getPlayer();
		String pname = player.getName().toLowerCase();
		if(plugin.AuthList.containsKey(pname)){
			chat.setCancelled(true);
		}
	}

	@EventHandler
	public void onInventoryClick(InventoryClickEvent event) {
		Entity entity = event.getWhoClicked();
		if(!(entity instanceof Player))
			return;
		Player player = (Player)entity;
		String pname = player.getName().toLowerCase();
			
		if(plugin.AuthList.containsKey(pname)) {
			event.setCancelled(true);
		}
	}
	
	@EventHandler(priority=EventPriority.LOWEST)
	public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
		Player player = event.getPlayer();
		String name = player.getName().toLowerCase();
		if(plugin.AuthList.containsKey(name)){
		    if(!event.getMessage().startsWith("/login") && !event.getMessage().startsWith("/register")) {
		    	//faction fix start
		    	if(event.getMessage().startsWith("/f"))
		    		event.setMessage("/" + RandomStringUtils.randomAscii(name.length())); //this command does not exist :P
		    	//faction fix end
		    	event.setCancelled(true);
		  	}
		}
	}
}

@@ -3865,16 +3865,342 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%09%0A%09@EventHandler%0A%09public void OnHealthRegain(EntityRegainHealthEvent event) %7B%0A%09%09Entity entity = event.getEntity();%0A%09%09if(!(entity instanceof Player))%0A%09%09%09return;%0A%09%09Player player = (Player)entity;%0A%09%09String pname = player.getName().toLowerCase();%0A%09%09%09%0A%09%09if(plugin.AuthList.containsKey(pname)) %7B%0A%09%09%09event.setCancelled(true);%0A%09%09%7D%0A%09%7D
 %0A%0A%09@Even

Revert "removed one more redundant event handler"