/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package handlers.admincommandhandlers;

import java.util.StringTokenizer;
import java.util.logging.Logger;

import javolution.lang.MathLib;
import javolution.text.TextBuilder;

import com.l2jserver.Config;
import com.l2jserver.gameserver.GmListTable;
import com.l2jserver.gameserver.cache.HtmCache;
import com.l2jserver.gameserver.datatables.AccessLevels;
import com.l2jserver.gameserver.datatables.AdminCommandAccessRights;
import com.l2jserver.gameserver.datatables.ItemTable;
import com.l2jserver.gameserver.datatables.NpcTable;
import com.l2jserver.gameserver.datatables.NpcWalkerRoutesTable;
import com.l2jserver.gameserver.datatables.SkillTable;
import com.l2jserver.gameserver.datatables.TeleportLocationTable;
import com.l2jserver.gameserver.handler.IAdminCommandHandler;
import com.l2jserver.gameserver.idfactory.IdFactory;
import com.l2jserver.gameserver.instancemanager.Manager;
import com.l2jserver.gameserver.instancemanager.QuestManager;
import com.l2jserver.gameserver.model.L2ItemInstance;
import com.l2jserver.gameserver.model.L2Multisell;
import com.l2jserver.gameserver.model.L2Object;
import com.l2jserver.gameserver.model.L2World;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.actor.instance.L2SummonInstance;
import com.l2jserver.gameserver.model.base.Experience;
import com.l2jserver.gameserver.model.olympiad.Olympiad;
import com.l2jserver.gameserver.network.SystemMessageId;
import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
import com.l2jserver.gameserver.network.serverpackets.PetInfo;
import com.l2jserver.gameserver.network.serverpackets.SocialAction;
import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;


/**
 * This class handles following admin commands:
 * - admin|admin1/admin2/admin3/admin4/admin5 = slots for the 5 starting admin menus
 * - gmliston/gmlistoff = includes/excludes active character from /gmlist results
 * - silence = toggles private messages acceptance mode
 * - diet = toggles weight penalty mode
 * - tradeoff = toggles trade acceptance mode
 * - reload = reloads specified component from multisell|skill|npc|htm|item|instancemanager
 * - set/set_menu/set_mod = alters specified server setting
 * - saveolymp = saves olympiad state manually
 * - manualhero = cycles olympiad and calculate new heroes.
 * @version $Revision: 1.3.2.1.2.4 $ $Date: 2007/07/28 10:06:06 $
 */
public class AdminAdmin implements IAdminCommandHandler
{
	private static Logger _log = Logger.getLogger(AdminAdmin.class.getName());
	
	private static final String[] ADMIN_COMMANDS =
	{
		"admin_camera",			//[L2J_JP_ADD]
		"admin_summon",			//[L2J_JP_ADD]
		"admin_unsummon",		//[L2J_JP_ADD]
	//	"admin_bvb",			//[JOJO]
		"admin_item_circle",	//[JOJO]
		"admin_admin",
		"admin_admin1",
		"admin_admin2",
		"admin_admin3",
		"admin_admin4",
		"admin_admin5",
		"admin_admin6",
		"admin_admin7",
		"admin_admin8",
		"admin_gmliston",
		"admin_gmlistoff",
		"admin_silence",
		"admin_diet",
		"admin_tradeoff",
		"admin_reload",
		"admin_set",
		"admin_set_mod",
		"admin_saveolymp",
		"admin_manualhero",
		"admin_sethero",
		"admin_endolympiad",
		"admin_setconfig",
		"admin_config_server"
	};
	
