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 ai.portals;
  
  import com.aionemu.gameserver.ai2.AI2Actions;
  import com.aionemu.gameserver.ai2.AI2Request;
  import com.aionemu.gameserver.ai2.AIName;
  import com.aionemu.gameserver.dataholders.DataManager;
  import com.aionemu.gameserver.model.DescriptionId;
  import com.aionemu.gameserver.model.autogroup.AutoGroupType;
  import com.aionemu.gameserver.model.gameobjects.Creature;
  import com.aionemu.gameserver.model.gameobjects.player.Player;
  import com.aionemu.gameserver.model.templates.portal.PortalPath;
  import com.aionemu.gameserver.model.templates.portal.PortalUse;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_DIALOG_WINDOW;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_FIND_GROUP;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_QUESTION_WINDOW;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
  import com.aionemu.gameserver.services.teleport.PortalService;
  import com.aionemu.gameserver.utils.PacketSendUtility;
  
  import ai.ActionItemNpcAI2;
  
  /**
   * @author Gigi, vlog
   */
  @AIName("satraportal")
  public class SatraPortalAI2 extends ActionItemNpcAI2 {
  
  	@Override
  	protected void handleDialogStart(Player player) {
  		PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(getObjectId(), 10)); // Initial dialog
  	}
  
  	@Override
  	public boolean onDialogSelect(Player player, final int dialogId, int questId, int extendedRewardIndex) {
  		AI2Request request = new AI2Request() {
  
  			@Override
  			public void acceptRequest(Creature requester, Player responder) {
  				moveToInstance(responder);
  			}
  		};
  		switch (dialogId) {
  			case 105:
  				AutoGroupType agt = AutoGroupType.getAutoGroup(player.getLevel(), getNpcId());
  				if (agt != null) {
  					PacketSendUtility.sendPacket(player, new SM_FIND_GROUP(0x1A, agt.getInstanceMapId()));
  				}
  				break;
  			case 65:
  				if (!player.isInGroup2()) {
  					PacketSendUtility.sendPacket(player, new SM_SYSTEM_MESSAGE(1390256));
  					return true;
  				}
  				if (player.getPlayerGroup2().isLeader(player)) {
  					PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(getObjectId(), 4762));
  				}
  				else {
  					if (!isAGroupMemberInInstance(player)) {
  						PacketSendUtility.sendPacket(player, new SM_SYSTEM_MESSAGE(1400361));
  						return true;
  					}
  					moveToInstance(player);
  				}
  				break;
  			case 4763: // easy
  				AI2Actions.addRequest(this, player, SM_QUESTION_WINDOW.STR_INSTANCE_DUNGEON_WITH_DIFFICULTY_ENTER_CONFIRM, getObjectId(),
  					request, new DescriptionId(1804103));
  				PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(getObjectId(), 4762));
  				break;
  			case 4848: // hard
  				AI2Actions.addRequest(this, player, SM_QUESTION_WINDOW.STR_INSTANCE_DUNGEON_WITH_DIFFICULTY_ENTER_CONFIRM, getObjectId(),
  					request, new DescriptionId(1804105));
  				PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(getObjectId(), 4762));
  				break;
  		}
  		return true;
  	}
  
  	private boolean isAGroupMemberInInstance(Player player) {
  		if (player.isInGroup2()) {
  			for (Player member : player.getPlayerGroup2().getMembers()) {
  				if (member.getWorldId() == 300470000) {
  					return true;
  				}
  			}
  		}
  		return false;
  	}
  
  	private void moveToInstance(Player player) {
  		PortalUse portalUse = DataManager.PORTAL2_DATA.getPortalUse(getNpcId());
  		if (portalUse != null) {
  			PortalPath portalPath = portalUse.getPortalPath(player.getRace());
  			if (portalPath != null) {
  				PortalService.port(portalPath, player, getObjectId());
  			}
  		}
  	}
  }
  