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 com.aionemu.gameserver.questEngine.handlers.template;
  
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Set;
  
  import com.aionemu.gameserver.model.DialogAction;
  import com.aionemu.gameserver.model.gameobjects.player.Player;
  import com.aionemu.gameserver.model.rift.RiftLocation;
  import com.aionemu.gameserver.model.vortex.VortexLocation;
  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.RiftService;
  import com.aionemu.gameserver.services.VortexService;
  
  /**
   * Standard xml-based handling for the DAILY quests with onKillInZone events
   *
   * @author vlog, reworked bobobear
   */
  public class KillInWorld extends QuestHandler {
  
  	private final int questId;
  	private final Set<Integer> startNpcs = new HashSet<Integer>();
  	private final Set<Integer> endNpcs = new HashSet<Integer>();
  	private final Set<Integer> worldIds = new HashSet<Integer>();
  	private final int killAmount;
  	private final int invasionWorldId;
  	private final int endDialog;
  
  	public KillInWorld(int questId, List<Integer> endNpcIds, List<Integer> startNpcIds, List<Integer> worldIds, int killAmount, int invasionWorld, int endDialog) {
  		super(questId);
  		if (startNpcIds != null) {
  			this.startNpcs.addAll(startNpcIds);
  			this.startNpcs.remove(0);
  		}
  		if (endNpcIds == null) {
  			this.endNpcs.addAll(startNpcs);
  		}
  		else {
  			this.endNpcs.addAll(endNpcIds);
  			this.endNpcs.remove(0);
  		}
  		this.questId = questId;
  		this.worldIds.addAll(worldIds);
  		this.worldIds.remove(0);
  		this.killAmount = killAmount;
  		this.invasionWorldId = invasionWorld;
  		this.endDialog = endDialog;
  	}
  
  	@Override
  	public void register() {
  		Iterator<Integer> iterator = startNpcs.iterator();
  		while (iterator.hasNext()) {
  			int startNpc = iterator.next();
  			qe.registerQuestNpc(startNpc).addOnQuestStart(getQuestId());
  			qe.registerQuestNpc(startNpc).addOnTalkEvent(getQuestId());
  		}
  		iterator = endNpcs.iterator();
  		while (iterator.hasNext()) {
  			int endNpc = iterator.next();
  			qe.registerQuestNpc(endNpc).addOnTalkEvent(getQuestId());
  		}
  		iterator = worldIds.iterator();
  		while (iterator.hasNext()) {
  			int worldId = iterator.next();
  			qe.registerOnKillInWorld(worldId, questId);
  		}
  		if (invasionWorldId != 0) {
  			qe.registerOnEnterWorld(questId);
  		}
  	}
  
  	@Override
  	public boolean onDialogEvent(QuestEnv env) {
  		Player player = env.getPlayer();
  		QuestState qs = player.getQuestStateList().getQuestState(questId);
  		int targetId = env.getTargetId();
  		DialogAction dialog = env.getDialog();
  		if (qs == null || qs.getStatus() == QuestStatus.NONE || qs.canRepeat()) {
  			if (startNpcs.isEmpty() || startNpcs.contains(targetId)) {
  				switch (dialog) {
  					case QUEST_SELECT: {
  						return sendQuestDialog(env, 4762);
  					}
  					case QUEST_ACCEPT_1: {
  						return sendQuestStartDialog(env);
  					}
  					default: {
  						return sendQuestStartDialog(env);
  					}
  				}
  			}
  		}
  		else if (qs != null && qs.getStatus() == QuestStatus.REWARD) {
  			if (endNpcs.contains(targetId) && endDialog != 0) {
  				switch (dialog) {
  					case USE_OBJECT: {
  						return sendQuestDialog(env, endDialog);
  					}
  					case SELECT_QUEST_REWARD: {
  						return sendQuestDialog(env, 5);
  					}
  					case SELECTED_QUEST_NOREWARD: {
  						return sendQuestEndDialog(env);
  					}
  					default:
  						break;
  				}
  			}
  			else {
  				return sendQuestEndDialog(env);
  			}
  		}
  		return false;
  	}
  
  	@Override
  	public boolean onEnterWorldEvent(QuestEnv env) {
  		Player player = env.getPlayer();
  		QuestState qs = player.getQuestStateList().getQuestState(questId);
  		VortexLocation vortexLoc = VortexService.getInstance().getLocationByWorld(invasionWorldId);
  		if (player.getWorldId() == invasionWorldId) {
  			if ((qs == null || qs.getStatus() == QuestStatus.NONE || qs.canRepeat())) {
  				if ((vortexLoc != null && vortexLoc.isActive()) || (searchOpenRift())) {
  					return QuestService.startQuest(env);
  				}
  				else if (invasionWorldId == 400010000 && player.isSpawned() && player.isProtectionActive()) {
  					return QuestService.startQuest(env);
  				}
  			}
  		}
  		return false;
  	}
  
  	private boolean searchOpenRift() {
  		for (RiftLocation loc : RiftService.getInstance().getRiftLocations().values()) {
  			if (loc.getWorldId() == invasionWorldId && loc.isOpened()) {
  				return true;
  			}
  		}
  		return false;
  	}
  
  	@Override
  	public boolean onKillInWorldEvent(QuestEnv env) {
  		return defaultOnKillRankedEvent(env, 0, killAmount, true); // reward
  	}
  }
  