package me.mcsheepmaster.plugin.sNoHax;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

//import net.minecraft.server.v1_8_R3.IChatBaseComponent;
//import net.minecraft.server.v1_8_R3.PacketPlayOutChat;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
//import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;

public class Alerter implements CommandExecutor, Listener {

	Main main;
	public Alerter(Main main) {
		this.main = main;
	}
	
	private ArrayList<String> silent = new ArrayList<>();
	
	public void alertHack(Player player, HackType type, int counter) {
		if(AutoBan.getInstance().isAuto(player)) {return;}
		if(player.hasPermission(Permission.ALL_BYPASS)) {return;}
		CheatAlertEvent event = new CheatAlertEvent(player, type, counter);
		Bukkit.getPluginManager().callEvent(event);
		if(event.isCancelled()) {
			return;
		}
		for(Player online : Bukkit.getOnlinePlayers()) {
			if(online.hasPermission(Permission.ALERT)) {
				if(!silent.contains(online.getName())) {
					String msg;
					if(type == HackType.FLY) {
						msg = ChatColor.DARK_RED + player.getName() + " is flying! (" + counter + ")";
					}
					else if(type == HackType.DERP) {
						msg = ChatColor.DARK_RED + player.getName() + " is derping! (" + counter + ")";
					}
					else if(type == HackType.NOKNOCKBACK) {
						msg = ChatColor.DARK_RED + player.getName() + " isn't taking knockback!";
					}
					else if(type == HackType.SPEED) {
						msg = ChatColor.DARK_RED + player.getName() + " is speeding!";
					}
					else if(type == HackType.SPRINT) {
						msg = ChatColor.DARK_RED + player.getName() + " is sprinting but not moving!";
					}
					else if(type == HackType.NOFALL) {
						msg = ChatColor.DARK_RED + player.getName() + " fell, but didn't take damage! (" + counter + ")";
					}
					else if(type == HackType.HJUMP) {
						msg = ChatColor.DARK_RED + player.getName() + " is jumping too high!";
					}
					else if(type == HackType.FASTBOW) {
						msg = ChatColor.DARK_RED + player.getName() + " is shooting their bow too fast! (" + counter + ")";
					}
					else if(type == HackType.FF) {
						msg = ChatColor.DARK_RED + player.getName() + " hit behind themself! (" + counter + ")";
					}
					else if(type == HackType.SPAM) {
						msg = ChatColor.DARK_RED + player.getName() + " is spamming! (" + counter + ")";
					}
					else if(type == HackType.SWEAR) {
						msg = ChatColor.DARK_RED + player.getName() + " just swore! (" + counter + ")";
					}
					else if(type == HackType.FASTBREAK) {
						msg = ChatColor.DARK_RED + player.getName() + " is breaking blocks to fast! (" + counter + ")";
					}
					else if(type == HackType.FASTPLACE) {
						msg = ChatColor.DARK_RED + player.getName() + " is placing blocks too fast! (" + counter + ")";
					}
					else if(type == HackType.FASTHIT) {
						msg = ChatColor.DARK_RED + player.getName() + " is hitting too fast! (" + counter + ")";
					}
					else if(type == HackType.WATERWALK) {
						msg = ChatColor.DARK_RED + player.getName() + " is walking on water! (" + counter + ")";
					}
					else if(type == HackType.CMD_SPAM) {
						msg = ChatColor.DARK_RED + player.getName() + " is spamming commands! (" + counter + ")";
					}
					else if(type == HackType.REGEN) {
						msg = ChatColor.DARK_RED + player.getName() + " is regaining health to fast! (" + counter + ")";
					}
					else {
						msg = ChatColor.DARK_RED + player.getName() + " has been detected for " + type.toString() + "!";
					}

					if(main.getConfig().getBoolean("config.console_alert")) {
						System.out.println(msg);
					}
					online.sendMessage(msg);
					/*msg = msg + ",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" + "tp " + player.getName() + "\"}]";
					IChatBaseComponent comp = IChatBaseComponent.ChatSerializer.a(msg);
					PacketPlayOutChat packet = new PacketPlayOutChat(comp);
					((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);*/

					/*if(counterClass.get(player.getName()) != null) {
						counterClass.put(player.getName(), counterClass.get(player.getName())+1);
					} else {
						counterClass.put(player.getName(), 1);
					}

					/Main main = new Main();
					if(counterClass.get(player.getName()) == main.getConfig().getInt("probability.possibly") && counterClass.get(player.getName()) < main.getConfig().getInt("probability.probably")) {
						setHackProbability(player, HackProbability.POSSIBLY);
					}
					if(counterClass.get(player.getName()) == main.getConfig().getInt("probability.probably") && counterClass.get(player.getName()) < main.getConfig().getInt("probability.definitly")) {
						setHackProbability(player, HackProbability.PROBABLY);
					}
					if(counterClass.get(player.getName()) == main.getConfig().getInt("probability.definitely")) {
						setHackProbability(player, HackProbability.DEFINITELY);
					}*/
				}
			}
		}
	}

	private Map<String, Integer> counterClass = new HashMap<>();
	
	public void setHackProbability(Player player, HackProbability prob) {
		HackProbability before = probability.get(player.getName());
		CheatProbabilityChangeEvent event = new CheatProbabilityChangeEvent(player, before, prob);
		Bukkit.getPluginManager().callEvent(event);
		if(event.isCancelled()) {return;}
		probability.put(player.getName(), prob);
		System.out.println(ChatColor.DARK_RED + player.getName() + "'s hack probability changed to " + prob + "!");
		for(Player online : Bukkit.getOnlinePlayers()) {
			if(online.hasPermission(Permission.PROBABILITY_ALERT)) {
				online.sendMessage(ChatColor.DARK_RED + player.getName() + "'s hack probability changed to " + prob + "!");
			}
		}
	}
	
	public HackProbability getHackProbability(Player player) {
		return probability.get(player.getName());
	}
	
	private Map<String, HackProbability> probability = new HashMap<>();
	
	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String tag, String[] args) {
		if(!(sender instanceof Player)) {
			sender.sendMessage(ChatColor.RED + "Player Command Only!");
			return true;
		}
		Player player = (Player) sender;
		if(!(player.hasPermission(Permission.ALERT))) {
			player.sendMessage(ChatColor.WHITE + "Unknown Command! Type \"/help\" for help.");
			return true;
		}
		else {
			if(tag.equalsIgnoreCase("silent")) {
				if(silent.contains(player.getName())) {
					player.sendMessage(ChatColor.RED + "Detection Already Hidden!");
					return true;
				}
				else {
					silent.add(player.getName());
					player.sendMessage(ChatColor.RED + "All alerts are now invisible for you.");
					return true;
				}
			}
		}
		return true;
	}
	
	@EventHandler
	public void onLeave(PlayerQuitEvent event) {
		Player player = event.getPlayer();
		if(silent.contains(player.getName())) {
			silent.remove(player.getName());
		}
	}
	
	@EventHandler
	public void onKick(PlayerKickEvent event) {
		Player player = event.getPlayer();
		if(silent.contains(player.getName())) {
			silent.remove(player.getName());
		}
	}
	
	private static Alerter alerter = new Alerter(new Main());
	public static Alerter getInstance() {return alerter;}

}