	public boolean useAdminCommand(String command, L2PcInstance activeChar)
	{
		
		if (command.startsWith("admin_admin"))
		{
			showMainPage(activeChar, command);
		}
		else if (command.equals("admin_config_server"))
		{
			showConfigPage(activeChar);
		}
		else if (command.startsWith("admin_gmliston"))
		{
			GmListTable.getInstance().showGm(activeChar);
			activeChar.sendMessage("Registered into gm list");
			AdminHelpPage.showHelpPage(activeChar,"gm_menu.htm");
		}
		else if (command.startsWith("admin_gmlistoff"))
		{
			GmListTable.getInstance().hideGm(activeChar);
			activeChar.sendMessage("Removed from gm list");
			AdminHelpPage.showHelpPage(activeChar,"gm_menu.htm");
		}
		else if (command.startsWith("admin_silence"))
		{
			if (activeChar.isSilenceMode()) // already in message refusal mode
			{
				activeChar.setSilenceMode(false);
				activeChar.sendPacket(new SystemMessage(SystemMessageId.MESSAGE_ACCEPTANCE_MODE));
			}
			else
			{
				activeChar.setSilenceMode(true);
				activeChar.sendPacket(new SystemMessage(SystemMessageId.MESSAGE_REFUSAL_MODE));
			}
			AdminHelpPage.showHelpPage(activeChar,"gm_menu.htm");
		}
		else if (command.startsWith("admin_saveolymp"))
		{
			Olympiad.getInstance().saveOlympiadStatus();
			activeChar.sendMessage("olympiad system saved.");
		}
		else if (command.startsWith("admin_endolympiad"))
		{
			try
			{
				Olympiad.getInstance().manualSelectHeroes();
			}
			catch (Exception e)
			{
				_log.warning("An error occured while ending olympiad: " + e);
			}
			activeChar.sendMessage("Heroes formed");
		}
		else if (command.startsWith("admin_manualhero") || command.startsWith("admin_sethero"))
		{
			L2PcInstance target = null;
			
			if (activeChar.getTarget() instanceof L2PcInstance)
			{
				target = (L2PcInstance) activeChar.getTarget();
				target.setHero(target.isHero() ? false : true);
			}
			else
			{
				target = activeChar;
				target.setHero(target.isHero() ? false : true);
			}
			target.broadcastUserInfo();
		}
		else if (command.startsWith("admin_diet"))
		{
			try
			{
				StringTokenizer st = new StringTokenizer(command);
				st.nextToken();
				if (st.nextToken().equalsIgnoreCase("on"))
				{
					activeChar.setDietMode(true);
					activeChar.sendMessage("Diet mode on");
				}
				else if (st.nextToken().equalsIgnoreCase("off"))
				{
					activeChar.setDietMode(false);
					activeChar.sendMessage("Diet mode off");
				}
			}
			catch (Exception ex)
			{
				if (activeChar.getDietMode())
				{
					activeChar.setDietMode(false);
					activeChar.sendMessage("Diet mode off");
				}
				else
				{
					activeChar.setDietMode(true);
					activeChar.sendMessage("Diet mode on");
				}
			}
			finally
			{
				activeChar.refreshOverloaded();
			}
			AdminHelpPage.showHelpPage(activeChar,"gm_menu.htm");
		}
		else if (command.startsWith("admin_tradeoff"))
		{
			try
			{
				String mode = command.substring(15);
				if (mode.equalsIgnoreCase("on"))
				{
					activeChar.setTradeRefusal(true);
					activeChar.sendMessage("Trade refusal enabled");
				}
				else if (mode.equalsIgnoreCase("off"))
				{
					activeChar.setTradeRefusal(false);
					activeChar.sendMessage("Trade refusal disabled");
				}
			}
			catch (Exception ex)
			{
				if (activeChar.getTradeRefusal())
				{
					activeChar.setTradeRefusal(false);
					activeChar.sendMessage("Trade refusal disabled");
				}
				else
				{
					activeChar.setTradeRefusal(true);
					activeChar.sendMessage("Trade refusal enabled");
				}
			}
			AdminHelpPage.showHelpPage(activeChar,"gm_menu.htm");
		}
		else if (command.startsWith("admin_reload"))
		{
			StringTokenizer st = new StringTokenizer(command);
			st.nextToken();
			String type = "";
			try
			{
				type = st.nextToken();
				if (type.equals("multisell"))
				{
					L2Multisell.getInstance().reload();
					activeChar.sendMessage("All Multisells have been reloaded");
				}
				else if (type.startsWith("teleport"))
				{
					TeleportLocationTable.getInstance().reloadAll();
					activeChar.sendMessage("Teleport Locations have been reloaded");
				}
				else if (type.startsWith("skill"))
				{
					SkillTable.getInstance().reload();
					activeChar.sendMessage("All Skills have been reloaded");
				}
				else if (type.equals("npc"))
				{
					NpcTable.getInstance().reloadAllNpc();
					QuestManager.getInstance().reloadAllQuests();
					activeChar.sendMessage("All NPCs have been reloaded");
				}
				else if (type.startsWith("htm"))
				{
					HtmCache.getInstance().reload();
					activeChar.sendMessage("Cache[HTML]: " + HtmCache.getInstance().getMemoryUsage() + " megabytes on " + HtmCache.getInstance().getLoadedFiles() + " files loaded");
				}
				else if (type.startsWith("item"))
				{
					ItemTable.getInstance().reload();
					activeChar.sendMessage("Item Templates have been reloaded");
				}
				else if (type.startsWith("config"))
				{
					Config.load();
					activeChar.sendMessage("All Config Settings have been reloaded");
				}
				else if (type.startsWith("instancemanager"))
				{
					Manager.reloadAll();
					activeChar.sendMessage("All Instance Manager has been reloaded");
				}
				else if (type.startsWith("npcwalkers"))
				{
					NpcWalkerRoutesTable.getInstance().load();
					activeChar.sendMessage("NPC Walker Routes have been reloaded");
				}
				else if (type.startsWith("access"))
				{
					AccessLevels.getInstance().reloadAccessLevels();
					AdminCommandAccessRights.getInstance().reloadAdminCommandAccessRights();
					activeChar.sendMessage("Access Rights have been reloaded");
				}
//-				else if (type.startsWith("config"))	// [L2J_JP ADD]
//-				{
//-					Config.load();
//-					activeChar.sendMessage("All GameServer Config have been reloaded");
//-				}
				else if (type.startsWith("quests"))
				{
					QuestManager.getInstance().reloadAllQuests();
					activeChar.sendMessage("All Quests have been reloaded");
				}
				
				activeChar.sendMessage("WARNING: There are several known issues regarding this feature. Reloading server data during runtime is STRONGLY NOT RECOMMENDED for live servers, just for developing environments.");
				
			}
			catch (Exception e)
			{
				activeChar.sendMessage("An error occured while reloading " + type + " !");
				activeChar.sendMessage("Usage: //reload <multisell|teleport|skill|npc|htm|item|config|instancemanager|npcwalkers|access|quests>");
				_log.warning("An error occured while reloading " + type + ": " + e); //do not mask an exception here
			}
		}
		else if (command.startsWith("admin_setconfig"))
		{
			StringTokenizer st = new StringTokenizer(command);
			st.nextToken();
			try
			{
				String pName = st.nextToken();
				String pValue = st.nextToken();
				if (Config.setParameterValue(pName, pValue))
					activeChar.sendMessage("Config parameter " + pName + " set to " + pValue);
				else
					activeChar.sendMessage("Invalid parameter!");
			}
			catch (Exception e)
			{
				activeChar.sendMessage("Usage: //setconfig <parameter> <value>");
			}
			finally
			{
				showConfigPage(activeChar);
			}
		}
		else if (command.startsWith("admin_set"))
		{
			StringTokenizer st = new StringTokenizer(command);
			String[] cmd = st.nextToken().split("_");
			try
			{
				String[] parameter = st.nextToken().split("=");
				String pName = parameter[0].trim();
				String pValue = parameter[1].trim();
				if (Config.setParameterValue(pName, pValue))
					activeChar.sendMessage("parameter " + pName + " succesfully set to " + pValue);
				else
					activeChar.sendMessage("Invalid parameter!");
			}
			catch (Exception e)
			{
				if (cmd.length == 2)
					activeChar.sendMessage("Usage: //set parameter=value");
			}
			finally
			{
				if (cmd.length == 3)
				{
					if (cmd[2].equalsIgnoreCase("mod"))
						AdminHelpPage.showHelpPage(activeChar, "mods_menu.htm");
				}
			}
		}
		
		// [L2J_JP_ADD SANDMAN] GM用カメラ移動テスト
		else if (command.startsWith("admin_camera"))
		{
			if (activeChar.getTarget() == null)
			{
				activeChar.sendMessage("Target incorrect.");
				activeChar.sendMessage("Usage:  //camera dist yaw pitch time duration [social]");
			}
			else
			{
				StringTokenizer st = new StringTokenizer(command);
				st.nextToken();
				
				L2Object target = null;	//[JOJO]
				try
				{
					/*L2Object */target = activeChar.getTarget();
					int scDist = Integer.parseInt(st.nextToken());
					int scYaw = Integer.parseInt(st.nextToken());
					int scPitch = Integer.parseInt(st.nextToken());
					int scTime = Integer.parseInt(st.nextToken());
					int scDuration = Integer.parseInt(st.nextToken());
					int social = 0; if (st.hasMoreTokens()) social = Integer.parseInt(st.nextToken());	//[JOJO]
					activeChar.sendMessage("camera " + scDist + " " + scYaw + " " + scPitch + " " + scTime + " " + scDuration + " " + social);
					activeChar.enterMovieMode();
					activeChar.specialCamera(target, scDist, scYaw, scPitch, scTime, scDuration);
					if (social > 0)
						activeChar.sendPacket(new SocialAction(target.getObjectId(), social));	//[JOJO]
				}
				catch (Exception e)
				{
					activeChar.sendMessage("Usage:  //camera dist yaw pitch time duration [social]");
				}
				finally
				{
					activeChar.leaveMovieMode();
					activeChar.setTarget(target);	//[JOJO]
				}
				
			}
			
		}
		
		// [L2J_JP_ADD] GM用サモン
		else if (command.startsWith("admin_summon"))
		{
			StringTokenizer st = new StringTokenizer(command);
			st.nextToken();
			try
			{
				// NPCIDの切り出し
				int npcId = Integer.parseInt(st.nextToken());
				if (npcId != 0)
					adminSummon(activeChar, npcId);
			}
			catch (Exception e)
			{
				// NPCIDが入力されていなかった場合の使い方表示
				activeChar.sendMessage("Usage:  //summon npcid");
			}
		}
		// [L2J_JP_ADD] GM用サモン(解除)
		else if (command.startsWith("admin_unsummon"))
		{
			// サモンが居れば、解除する
			if (activeChar.getPet() != null)
				activeChar.getPet().unSummon(activeChar);
		}
	//	// [L2J_JP ADD] Boss vs Boss
	//	else if (command.startsWith("admin_bvb"))
	//	{
	//		StringTokenizer st = new StringTokenizer(command);
	//		st.nextToken();
	//		try
	//		{
	//			String cmd = st.nextToken();
	//			if (cmd.equalsIgnoreCase("start"))
	//			{
	//				BvBManager.getInstance().start();
	//			}
	//			else if (cmd.equalsIgnoreCase("end"))
	//			{
	//				BvBManager.getInstance().end();
	//			}
	//			else
	//			{
	//				throw new java.util.NoSuchElementException();
	//			}
	//		}
	//		catch (java.util.NoSuchElementException e)
	//		{
	//			activeChar.sendMessage("Usage://bvb start or //bvb end");
	//		}
	//	}
		else if (command.startsWith("admin_item_circle"))	//[JOJO] お遊び：アデナで地面に○を描く
		{
			StringTokenizer st = new StringTokenizer(command);
			st.nextToken();
			try {
				int x0 = activeChar.getX();
				int y0 = activeChar.getY();
				int z0 = activeChar.getZ();
				double radius = Integer.parseInt(st.nextToken());
				double count = 0;
				int itemId = 57;
				if (st.hasMoreTokens()) count = Integer.parseInt(st.nextToken());
				if (st.hasMoreTokens()) itemId = Integer.parseInt(st.nextToken());
				int limit = (int)Math.round(MathLib.TWO_PI / 10 * radius);
				if (limit < 8) limit = 8;
				if (limit > 300) limit = 300;
				if (count == 0)
					count = limit;
				else if (count > limit)
				{
					count = limit;
					activeChar.sendMessage("Warning: too many count");
				}
				
				double alpha = MathLib.TWO_PI / count;
				while (--count >= 0)
				{
					double angle = alpha * count;
					int x = x0 + (int) Math.round(Math.cos(angle) * radius);
					int y = y0 + (int) Math.round(Math.sin(angle) * radius);
					int z = z0;
					L2ItemInstance item = new L2ItemInstance(IdFactory.getInstance().getNextId(), itemId);
					item.setOwnerId(activeChar.getObjectId());
					item.new doItemDropTask(item, activeChar, x, y, z).run();
					item.setProtected(false);
					L2World.getInstance().storeObject(item);
				}
			}
			catch (Exception e)
			{
				activeChar.sendMessage("Usage://item_circle radius [count] [item]");
			}
		}

		return true;
	}
	
