package me.craftiii4.Rounds.PossibleRounds.Test;

import java.util.List;

import me.craftiii4.Rounds.GetBlockUnderneath;
import me.craftiii4.Rounds.PickRounds;
import me.craftiii4.Rounds.Rounds;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;

public class BoxSquare {
	private static int time = 60;
	static int example1;
	static int delay = 0;
	static int right1 = 7;
	static int left1 = -7;

	@SuppressWarnings("deprecation")
	public static void startRoundOne45(final Rounds plugin) {

		int currentround = plugin.getDataConfig().getInt("Rounds.CurrentLevel");
		int newround = currentround + 1;

		int howmanyplayers = plugin.getDataConfig()
				.getStringList("PlayersPlaying").size();
		int togo = 0;

		while (howmanyplayers > togo) {
			String playername = plugin.getDataConfig()
					.getStringList("PlayersPlaying").get(togo);
			Player player1 = Bukkit.getPlayer(playername);

			if (!player1.equals(null)) {
				player1.sendMessage(ChatColor.GREEN + "Starting "
						+ ChatColor.GOLD + "Round " + ChatColor.DARK_PURPLE
						+ newround + ChatColor.GREEN + " - Look down - Medium ");

			}
			togo++;
		}

		time = 60;
		plugin.getDataConfig().set("Rounds.CurrentLevel", currentround + 1);
		GetBlockUnderneath.started = true;
		delay = 0;
		example1 = plugin.getServer().getScheduler()
				.scheduleAsyncRepeatingTask(plugin, new Runnable() {
					public void run() {
						
						
						
						if (time == 36) {
							plugin.getServer()
									.getScheduler()
									.scheduleSyncDelayedTask(plugin,
											new Runnable() {

												public void run() {
													
													int blockx = plugin.getArenasConfig().getInt("CenterBlock.X");
													int blocky = plugin.getArenasConfig().getInt("CenterBlock.Y");
													int blockz = plugin.getArenasConfig().getInt("CenterBlock.Z");

													String blockw = plugin.getArenasConfig().getString("World");

													Location locationofblock = Bukkit.getWorld(blockw)
															.getBlockAt(blockx, blocky, blockz)
															.getLocation();

													int start1 = -15;
													int start2 = -11;

													while (start2 <= 11) {
														start1 = -15;

														while (start1 <= 15) {

															Location middle = locationofblock.getBlock()
																	.getLocation();

															Block wool = middle.add(start1, 0, start2)
																	.getBlock();
															wool.setType(Material.WOOL);

															start1++;
														}

														start2++;
													}
													
													
													stopexample1(plugin);
													
													

												}
											}, 60L);
							
						}


						time--;
						if (time < 30) {
							time = 36;
						}
						if (Rounds.stop.equals("ForceStop")) {
							stopexample2(plugin);
						}
						if (Rounds.alldead == true) {
							stopexample3(plugin);
						}

					}

				}, 00L, 80L);

	}

	public static void stopexample1(final Rounds plugin) {
		plugin.getServer().getScheduler().cancelTask(example1);
		Bukkit.getScheduler().cancelTask(example1);
		time = 32;
		plugin.getServer().getScheduler()
				.scheduleSyncDelayedTask(plugin, new Runnable() {

					public void run() {

						int howmanyplayers = plugin.getDataConfig()
								.getStringList("PlayersPlaying").size();
						int togo = 0;
						int currentround = plugin.getDataConfig().getInt(
								"Rounds.CurrentLevel");

						while (howmanyplayers > togo) {
							String playername = plugin.getDataConfig()
									.getStringList("PlayersPlaying").get(togo);
							Player player = Bukkit.getPlayer(playername);

							if (!player.equals(null)) {
								int newround = currentround + 1;
								player.sendMessage(ChatColor.GOLD
										+ "Weldone at completeing Round "
										+ ChatColor.DARK_PURPLE + currentround
										+ ChatColor.GOLD
										+ ", get ready for round "
										+ ChatColor.DARK_PURPLE + newround);
							}
							togo++;
						}

						//

					}
				}, 0L);

		plugin.getServer().getScheduler()
				.scheduleSyncDelayedTask(plugin, new Runnable() {

					public void run() {

						int howmanyplayers = plugin.getDataConfig()
								.getStringList("PlayersPlaying").size();
						int togo = 0;
						// int currentround =
						// plugin.getDataConfig().getInt("Rounds.CurrentLevel");

						while (howmanyplayers > togo) {

							String playername = plugin.getDataConfig()
									.getStringList("PlayersPlaying").get(togo);
							Player player = Bukkit.getPlayer(playername);

							if (!player.equals(null)) {
								if (player.isOnline()) {
									// int newround = currentround + 1;
									player.sendMessage(ChatColor.AQUA
											+ "------------" + ChatColor.GOLD
											+ " [" + ChatColor.LIGHT_PURPLE
											+ " Rounds " + ChatColor.GOLD
											+ "] " + ChatColor.AQUA
											+ "----------");
								}
							}
							togo++;
						}

						int currentround = plugin.getDataConfig().getInt(
								"Rounds.CurrentLevel");
						int newround = currentround + 1;
						PickRounds.PickRound(plugin, newround);
					}

				}, 100L);
	}

	public static void stopexample3(Rounds plugin) {
		plugin.getServer().getScheduler().cancelTask(example1);
		Bukkit.getScheduler().cancelTask(example1);
		time = 32;
		GetBlockUnderneath.running = false;
		Rounds.stop = "Run";

		//

		int howmanyplayers = plugin.getDataConfig()
				.getStringList("PlayersPlaying").size();
		int togo = 0;
		int currentround = plugin.getDataConfig().getInt("Rounds.CurrentLevel");

		while (howmanyplayers > togo) {
			String playername = plugin.getDataConfig()
					.getStringList("PlayersPlaying").get(togo);
			Player player = Bukkit.getPlayer(playername);

			if (!player.equals(null)) {
				player.sendMessage(ChatColor.GOLD
						+ "All players are dead! Got to round "
						+ ChatColor.DARK_PURPLE + currentround);
			}
			togo++;
		}

		//

		//

		List<String> playing = plugin.getDataConfig().getStringList(
				"PlayersPlaying");
		playing.clear();
		plugin.getDataConfig().set("PlayersPlaying", playing);

		Rounds.gamerunning = false;
		Rounds.blockplayer.clear();

		plugin.saveDataConfig();

	}

	public static void stopexample2(Rounds plugin) {
		plugin.getServer().getScheduler().cancelTask(example1);
		Bukkit.getScheduler().cancelTask(example1);
		time = 32;
		Rounds.stop = "Run";
		GetBlockUnderneath.running = false;

	}

}
