/*
 * Copyright © 2004-2025 L2J DataPack
 * 
 * This file is part of L2J DataPack.
 * 
 * L2J DataPack 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.
 * 
 * L2J DataPack 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 com.l2jserver.datapack.handlers.admincommandhandlers;

import static com.l2jserver.gameserver.config.Configuration.customs;

import java.util.StringTokenizer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.l2jserver.gameserver.data.xml.impl.AdminData;
import com.l2jserver.gameserver.handler.AdminCommandHandler;
import com.l2jserver.gameserver.handler.IAdminCommandHandler;
import com.l2jserver.gameserver.model.L2Clan;
import com.l2jserver.gameserver.model.L2Object;
import com.l2jserver.gameserver.model.L2World;
import com.l2jserver.gameserver.model.Location;
import com.l2jserver.gameserver.model.actor.L2Character;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.network.SystemMessageId;

/**
 * This class handles following admin commands: - handles every admin menu command
 * @version $Revision: 1.3.2.6.2.4 $ $Date: 2005/04/11 10:06:06 $
 */
public class AdminMenu implements IAdminCommandHandler {
	private static final Logger LOG = LoggerFactory.getLogger(AdminMenu.class);
	
	private static final String[] ADMIN_COMMANDS = {
		"admin_char_manage",
		"admin_teleport_character_to_menu",
		"admin_recall_char_menu",
		"admin_recall_party_menu",
		"admin_recall_clan_menu",
		"admin_goto_char_menu",
		"admin_kick_menu",
		"admin_kill_menu",
		"admin_ban_menu",
		"admin_unban_menu"
	};
	
	@Override
	public boolean useAdminCommand(String command, L2PcInstance activeChar) {
		if (command.equals("admin_char_manage")) {
			showMainPage(activeChar);
		} else if (command.startsWith("admin_teleport_character_to_menu")) {
			String[] data = command.split(" ");
			if (data.length == 5) {
				String playerName = data[1];
				L2PcInstance player = L2World.getInstance().getPlayer(playerName);
				if (player != null) {
					teleportCharacter(player, new Location(Integer.parseInt(data[2]), Integer.parseInt(data[3]), Integer.parseInt(data[4])), activeChar, "Admin is teleporting you.");
				}
			}
			showMainPage(activeChar);
		} else if (command.startsWith("admin_recall_char_menu")) {
			try {
				String targetName = command.substring(23);
				L2PcInstance player = L2World.getInstance().getPlayer(targetName);
				teleportCharacter(player, activeChar.getLocation(), activeChar, "Admin is teleporting you.");
			} catch (StringIndexOutOfBoundsException e) {
			}
		} else if (command.startsWith("admin_recall_party_menu")) {
			try {
				String targetName = command.substring(24);
				L2PcInstance player = L2World.getInstance().getPlayer(targetName);
				if (player == null) {
					activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
					return true;
				}
				if (!player.isInParty()) {
					activeChar.sendMessage("Player is not in party.");
					teleportCharacter(player, activeChar.getLocation(), activeChar, "Admin is teleporting you.");
					return true;
				}
				for (L2PcInstance pm : player.getParty().getMembers()) {
					teleportCharacter(pm, activeChar.getLocation(), activeChar, "Your party is being teleported by an Admin.");
				}
			} catch (Exception e) {
				LOG.warn(e.getMessage(), e);
			}
		} else if (command.startsWith("admin_recall_clan_menu")) {
			try {
				String targetName = command.substring(23);
				L2PcInstance player = L2World.getInstance().getPlayer(targetName);
				if (player == null) {
					activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
					return true;
				}
				L2Clan clan = player.getClan();
				if (clan == null) {
					activeChar.sendMessage("Player is not in a clan.");
					teleportCharacter(player, activeChar.getLocation(), activeChar, "Admin is teleporting you.");
					return true;
				}
				
				for (L2PcInstance member : clan.getOnlineMembers(0)) {
					teleportCharacter(member, activeChar.getLocation(), activeChar, "Your clan is being teleported by an Admin.");
				}
			} catch (Exception e) {
				LOG.warn(e.getMessage(), e);
			}
		} else if (command.startsWith("admin_goto_char_menu")) {
			try {
				String targetName = command.substring(21);
				L2PcInstance player = L2World.getInstance().getPlayer(targetName);
				activeChar.setInstanceId(player.getInstanceId());
				teleportToCharacter(activeChar, player);
			} catch (StringIndexOutOfBoundsException e) {
			}
		} else if (command.equals("admin_kill_menu")) {
			handleKill(activeChar);
		} else if (command.startsWith("admin_kick_menu")) {
			StringTokenizer st = new StringTokenizer(command);
			if (st.countTokens() > 1) {
				st.nextToken();
				String player = st.nextToken();
				L2PcInstance plyr = L2World.getInstance().getPlayer(player);
				String text;
				if (plyr != null) {
					plyr.logout();
					text = "You kicked " + plyr.getName() + " from the game.";
				} else {
					text = "Player " + player + " was not found in the game.";
				}
				activeChar.sendMessage(text);
			}
			showMainPage(activeChar);
		} else if (command.startsWith("admin_ban_menu")) {
			StringTokenizer st = new StringTokenizer(command);
			if (st.countTokens() > 1) {
				String subCommand = "admin_ban_char";
				if (!AdminData.getInstance().hasAccess(subCommand, activeChar.getAccessLevel())) {
					activeChar.sendMessage("You don't have the access right to use this command!");
					LOG.warn("Character {} tried to use admin command {}, but have no access to it!", activeChar.getName(), subCommand);
					return false;
				}
				IAdminCommandHandler ach = AdminCommandHandler.getInstance().getHandler(subCommand);
				ach.useAdminCommand(subCommand + command.substring(14), activeChar);
			}
			showMainPage(activeChar);
		} else if (command.startsWith("admin_unban_menu")) {
			StringTokenizer st = new StringTokenizer(command);
			if (st.countTokens() > 1) {
				String subCommand = "admin_unban_char";
				if (!AdminData.getInstance().hasAccess(subCommand, activeChar.getAccessLevel())) {
					activeChar.sendMessage("You don't have the access right to use this command!");
					LOG.warn("Character {} tried to use admin command {}, but have no access to it!", activeChar.getName(), subCommand);
					return false;
				}
				IAdminCommandHandler ach = AdminCommandHandler.getInstance().getHandler(subCommand);
				ach.useAdminCommand(subCommand + command.substring(16), activeChar);
			}
			showMainPage(activeChar);
		}
		return true;
	}
	