	public String[] getAdminCommandList()
	{
		return ADMIN_COMMANDS;
	}
	
	private void showMainPage(L2PcInstance activeChar, String command)
	{
		int mode = 0;
		String filename = null;
		try
		{
			mode = Integer.parseInt(command.substring(11));
		}
		catch (Exception e)
		{
		}
		switch (mode)
		{
			case 1:
				filename = "main";
				break;
			case 2:
				filename = "game";
				break;
			case 3:
				filename = "effects";
				break;
			case 4:
				filename = "server";
				break;
			case 5:
				filename = "mods";
				break;
			case 6:
				filename = "char";
				break;
			case 7:
				filename = "gm";
				break;
			case 8:
				filename = "old";
				break;
			default:
				if (Config.GM_ADMIN_MENU_STYLE.equals("modern"))
					filename = "main";
				else
					filename = "classic";
				break;
		}
		AdminHelpPage.showHelpPage(activeChar, filename + "_menu.htm");
	}

	// [L2J_JP_ADD] GMサモン実行メソッド
	public void adminSummon(L2PcInstance activeChar, int npcId)
	{
		// 既にペットがいる場合はペットを戻す。
        if (activeChar.getPet() != null)
		{
			SystemMessage sm = new SystemMessage(SystemMessageId.YOU_ALREADY_HAVE_A_PET);
			activeChar.sendPacket(sm);
			activeChar.getPet().unSummon(activeChar);
		}
        
        // ここからはどこか(L2SkillSummon.javaだったかな)の処理を丸写しです。
        L2NpcTemplate summonTemplate = NpcTable.getInstance().getTemplate(npcId);
		L2SummonInstance summon = new L2SummonInstance(IdFactory.getInstance().getNextId(), summonTemplate, activeChar, null);
		
        summon.setTitle(activeChar.getName());
		summon.setExpPenalty(0);
		if (summon.getLevel() >= Experience.LEVEL.length)
		{
			summon.getStat().setExp(Experience.LEVEL[Experience.LEVEL.length - 1]);
		}
		else
		{
			summon.getStat().setExp(Experience.LEVEL[(summon.getLevel() % Experience.LEVEL.length)]);
		}
        summon.getStat().setExp(0);
		summon.setCurrentHp(summon.getMaxHp());
		summon.setCurrentMp(summon.getMaxMp());
		summon.setHeading(activeChar.getHeading());
		summon.setRunning();
		activeChar.setPet(summon);
		
        L2World.getInstance().storeObject(summon);
		summon.spawnMe(activeChar.getX() + 50, activeChar.getY() + 100, activeChar.getZ());
		
		summon.setFollowStatus(true);
		summon.setShowSummonAnimation(false);	// addVisibleObject created the info packets with summon animation
												// if someone comes into range now, the animation shouldnt show any more
		activeChar.sendPacket(new PetInfo(summon, /*TODO:Check!*/0));
    }

