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.gelkmaros;
  
  import static com.aionemu.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE.STR_MSG_FULL_INVENTORY;
  
  import com.aionemu.gameserver.model.DialogAction;
  import com.aionemu.gameserver.model.gameobjects.Item;
  import com.aionemu.gameserver.model.gameobjects.Npc;
  import com.aionemu.gameserver.model.gameobjects.player.Player;
  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.instance.InstanceService;
  import com.aionemu.gameserver.services.teleport.TeleportService2;
  import com.aionemu.gameserver.utils.PacketSendUtility;
  import com.aionemu.gameserver.world.WorldMapInstance;
  
  /**
   * @author pralinka
   * @rework FrozenKiller
   */
  
  public class _20032AllAboutAbnormalAether extends QuestHandler {
  
  	private final static int questId = 20032;
  	private final static int[] npc_ids = { 799247, 799250, 799325, 799503, 799258, 799239 };
  
  	public _20032AllAboutAbnormalAether() {
  		super(questId);
  	}
  
  	@Override
  	public void register() {
  		qe.registerOnEnterZoneMissionEnd(questId);
  		qe.registerOnLevelUp(questId);
  		qe.registerOnEnterWorld(questId);
  		qe.registerOnDie(questId);
  		qe.registerOnLogOut(questId);
  		qe.registerQuestNpc(215488).addOnKillEvent(questId);
  		for (int npc_id : npc_ids) {
  			qe.registerQuestNpc(npc_id).addOnTalkEvent(questId);
  		}
  		qe.registerQuestItem(182215592, questId);
  		qe.registerQuestItem(182215593, questId);
  	}
  
  	@Override
  	public boolean onZoneMissionEndEvent(QuestEnv env) {
  		return defaultOnZoneMissionEndEvent(env);
  	}
  
  	@Override
  	public boolean onLvlUpEvent(QuestEnv env) {
  		return defaultOnLvlUpEvent(env, 20031, true);
  	}
  
  	@Override
  	public boolean onDialogEvent(final QuestEnv env) {
  		final Player player = env.getPlayer();
  		final QuestState qs = player.getQuestStateList().getQuestState(questId);
  		if (qs == null) {
  			return false;
  		}
  
  		int var = qs.getQuestVarById(0);
  		int targetId = 0;
  		if (env.getVisibleObject() instanceof Npc) {
  			targetId = ((Npc) env.getVisibleObject()).getNpcId();
  		}
  
  		if (qs.getStatus() == QuestStatus.REWARD) {
  			if (targetId == 799247) { // Angrad
  				if (env.getDialog() == DialogAction.QUEST_SELECT) {
  					return sendQuestDialog(env, 10002);
  				}
  				else {
  					return sendQuestEndDialog(env);
  				}
  			}
  			return false;
  		}
  		else if (qs.getStatus() != QuestStatus.START) {
  			return false;
  		}
  		if (targetId == 799247) { // Angrad
  			switch (env.getDialog()) {
  				case QUEST_SELECT: {
  					if (var == 0) {
  						return sendQuestDialog(env, 1011);
  					}
  				}
  				case SETPRO1:
  					return defaultCloseDialog(env, 0, 1);
  				default:
  					break;
  			}
  		}
  		else if (targetId == 799250) { // Eddas
  			switch (env.getDialog()) {
  				case QUEST_SELECT:
  					if (var == 1) {
  						return sendQuestDialog(env, 1352);
  					}
  				case SETPRO2:
  					return defaultCloseDialog(env, 1, 2);
  				default:
  					break;
  			}
  		}
  		else if (targetId == 799325) { // Taloc's Guardian
  			switch (env.getDialog()) {
  				case QUEST_SELECT: {
  					if (var == 2)
  						return sendQuestDialog(env, 1693);
  				}
  				case SETPRO3:
  					if (player.isInGroup2()) {
  						return sendQuestDialog(env, 2034);
  					}
  					else {
  						if (giveQuestItem(env, 182215592, 1) && giveQuestItem(env, 182215593, 1)) {
  							WorldMapInstance newInstance = InstanceService.getNextAvailableInstance(300190000);
  							InstanceService.registerPlayerWithInstance(newInstance, player);
  							TeleportService2.teleportTo(player, 300190000, newInstance.getInstanceId(), 202.26694f, 226.0532f, 1098.236f, (byte) 30);
  							changeQuestStep(env, 2, 3, false);
  							return closeDialogWindow(env);
  						}
  						else {
  							PacketSendUtility.sendPacket(player, STR_MSG_FULL_INVENTORY);
  							return sendQuestSelectionDialog(env);
  						}
  					}
  				case FINISH_DIALOG:
  					return sendQuestSelectionDialog(env);
  				default:
  					break;
  			}
  		}
  		return false;
  	}
  
  	@Override
  	public boolean onKillEvent(QuestEnv env) {
  		Player player = env.getPlayer();
  		QuestState qs = player.getQuestStateList().getQuestState(questId);
  		if (qs == null || qs.getStatus() != QuestStatus.START) {
  			return false;
  		}
  		int targetId = 0;
  		if (env.getVisibleObject() instanceof Npc) {
  			targetId = ((Npc) env.getVisibleObject()).getNpcId();
  		}
  		switch (targetId) {
  			case 215488: // Celestius
  				if (qs.getQuestVarById(0) == 6) {
  					return defaultOnKillEvent(env, 215488, 6, 7);
  				}
  		}
  		return false;
  	}
  
  	@Override
  	public HandlerResult onItemUseEvent(final QuestEnv env, Item item) {
  		Player player = env.getPlayer();
  		QuestState qs = player.getQuestStateList().getQuestState(questId);
  		if (qs != null && qs.getStatus() == QuestStatus.START) {
  			if (player.getWorldId() == 300190000) {
  				int itemId = item.getItemId();
  				int var = qs.getQuestVarById(0);
  				int var1 = qs.getQuestVarById(1);
  				if (itemId == 182215593) { // Taloc Fruit
  					changeQuestStep(env, 4, 5, false); // 5
  					return HandlerResult.SUCCESS;
  				}
  				else if (itemId == 182215592) { // Taloc's Tears
  					if (var == 5) {
  						if (var1 >= 0 && var1 < 19) {
  							changeQuestStep(env, var1, var1 + 1, false, 1);
  							return HandlerResult.SUCCESS;
  						}
  						else if (var1 == 19) {
  							qs.setQuestVar(6);
  							updateQuestStatus(env);
  							return HandlerResult.SUCCESS;
  						}
  					}
  				}
  			}
  		}
  		return HandlerResult.UNKNOWN;
  	}
  
  	@Override
  	public boolean onEnterWorldEvent(QuestEnv env) {
  		Player player = env.getPlayer();
  		QuestState qs = player.getQuestStateList().getQuestState(questId);
  		if (player.getWorldId() != 300190000) {
  			if (qs != null && qs.getStatus() == QuestStatus.START) {
  				int var = qs.getQuestVarById(0);
  				if (var >= 4 && var < 6) {
  					removeQuestItem(env, 182215592, 1);
  					removeQuestItem(env, 182215593, 1);
  					qs.setQuestVar(2);
  					updateQuestStatus(env);
  					return true;
  				}
  				else if (var == 7) {
  					removeQuestItem(env, 182215592, 1);
  					removeQuestItem(env, 182215593, 1);
  					qs.setQuestVar(8);
  					qs.setStatus(QuestStatus.REWARD);
  					updateQuestStatus(env);
  					return true;
  				}
  			}
  		}
  		else if (player.getWorldId() != 220070000) {
  			if (qs != null && qs.getStatus() == QuestStatus.START) {
  				int var = qs.getQuestVarById(0);
  				if (var == 3) {
  					qs.setQuestVar(4);
  					updateQuestStatus(env);
  					return true;
  				}
  			}
  		}
  		return false;
  	}
  
  	@Override
  	public boolean onDieEvent(QuestEnv env) {
  		Player player = env.getPlayer();
  		QuestState qs = player.getQuestStateList().getQuestState(questId);
  		if (qs != null && qs.getStatus() == QuestStatus.START) {
  			int var = qs.getQuestVarById(0);
  			if (var >= 4 && var < 6) {
  				removeQuestItem(env, 182215592, 1);
  				removeQuestItem(env, 182215593, 1);
  				qs.setQuestVar(2);
  				updateQuestStatus(env);
  				return true;
  			}
  		}
  		return false;
  	}
  
  	@Override
  	public boolean onLogOutEvent(QuestEnv env) {
  		Player player = env.getPlayer();
  		QuestState qs = player.getQuestStateList().getQuestState(questId);
  		if (qs != null && qs.getStatus() == QuestStatus.START) {
  			int var = qs.getQuestVarById(0);
  			if (var >= 4 && var < 6) {
  				removeQuestItem(env, 182215592, 1);
  				removeQuestItem(env, 182215593, 1);
  				qs.setQuestVar(2);
  				updateQuestStatus(env);
  				return true;
  			}
  		}
  		return false;
  	}
  }
  