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.verteron;
  
  import com.aionemu.gameserver.model.DialogAction;
  import com.aionemu.gameserver.model.TeleportAnimation;
  import com.aionemu.gameserver.model.gameobjects.player.Player;
  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.teleport.TeleportService2;
  import com.aionemu.gameserver.utils.ThreadPoolManager;
  
  /**
   * @author pralinka
   */
  public class _14016AGateAgape extends QuestHandler {
  
  	private final static int questId = 14016;
  
  	public _14016AGateAgape() {
  		super(questId);
  	}
  
  	@Override
  	public void register() {
  		int[] npcs = { 203098, 700142 };
  		qe.registerOnEnterZoneMissionEnd(questId);
  		qe.registerOnLevelUp(questId);
  		qe.registerOnEnterWorld(questId);
  		qe.registerOnDie(questId);
  		qe.registerQuestNpc(233873).addOnKillEvent(questId);
  		// qe.registerOnMovieEndQuest(153, questId);
  		for (int npcId : npcs) {
  			qe.registerQuestNpc(npcId).addOnTalkEvent(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 = env.getTargetId();
  		DialogAction dialog = env.getDialog();
  		if (qs.getStatus() == QuestStatus.START) {
  			switch (targetId) {
  				case 203098: {
  					switch (dialog) {
  						case QUEST_SELECT: {
  							if (var == 0) {
  								return sendQuestDialog(env, 1011);
  							}
  						}
  						case SETPRO1: {
  							TeleportService2.teleportTo(player, 210030000, 2683.2085f, 1068.8977f, 199.375f, (byte) 60, TeleportAnimation.BEAM_ANIMATION);
  							changeQuestStep(env, 0, 1, false); // 1
  							return closeDialogWindow(env);
  						}
  						default:
  							break;
  					}
  					break;
  				}
  				case 700142: // Abyss Gate
  					long Seal = player.getInventory().getItemCountByItemId(182215317);
  					if (Seal == 1) {
  						destroy(-2, env);
  						return false;
  					}
  					break;
  			}
  
  		}
  		else if (qs.getStatus() == QuestStatus.REWARD) {
  			if (targetId == 203098) { // Spatalos
  				if (env.getDialog() == DialogAction.QUEST_SELECT) {
  					return sendQuestDialog(env, 1352);
  				}
  				else {
  					removeQuestItem(env, 182215317, 1);
  					return sendQuestEndDialog(env);
  				}
  			}
  		}
  		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.getQuestVars().getQuestVars();
  			if (var == 2) {
  				changeQuestStep(env, 2, 1, false);
  				removeQuestItem(env, 182215317, 1);
  				return true;
  			}
  		}
  		return false;
  	}
  
  	@Override
  	public boolean onEnterWorldEvent(QuestEnv env) {
  		Player player = env.getPlayer();
  		QuestState qs = player.getQuestStateList().getQuestState(questId);
  		if (qs == null) {
  			return false;
  		}
  		if (qs.getStatus() == QuestStatus.START) {
  			int var = qs.getQuestVars().getQuestVars();
  			if (var == 2 && player.getWorldId() != 310030000) {
  				changeQuestStep(env, 2, 1, false);
  				removeQuestItem(env, 182215317, 1);
  				return true;
  			}
  			else if (var == 1 && player.getWorldId() == 310030000) {
  				changeQuestStep(env, 1, 2, false); // 2
  				QuestService.addNewSpawn(310030000, player.getInstanceId(), 233873, (float) 258.89917, (float) 237.20166, (float) 217.06035, (byte) 0);
  				return true;
  			}
  		}
  		return false;
  	}
  
  	@Override
  	public boolean onKillEvent(QuestEnv env) {
  		return defaultOnKillEvent(env, 233873, 2, 2);
  	}
  
  	@Override
  	public boolean onZoneMissionEndEvent(QuestEnv env) {
  		return defaultOnZoneMissionEndEvent(env);
  	}
  
  	@Override
  	public boolean onLvlUpEvent(QuestEnv env) {
  		int[] verteronQuests = { 14011, 14012, 14013, 14014, 14015 };
  		return defaultOnLvlUpEvent(env, verteronQuests, true);
  	}
  
  	private void destroy(final int var, final QuestEnv env) {
  		final int targetObjectId = env.getVisibleObject().getObjectId();
  
  		final Player player = env.getPlayer();
  		ThreadPoolManager.getInstance().schedule(new Runnable() {
  
  			@Override
  			public void run() {
  				if (player.getTarget().getObjectId() != targetObjectId) {
  					return;
  				}
  				switch (var) {
  					case -2:
  						changeQuestStep(env, 2, 3, true);
  						playQuestMovie(env, 153);
  						break;
  				}
  			}
  		}, 100);
  	}
  }
  