package com.rs.game.player.dialogues.impl;

import java.util.ArrayList;
import java.util.List;

import com.rs.Settings;
import com.rs.game.Animation;
import com.rs.game.Entity;
import com.rs.game.Graphics;
import com.rs.game.Region;
import com.rs.game.World;
import com.rs.game.WorldTile;
import com.rs.game.minigames.riseofthesix.RiseOfTheSix;
import com.rs.game.player.Player;
import com.rs.game.player.dialogues.Dialogue;
import com.rs.game.tasks.WorldTask;
import com.rs.game.tasks.WorldTasksManager;
import com.rs.utils.Utils;

public class RiseOfTheSixD extends Dialogue {

	public RiseOfTheSixD() {

	}

	@Override
	public void start() {
		stage = 1;
		player.setNextAnimation(new Animation(21924));
		sendOptionsDialogue(DEFAULT_OPTIONS_TITLE, "Start.", "Nevermind.");
	}

	@Override
	public void run(int interfaceId, int componentId) {
		switch (stage) {
		case -1:
			stop();
			end();
			break;
		case 1:
			switch (componentId) {
			case OPTION_1:
				stage = 2;
				sendOptionsDialogue(DEFAULT_OPTIONS_TITLE, "Solo.", "I changed my mind.");
				break;
			case OPTION_2:
			    end();
			}
			break;
		case 2:
			switch (componentId) {
			case OPTION_1:
				if (player.getInventory().containsItem(30004, 1)) {
					player.getControlerManager().startControler("NewRiseOfSix", true);
					end();
				} else {
					stage = -1;
					sendDialogue("You need a Barrows totem to enter.");
				}
				break;
			case OPTION_2:
			    	end();
				break;
			}
			break;
		default:
			stop();
			end();
			break;
		}
	}

	public void stop() {
		player.setNextAnimation(new Animation(21922));
	}

	public static void joinParty(Player player, String username) {
		Player host = World.getPlayerByDisplayName(username);
		if (player.getUsername().equalsIgnoreCase(username))
			player.getPackets().sendGameMessage("You can't join your own party.");
		else if (!World.containsPlayer(username) || host == null)
			player.getPackets().sendGameMessage("That player is offline.");
		else if (host.isLocked())
			player.getPackets().sendGameMessage("The player you have entered is busy.");
		else if (host.getROSPartyMembers().size() >= 4)
			player.getPackets().sendGameMessage("The player you have entered already has a full party of 4.");
		else {
			host.addROSPartyMember(player);
			player.getPackets().sendGameMessage("You have joined " + Utils.fixChatMessage(username) + "'s party.");
			host.getPackets().sendGameMessage(player.getDisplayName() + " has joined your party.");
		}
		player.setNextAnimation(new Animation(21922));
	}

	@Override
	public void finish() {

	}
	
	public static Player[] getNearPlayers(Player player, int distance, int maxTargets) {
		List<Entity> possibleTargets = new ArrayList<Entity>();
		stop: for (int regionId : player.getMapRegionsIds()) {
			Region region = World.getRegion(regionId);
				List<Integer> playerIndexes = region.getPlayerIndexes();
				if (playerIndexes == null)
					continue;
				for (int playerIndex : playerIndexes) {
					Player p2 = World.getPlayers().get(playerIndex);
					if (p2 == null || p2 == player || p2.isDead() || !p2.hasStarted() || p2.hasFinished() 
					 || !p2.withinDistance(player, distance))
						continue;
					possibleTargets.add(p2);
					if (possibleTargets.size() == maxTargets)
						break stop;
				}
		}
		return possibleTargets.toArray(new Player[possibleTargets.size()]);
	}
}