package com.l2dot.gameserver.handler.voicedcommandhandlers;

import com.l2dot.Config;
import com.l2dot.gameserver.cache.HtmCache;
import com.l2dot.gameserver.handler.IVoicedCommandHandler;
import com.l2dot.gameserver.model.actor.instance.L2PcInstance;
import com.l2dot.gameserver.model.entity.TvTEvent;
import com.l2dot.gameserver.network.serverpackets.NpcHtmlMessage;

public class tvtcmds implements IVoicedCommandHandler
{
	private static final String[] VOICED_COMMANDS = { "tvtjoin", "join", "tvtleave", "leave", "tvtinfo" };

	public boolean useVoicedCommand(String command, L2PcInstance playerInstance, String target)
	{
		if ((command.startsWith("tvtjoin")) || (command.startsWith("join")))
		{
			JoinTvT(playerInstance);
		}
		else if ((command.startsWith("tvtleave")) || (command.startsWith("leave")))
		{
			LeaveTvT(playerInstance);
		}
		else if (command.startsWith("tvtinfo"))
		{
			ShowTvTInfoPage(playerInstance);
		}
		return true;
	}

	public String[] getVoicedCommandList()
	{
		return VOICED_COMMANDS;
	}

	public boolean JoinTvT (L2PcInstance playerInstance)
	{
		if (playerInstance == null || !TvTEvent.isParticipating())
		{
			return false;
		}

		if (playerInstance.isInJail())
		{
			playerInstance.sendMessage("Oh hell no.");
		}
		else if (playerInstance.isInOlympiadMode())
		{
			playerInstance.sendMessage("You can not register for an event while participating in the Olympiad.");
		}
		else if (playerInstance.getLevel() < Config.TVT_EVENT_MIN_LVL)
		{
			playerInstance.sendMessage("You do not meet the level requirements to join this event.");
		}
		else if (TvTEvent.isStarted())
		{
			playerInstance.sendMessage("The event has already started.");
		}
		else if (TvTEvent.isInactive())
		{
			playerInstance.sendMessage("You cannot register for a nonexistant event.");
		}
/*
		else if (TvTEvent.isParticipating())
		{
			playerInstance.sendMessage("You're already registered for this event.");
		}
*/
		else if (TvTEvent.addParticipant(playerInstance))
		{
			playerInstance.sendMessage("You're now registered for the TvT event.");
		}
		return true;
	}
	public boolean LeaveTvT (L2PcInstance playerInstance)
	{
		if (playerInstance == null || !TvTEvent.isParticipating())
		{
			return false;
		}

		if (TvTEvent.isStarted())
		{
			playerInstance.sendMessage("You cannot leave during an event.");
		}
		else if (TvTEvent.isInactive())
		{
			playerInstance.sendMessage("You cannot leave from a nonexistant event.");
		}
		
		return true;
	}
	public boolean ShowTvTInfoPage (L2PcInstance playerInstance)
	{
		if (playerInstance == null || !TvTEvent.isParticipating())
		{
			return false;
		}

		if (TvTEvent.isStarted())
		{
			String voiceTvTInfo = HtmCache.getInstance().getHtm("data/html/mods/TvTEventStatus.htm");
			if (voiceTvTInfo == null)
			{
				voiceTvTInfo = "<html><body><br><br><center><font color=LEVEL>404:</font> File Not Found</center></body></html>";
			}
			playerInstance.sendPacket(new NpcHtmlMessage(1, voiceTvTInfo));
		}
		else if (TvTEvent.isInactive())
		{
			playerInstance.sendMessage("You cannot get info for a nonexistant event.");
		}
		return true;
	}
}
