content: /**
   * This file is part of Aion-Lightning <aion-lightning.org>.
   *
   *  Aion-Lightning 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.
   *
   *  Aion-Lightning 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 Aion-Lightning.
   *  If not, see <http://www.gnu.org/licenses/>.
   */
  package quest.pandaemonium;
  
  import com.aionemu.gameserver.model.DialogAction;
  import com.aionemu.gameserver.model.Race;
  import com.aionemu.gameserver.model.gameobjects.Item;
  import com.aionemu.gameserver.model.gameobjects.player.Player;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_ITEM_USAGE_ANIMATION;
  import com.aionemu.gameserver.questEngine.handlers.HandlerResult;
  import com.aionemu.gameserver.questEngine.handlers.QuestHandler;
  import com.aionemu.gameserver.questEngine.model.QuestEnv;
  import com.aionemu.gameserver.questEngine.model.QuestState;
  import com.aionemu.gameserver.questEngine.model.QuestStatus;
  import com.aionemu.gameserver.services.QuestService;
  import com.aionemu.gameserver.utils.PacketSendUtility;
  import com.aionemu.gameserver.utils.ThreadPoolManager;
  
  /**
   * @author FrozenKiller
   */
  public class _23831SupportfromPandaemonium2 extends QuestHandler {
  
  	private final static int questId = 23831;
  
  	public _23831SupportfromPandaemonium2() {
  		super(questId);
  	}
  
  	@Override
  	public void register() {
  		qe.registerQuestItem(182216124, questId);
  		qe.registerOnLevelUp(questId);
  		qe.registerQuestNpc(204061).addOnTalkEvent(questId);
  		qe.registerOnEnterWorld(questId);
  	}
  
  	@Override
  	public boolean onDialogEvent(QuestEnv env) {
  		Player player = env.getPlayer();
  		QuestState qs = player.getQuestStateList().getQuestState(questId);
  		DialogAction dialog = env.getDialog();
  		int targetId = env.getTargetId();
  		if (qs == null) {
  			return false;
  		}
  
  		if (qs.getStatus() == QuestStatus.REWARD) {
  			if (targetId == 204061) { // Aud
  				switch (dialog) {
  					case USE_OBJECT: {
  						return sendQuestDialog(env, 10002);
  					}
  					case SELECT_QUEST_REWARD: {
  						return sendQuestDialog(env, 5);
  					}
  					default:
  						return sendQuestEndDialog(env);
  				}
  			}
  		}
  		return false;
  	}
  
  	@Override
  	public HandlerResult onItemUseEvent(final QuestEnv env, final Item item) {
  		final Player player = env.getPlayer();
  		final QuestState qs = player.getQuestStateList().getQuestState(questId);
  		if (qs != null && qs.getStatus() == QuestStatus.START) {
  			PacketSendUtility.broadcastPacket(player, new SM_ITEM_USAGE_ANIMATION(player.getObjectId(), 0, item.getObjectId(), item.getItemId(), 2000, 0), true);
  			ThreadPoolManager.getInstance().schedule(new Runnable() {
  
  				@Override
  				public void run() {
  					removeQuestItem(env, item.getItemId(), 1);
  					PacketSendUtility.broadcastPacket(player, new SM_ITEM_USAGE_ANIMATION(player.getObjectId(), 0, item.getObjectId(), item.getItemId(), 0, 1), true);
  					qs.setQuestVar(1);
  					qs.setStatus(QuestStatus.REWARD);
  					updateQuestStatus(env);
  				}
  			}, 2000);
  		}
  		return HandlerResult.FAILED;
  	}
  
  	@Override
  	public boolean onLvlUpEvent(QuestEnv env) {
  		Player player = env.getPlayer();
  		QuestState qs = player.getQuestStateList().getQuestState(questId);
  		if (player.getLevel() >= 40 && (qs == null || qs.getStatus() == QuestStatus.NONE) && player.getRace() == Race.ASMODIANS) {
  			giveQuestItem(env, 182216124, 1);
  			return QuestService.startQuest(env);
  		}
  		return false;
  	}
  
  	@Override
  	public boolean onEnterWorldEvent(QuestEnv env) { // Fix for player who already have this Quest
  		Player player = env.getPlayer();
  		QuestState qs = player.getQuestStateList().getQuestState(questId);
  		if (qs != null && qs.getStatus() == QuestStatus.START && player.getInventory().getItemCountByItemId(182216124) < 1 && player.getWorldId() == 120010000) {
  			return giveQuestItem(env, 182216124, 1);
  		}
  		return false;
  	}
  }
  