	@Override
	public String[] getAdminCommandList() {
		return ADMIN_COMMANDS;
	}
	
	private void handleKill(L2PcInstance activeChar) {
		handleKill(activeChar, null);
	}
	
	private void handleKill(L2PcInstance activeChar, String player) {
		L2Object obj = activeChar.getTarget();
		L2Character target = (L2Character) obj;
		String filename = "main_menu.htm";
		if (player != null) {
			L2PcInstance plyr = L2World.getInstance().getPlayer(player);
			if (plyr != null) {
				target = plyr;
				activeChar.sendMessage("You killed " + plyr.getName());
			}
		}
		if (target != null) {
			if (target instanceof L2PcInstance) {
				target.reduceCurrentHp(target.getMaxHp() + target.getMaxCp() + 1, activeChar, null);
				filename = "charmanage.htm";
			} else if (customs().championEnable() && target.isChampion()) {
				target.reduceCurrentHp((target.getMaxHp() * customs().getChampionHp()) + 1, activeChar, null);
			} else {
				target.reduceCurrentHp(target.getMaxHp() + 1, activeChar, null);
			}
		} else {
			activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
		}
		AdminHtml.showAdminHtml(activeChar, filename);
	}
	
	private void teleportCharacter(L2PcInstance player, Location loc, L2PcInstance activeChar, String message) {
		if (player != null) {
			player.sendMessage(message);
			player.teleToLocation(loc, true);
		}
		showMainPage(activeChar);
	}
	
	private void teleportToCharacter(L2PcInstance activeChar, L2Object target) {
		L2PcInstance player = null;
		if (target instanceof L2PcInstance) {
			player = (L2PcInstance) target;
		} else {
			activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
			return;
		}
		if (player.getObjectId() == activeChar.getObjectId()) {
			player.sendPacket(SystemMessageId.CANNOT_USE_ON_YOURSELF);
		} else {
			activeChar.setInstanceId(player.getInstanceId());
			activeChar.teleToLocation(player.getLocation(), true);
			activeChar.sendMessage("You're teleporting yourself to character " + player.getName());
		}
		showMainPage(activeChar);
	}
	
	private void showMainPage(L2PcInstance activeChar) {
		AdminHtml.showAdminHtml(activeChar, "charmanage.htm");
	}
}
