/*
 * Caveworld
 *
 * Copyright (c) 2014 kegare
 * https://github.com/kegare
 *
 * This mod is distributed under the terms of the Minecraft Mod Public License 1.0, or MMPL.
 * Please check the contents of the license located in http://www.mod-buildcraft.com/MMPL-1.0.txt
 */

package com.kegare.caveworld.core;

import java.awt.Desktop;
import java.net.URI;
import java.util.List;
import java.util.Locale;

import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandNotFoundException;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatMessageComponent;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.MathHelper;
import net.minecraft.util.StringUtils;
import net.minecraft.world.ChunkPosition;
import net.minecraft.world.WorldServer;

import com.google.common.collect.Lists;
import com.kegare.caveworld.util.Version;

import cpw.mods.fml.common.Loader;

public class CommandCaveworld implements ICommand
{
	@Override
	public int compareTo(Object obj)
	{
		return getCommandName().compareTo(((ICommand)obj).getCommandName());
	}

	@Override
	public String getCommandName()
	{
		return "caveworld";
	}

	@Override
	public String getCommandUsage(ICommandSender sender)
	{
		throw new CommandNotFoundException();
	}

	@Override
	public List getCommandAliases()
	{
		return null;
	}

	@Override
	public void processCommand(ICommandSender sender, String[] args)
	{
		if (args.length <= 0 || args[0].equalsIgnoreCase("version"))
		{
			StringBuilder builder = new StringBuilder();

			builder.append(EnumChatFormatting.AQUA).append(" Caveworld ").append(EnumChatFormatting.RESET).append(Version.getCurrent());

			if (Version.DEV_DEBUG)
			{
				builder.append(EnumChatFormatting.RED).append(" dev").append(EnumChatFormatting.RESET);
			}
			else if (Config.hardcoreEnabled)
			{
				builder.append(EnumChatFormatting.RED).append(" hardcore").append(EnumChatFormatting.RESET);
			}

			builder.append(" for ").append(Loader.instance().getMCVersionString());
			builder.append(EnumChatFormatting.GRAY).append(" (Latest: ").append(Version.getLatest()).append(")");

			sender.sendChatToPlayer(ChatMessageComponent.createFromText(sender instanceof EntityPlayerMP ? builder.toString() : StringUtils.stripControlCodes(builder.toString())));
			sender.sendChatToPlayer(ChatMessageComponent.createFromText("  " + Caveworld.metadata.description));
			sender.sendChatToPlayer(ChatMessageComponent.createFromText("  " + Caveworld.metadata.url).setColor(EnumChatFormatting.DARK_GRAY));
		}
		else if (args[0].equalsIgnoreCase("forum") || args[0].equalsIgnoreCase("url"))
		{
			try
			{
				Desktop.getDesktop().browse(new URI(Caveworld.metadata.url));
			}
			catch (Exception ignored) {}
		}
		else if (Version.DEV_DEBUG && sender instanceof EntityPlayerMP && ((EntityPlayerMP)sender).capabilities.isCreativeMode)
		{
			EntityPlayerMP player = (EntityPlayerMP)sender;

			if (args[0].equalsIgnoreCase("mineshaft"))
			{
				WorldServer world = player.getServerForPlayer();
				int x = MathHelper.floor_double(player.posX);
				int y = MathHelper.floor_double(player.posY);
				int z = MathHelper.floor_double(player.posZ);
				ChunkPosition pos = world.getChunkProvider().findClosestStructure(world, "Mineshaft", x, y, z);

				if (pos != null) player.playerNetServerHandler.setPlayerLocation(pos.x, pos.y + 1.5D, pos.z, player.rotationYaw, player.rotationPitch);
			}
			else if (args[0].equalsIgnoreCase("stronghold"))
			{
				WorldServer world = player.getServerForPlayer();
				int x = MathHelper.floor_double(player.posX);
				int y = MathHelper.floor_double(player.posY);
				int z = MathHelper.floor_double(player.posZ);
				ChunkPosition pos = world.getChunkProvider().findClosestStructure(world, "Stronghold", x, y, z);

				if (pos != null) player.playerNetServerHandler.setPlayerLocation(pos.x, pos.y + 1.5D, pos.z, player.rotationYaw, player.rotationPitch);
			}
			else if (args[0].equalsIgnoreCase("mining") && args.length > 2)
			{
				String str = args[2];
				boolean flag = str.toUpperCase(Locale.ENGLISH).endsWith("L");

				if (flag)
				{
					str = str.substring(0, str.length() - 1);
				}

				int value = 0;

				if (args[1].equalsIgnoreCase("add"))
				{
					value = CommandBase.parseIntBounded(sender, str, Short.MIN_VALUE, Short.MAX_VALUE);
				}
				else if (args[1].equalsIgnoreCase("remove") || args[1].equalsIgnoreCase("reduce"))
				{
					value = - CommandBase.parseIntBounded(sender, str, Short.MIN_VALUE, Short.MAX_VALUE);
				}

				if (value != 0)
				{
					CaveMiningPlayer data = CaveMiningPlayer.get(player);

					if (flag)
					{
						data.addMiningLevel(value);
					}
					else
					{
						data.addMiningCount(value);
					}
				}
			}
		}
	}

	@Override
	public boolean canCommandSenderUseCommand(ICommandSender sender)
	{
		return sender instanceof MinecraftServer || sender instanceof EntityPlayerMP;
	}

	@Override
	public List addTabCompletionOptions(ICommandSender sender, String[] args)
	{
		return Lists.newArrayList("version", "forum");
	}

	@Override
	public boolean isUsernameIndex(String[] args, int index)
	{
		return false;
	}
}