	public void showConfigPage(L2PcInstance activeChar)
	{		
		NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
		TextBuilder replyMSG = new TextBuilder("<html><title>L2J :: Config</title><body>"
			+ "<center><table width=270><tr><td width=60><button value=\"Main\" action=\"bypass -h admin_admin\" width=60 height=25 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td><td width=150>Config Server Panel</td><td width=60><button value=\"Back\" action=\"bypass -h admin_admin4\" width=60 height=25 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr></table></center><br>"
			+ "<center><table width=260><tr><td width=140></td><td width=40></td><td width=40></td></tr>"
			+ "<tr><td><font color=\"00AA00\">Drop:</font></td><td></td><td></td></tr>"
			+ "<tr><td><font color=\"LEVEL\">Rate EXP</font> = ")
				.append(Config.RATE_XP)
				.append("</td><td><edit var=\"param1\" width=40 height=15></td><td><button value=\"Set\" action=\"bypass -h admin_setconfig RateXp $param1\" width=40 height=25 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>");
		replyMSG.append("<tr><td><font color=\"LEVEL\">Rate SP</font> = ")
				.append(Config.RATE_SP)
				.append("</td><td><edit var=\"param2\" width=40 height=15></td><td><button value=\"Set\" action=\"bypass -h admin_setconfig RateSp $param2\" width=40 height=25 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>");
		replyMSG.append("<tr><td><font color=\"LEVEL\">Rate Drop Spoil</font> = ")
				.append(Config.RATE_DROP_SPOIL)
				.append("</td><td><edit var=\"param4\" width=40 height=15></td><td><button value=\"Set\" action=\"bypass -h admin_setconfig RateDropSpoil $param4\" width=40 height=25 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>");
		replyMSG.append("<tr><td width=140></td><td width=40></td><td width=40></td></tr>"
			+ "<tr><td><font color=\"00AA00\">Enchant:</font></td><td></td><td></td></tr>"
			+ "<tr><td><font color=\"LEVEL\">Enchant Weapon</font> = ")
				.append(Config.ENCHANT_CHANCE_WEAPON)
				.append("</td><td><edit var=\"param5\" width=40 height=15></td><td><button value=\"Set\" action=\"bypass -h admin_setconfig EnchantChanceWeapon $param5\" width=40 height=25 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>");
		replyMSG.append("<tr><td><font color=\"LEVEL\">Enchant Armor</font> = ")
				.append(Config.ENCHANT_CHANCE_ARMOR)
				.append("</td><td><edit var=\"param6\" width=40 height=15></td><td><button value=\"Set\" action=\"bypass -h admin_setconfig EnchantChanceArmor $param6\" width=40 height=25 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>");
		replyMSG.append("<tr><td><font color=\"LEVEL\">Enchant Jewelry</font> = ")
				.append(Config.ENCHANT_CHANCE_JEWELRY)
				.append("</td><td><edit var=\"param7\" width=40 height=15></td><td><button value=\"Set\" action=\"bypass -h admin_setconfig EnchantChanceJewelry $param7\" width=40 height=25 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>");
		replyMSG.append("<tr><td><font color=\"LEVEL\">Enchant Element</font> = ")
				.append(Config.ENCHANT_CHANCE_ELEMENT)
				.append("</td><td><edit var=\"param8\" width=40 height=15></td><td><button value=\"Set\" action=\"bypass -h admin_setconfig EnchantChanceElement $param8\" width=40 height=25 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>");

		replyMSG.append("</table></body></html>");
		adminReply.setHtml(replyMSG.toString());
		activeChar.sendPacket(adminReply);
	}
}
