package me.nallar.tickthreading.minecraft.commands;

import java.util.List;

import javassist.is.faulty.Timings;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.minecraft.TickManager;
import me.nallar.tickthreading.minecraft.TickThreading;
import me.nallar.tickthreading.util.TableFormatter;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.Entity;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;

public class ProfileCommand extends Command {
	public static String name = "profile";

	@Override
	public String getCommandName() {
		return name;
	}

	@Override
	public boolean canCommandSenderUseCommand(ICommandSender commandSender) {
		return !TickThreading.instance.requireOpForProfileCommand || super.canCommandSenderUseCommand(commandSender);
	}

	@Override
	public void processCommand(final ICommandSender commandSender, List<String> arguments) {
		World world = DimensionManager.getWorld(0);
		long time_ = 10;
		boolean entity_ = false;
		boolean location_ = false;
		try {
			if (!arguments.isEmpty()) {
				entity_ = "e".equals(arguments.get(0));
				if ("c".equals(arguments.get(0))) {
					entity_ = true;
					location_ = true;
				}
			}
			if (arguments.size() > 1) {
				time_ = Integer.valueOf(arguments.get(1));
			}
			if (arguments.size() > 2) {
				world = DimensionManager.getWorld(Integer.valueOf(arguments.get(2)));
			} else if (commandSender instanceof Entity) {
				world = ((Entity) commandSender).worldObj;
			}
		} catch (Exception e) {
			world = null;
		}
		if (world == null) {
			sendChat(commandSender, "Usage: /profile [type=a/e/c] [time=10] [dimensionid=current dimension]");
			return;
		}
		final TickManager manager = TickThreading.instance.getManager(world);
		final long time = time_;
		final boolean entity = entity_;
		final boolean location = location_;
		final int hashCode = commandSender instanceof Entity ? manager.getHashCode((Entity) commandSender) : 0;
		if (entity) {
			if (location) {
				try {
					manager.getEntityCallable(hashCode).profilingEnabled = true;
					manager.getTileEntityCallable(hashCode).profilingEnabled = true;
				} catch (NullPointerException ignored) {
				}
			} else {
				manager.profilingEnabled = true;
			}
		} else {
			Timings.enabled = true;
		}
		Runnable profilingRunnable = new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(1000 * time);
				} catch (InterruptedException ignored) {
				}
				if (entity) {
					manager.profilingEnabled = false;
					if (location) {
						try {
							manager.getEntityCallable(hashCode).profilingEnabled = false;
							manager.getTileEntityCallable(hashCode).profilingEnabled = false;
						} catch (NullPointerException ignored) {
						}
					}
				} else {
					Timings.enabled = false;
				}
				try {
					Thread.sleep(100 * time);
				} catch (InterruptedException ignored) {
				}
				if (entity) {
					sendChat(commandSender, String.valueOf(manager.entityTickProfiler.writeData(new TableFormatter(commandSender))));
					manager.entityTickProfiler.clear();
				} else {
					sendChat(commandSender, String.valueOf(Timings.writeData(new TableFormatter(commandSender))));
					Timings.clear();
				}
			}
		};
		Thread profilingThread = new Thread(profilingRunnable);
		profilingThread.setName("TT Profiler");
		sendChat(commandSender, "Profiling for " + time + " seconds in " + Log.name(world));
		profilingThread.start();
	}
}

@@ -1016,18 +1016,17 @@
 time_ = 
-10
+7
 ;%0D%0A%09%09boo
@@ -1698,18 +1698,17 @@
 %5D %5Btime=
-10
+7
 %5D %5Bdimen
@@ -2049,24 +2049,176 @@
 (entity) %7B%0D%0A
+%09%09%09if (manager.profilingEnabled) %7B%0D%0A%09%09%09%09sendChat(commandSender, %22Someone else is currently profiling, please wait and try again.%22);%0D%0A%09%09%09%09return;%0D%0A%09%09%09%7D%0D%0A
 %09%09%09if (locat
@@ -2480,24 +2480,24 @@
 rue;%0D%0A%09%09%09%7D%0D%0A
-
 %09%09%7D else %7B%0D%0A
@@ -2496,16 +2496,159 @@
 else %7B%0D%0A
+%09%09%09if (Timings.enabled) %7B%0D%0A%09%09%09%09sendChat(commandSender, %22Someone else is currently profiling, please wait and try again.%22);%0D%0A%09%09%09%09return;%0D%0A%09%09%09%7D%0D%0A
 %09%09%09Timin

Change default profiling time to 7 seconds, don't allow multiple people to profile at the same time. It didn't work anyway