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.bare_truth;
  
  import com.aionemu.gameserver.dataholders.DataManager;
  import com.aionemu.gameserver.model.DialogAction;
  import com.aionemu.gameserver.model.EmotionType;
  import com.aionemu.gameserver.model.gameobjects.Npc;
  import com.aionemu.gameserver.model.gameobjects.player.Player;
  import com.aionemu.gameserver.model.gameobjects.state.CreatureState;
  import com.aionemu.gameserver.network.aion.SystemMessageId;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_EMOTION;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
  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.services.instance.InstanceService;
  import com.aionemu.gameserver.services.teleport.TeleportService2;
  import com.aionemu.gameserver.skillengine.SkillEngine;
  import com.aionemu.gameserver.utils.PacketSendUtility;
  import com.aionemu.gameserver.utils.ThreadPoolManager;
  import com.aionemu.gameserver.world.WorldMapInstance;
  
  /**
   * @author pralinka
   * @rework FrozenKiller
   */
  public class _14030RetrievedMemory extends QuestHandler {
  
  	private final static int questId = 14030;
  	private final static int[] npcs = { 790001, 700551, 205119, 700552, 203700 };
  	private final static int[] mobs = { 214578, 215396, 215397, 215398, 215399, 215400, 205021, 205022, 215400 };
  
  	public _14030RetrievedMemory() {
  		super(questId);
  	}
  
  	@Override
  	public boolean onLvlUpEvent(QuestEnv env) {
  		return defaultOnLvlUpEvent(env);
  	}
  
  	@Override
  	public void register() {
  		qe.registerOnLevelUp(questId);
  		qe.registerOnDie(questId);
  		qe.registerOnEnterWorld(questId);
  		for (int npc_id : npcs) {
  			qe.registerQuestNpc(npc_id).addOnTalkEvent(questId);
  		}
  		for (int mob : mobs) {
  			qe.registerQuestNpc(mob).addOnKillEvent(questId);
  		}
  	}
  
  	@Override
  	public boolean onDialogEvent(QuestEnv env) {
  		Player player = env.getPlayer();
  		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.START) {
  			switch (targetId) {
  				case 203700: {
  					switch (env.getDialog()) {
  						case QUEST_SELECT: {
  							if (var == 0) {
  								return sendQuestDialog(env, 1011);
  							}
  						}
  						case SETPRO1: {
  							return defaultCloseDialog(env, 0, 1); // 1
  						}
  						default:
  							break;
  					}
  				}
  				case 790001: {
  					switch (env.getDialog()) {
  						case QUEST_SELECT: {
  							if (var == 1) {
  								return sendQuestDialog(env, 1352);
  							}
  							else if (var == 3) {
  								return sendQuestDialog(env, 2034);
  							}
  						}
  						case SETPRO2: {
  							return defaultCloseDialog(env, 1, 2); // 2
  						}
  						case SETPRO4: {
  							giveQuestItem(env, 182215387, 1); // Concentrated Marble of Protection
  							return defaultCloseDialog(env, 3, 4); // 4
  						}
  						default:
  							break;
  					}
  				}
  				case 700551: {
  					if (env.getDialog() == DialogAction.USE_OBJECT && var == 4) {
  						WorldMapInstance newInstance = InstanceService.getNextAvailableInstance(310120000);
  						InstanceService.registerPlayerWithInstance(newInstance, player);
  						TeleportService2.teleportTo(player, 310120000, newInstance.getInstanceId(), 52, 174, 229);
  						return true;
  					}
  				}
  				case 205119: {
  					switch (env.getDialog()) {
  						case QUEST_SELECT: {
  							if (var == 4) {
  								return sendQuestDialog(env, 2375);
  							}
  						}
  						case SETPRO5: {
  							if (var == 4) {
  								removeQuestItem(env, 182215387, 1);
  								SkillEngine.getInstance().applyEffectDirectly(281, player, player, 0);
  								player.setState(CreatureState.FLIGHT_TELEPORT);
  								player.unsetState(CreatureState.ACTIVE);
  								player.setFlightTeleportId(1001);
  								PacketSendUtility.sendPacket(player, new SM_EMOTION(player, EmotionType.START_FLYTELEPORT, 1001, 0));
  								final QuestEnv qe = env;
  								ThreadPoolManager.getInstance().schedule(new Runnable() {
  
  									@Override
  									public void run() {
  										changeQuestStep(qe, 4, 5, false); // 5
  									}
  								}, 43000);
  								return true;
  							}
  						}
  						default:
  							break;
  					}
  				}
  				case 700552: {
  					if (env.getDialog() == DialogAction.USE_OBJECT && var == 56) {
  						TeleportService2.teleportTo(player, 110010000, 1322.1934f, 1511.148f, 567.909f);
  						return true;
  					}
  				}
  				default:
  					break;
  			}
  		}
  		else if (qs.getStatus() == QuestStatus.REWARD) {
  			if (targetId == 203700) {
  				return sendQuestEndDialog(env);
  			}
  		}
  		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) {
  			int var = qs.getQuestVarById(0);
  			if (var == 2) {
  				return defaultOnKillEvent(env, 214578, 2, 3); // 3
  			}
  			else if (var >= 5 && var < 55) {
  				int[] npcIds = { 215396, 215397, 215398, 215399, 215400, 205021, 205022 };
  				if (var == 54) {
  					QuestService.addNewSpawn(310120000, player.getInstanceId(), 215400, 299.4378f, 289.15744f, 206.48138f, (byte) 75);
  				}
  				return defaultOnKillEvent(env, npcIds, 5, 55); // 5-55
  			}
  			else if (var == 55) {
  				return defaultOnKillEvent(env, 215400, 55, 56); // 56
  			}
  		}
  		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 < 56) {
  				changeQuestStep(env, var, 4, false); // 3
  				PacketSendUtility.sendPacket(player, new SM_SYSTEM_MESSAGE(SystemMessageId.QUEST_FAILED_$1, DataManager.QUEST_DATA.getQuestById(questId).getName()));
  				return true;
  			}
  		}
  		return false;
  	}
  
  	@Override
  	public boolean onEnterWorldEvent(QuestEnv env) {
  		Player player = env.getPlayer();
  		QuestState qs = player.getQuestStateList().getQuestState(questId);
  		if (player.getWorldId() != 310120000) {
  			if (qs != null && qs.getStatus() == QuestStatus.START) {
  				int var = qs.getQuestVarById(0);
  				if (var > 4 && var < 56) {
  					changeQuestStep(env, var, 4, false); // 3
  					PacketSendUtility.sendPacket(player, new SM_SYSTEM_MESSAGE(SystemMessageId.QUEST_FAILED_$1, DataManager.QUEST_DATA.getQuestById(questId).getName()));
  					return true;
  				}
  				else if (var == 56) {
  					qs.setQuestVar(57);
  					qs.setStatus(QuestStatus.REWARD);
  					updateQuestStatus(env);
  					return true;
  				}
  			}
  		}
  		return false;
  	}
  }
  