package me.craftiii4.Rounds.PossibleRounds.BonusRounds;

import java.util.List;
import java.util.Random;

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

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

public class BonusRandomLocation {
	

	private static int time = 17;
	static int example1;
	static int delay = 10;
	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.BLUE + " - Bonus Round!");
			
			}
			togo++;
		}
		
		
		
		
		
		
		time = 17;
		plugin.getDataConfig().set("Rounds.CurrentLevel", 1);
		GetBlockUnderneath.started = true;
		example1 = plugin.getServer().getScheduler()
				.scheduleAsyncRepeatingTask(plugin, new Runnable() {
					
					public void run() {			
						
						if (time == 16) {
							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 + "Better get moving! Check out below");
								
								}
								togo++;
							}
						}
						
						if (time <= 15 && time > 0) {
							
							int timesthroughpink = 0;
							int timestogothroughpink = 4;
							
							int timesthroughblue = 0;
							int timestogothroughblue = 0;
							
							int timesthroughpurple = 0;
							int timestogothroughpurple = 0;
							
							if (time <= 12) {
								timestogothroughpink = 6;
							}
							if (time <= 12) {
								timestogothroughblue = 2;
							}
							if (time <= 9) {
								timestogothroughpink = 8;
							}
							if (time <= 5) {
								timestogothroughpurple = 1;
							}
							if (time <= 5) {
								timestogothroughblue = 4;
							}
							
							while (timesthroughpurple < timestogothroughpurple) {
								
								int START = -7;
								int END = 7;
								Random random = new Random();
								long range = (long) END - (long) START + 1;
								// compute a fraction of the range, 0 <= frac < range
								long fraction = (long) (range * random.nextDouble());
								final int randomx = (int) (fraction + START);
								
								int START1 = -5;
								int END1 = 5;
								Random random1 = new Random();
								long range1 = (long) END1 - (long) START1 + 1;
								// compute a fraction of the range, 0 <= frac < range
								long fraction1 = (long) (range1 * random1.nextDouble());
								final int randomz = (int) (fraction1 + START1);
								
								final int centerx = plugin.getArenasConfig().getInt("CenterBlock.X") + randomx;
								final int centery = plugin.getArenasConfig().getInt("CenterBlock.Y");
								final int centerz = plugin.getArenasConfig().getInt("CenterBlock.Z") + randomz;
							
								final String world = plugin.getArenasConfig().getString("World");
								
								Block block1 = Bukkit.getWorld(world)
										.getBlockAt(centerx, centery,
												centerz);
								
								if (block1.getData() == 0) {
									block1.setType(Material.WOOL);
									block1.setData((byte) 10);
									
								}
								timesthroughpurple++;
							}
							
							while (timesthroughblue < timestogothroughblue) {
								
								int START = -7;
								int END = 7;
								Random random = new Random();
								long range = (long) END - (long) START + 1;
								// compute a fraction of the range, 0 <= frac < range
								long fraction = (long) (range * random.nextDouble());
								final int randomx = (int) (fraction + START);
								
								int START1 = -5;
								int END1 = 5;
								Random random1 = new Random();
								long range1 = (long) END1 - (long) START1 + 1;
								// compute a fraction of the range, 0 <= frac < range
								long fraction1 = (long) (range1 * random1.nextDouble());
								final int randomz = (int) (fraction1 + START1);
								
								final int centerx = plugin.getArenasConfig().getInt("CenterBlock.X") + randomx;
								final int centery = plugin.getArenasConfig().getInt("CenterBlock.Y");
								final int centerz = plugin.getArenasConfig().getInt("CenterBlock.Z") + randomz;
							
								final String world = plugin.getArenasConfig().getString("World");
								
								Block block1 = Bukkit.getWorld(world)
										.getBlockAt(centerx, centery,
												centerz);
								
								if (block1.getData() == 0) {
									block1.setType(Material.WOOL);
									block1.setData((byte) 11);
									
								}
								timesthroughblue++;
							}
							
							
							while (timesthroughpink < timestogothroughpink) {
								
								int START = -7;
								int END = 7;
								Random random = new Random();
								long range = (long) END - (long) START + 1;
								// compute a fraction of the range, 0 <= frac < range
								long fraction = (long) (range * random.nextDouble());
								final int randomx = (int) (fraction + START);
								
								int START1 = -5;
								int END1 = 5;
								Random random1 = new Random();
								long range1 = (long) END1 - (long) START1 + 1;
								// compute a fraction of the range, 0 <= frac < range
								long fraction1 = (long) (range1 * random1.nextDouble());
								final int randomz = (int) (fraction1 + START1);
								
								final int centerx = plugin.getArenasConfig().getInt("CenterBlock.X") + randomx;
								final int centery = plugin.getArenasConfig().getInt("CenterBlock.Y");
								final int centerz = plugin.getArenasConfig().getInt("CenterBlock.Z") + randomz;
							
								final String world = plugin.getArenasConfig().getString("World");
								
								Block block1 = Bukkit.getWorld(world)
										.getBlockAt(centerx, centery,
												centerz);
								
								if (block1.getData() == 0) {
									block1.setType(Material.WOOL);
									block1.setData((byte) 6);
									
								}
								timesthroughpink++;
							}
						
						}
						
						if (time == 0) {
							plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
								

							    public void run() {
									stopexample1(plugin);
									
							    }
							}, 100L);
						}
						time--;
						if (time < -10) {
							time = 0;
						}
						
						if (Rounds.stop.equals("ForceStop")) {
							stopexample2(plugin);
						}
						if (Rounds.alldead == true) {
							stopexample3(plugin);
						}

					}

				}, 60L, 40L);
	}

	public static void stopexample1(final Rounds plugin) {
		plugin.getServer().getScheduler().cancelTask(example1);
		Bukkit.getScheduler().cancelTask(example1);
		time = 17;
		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.GOLD + "Weldone at completeing Round " + ChatColor.DARK_PURPLE + currentround +  ChatColor.GOLD + ", get ready for round " + ChatColor.DARK_PURPLE + newround);
					
						int howmanytodo = 3;
						int howmanydone = 0;
						
						while (howmanydone < howmanytodo) {
						int START = -7;
						int END = 7;
						Random random = new Random();
						long range = (long) END - (long) START + 1;
						// compute a fraction of the range, 0 <= frac < range
						long fraction = (long) (range * random.nextDouble());
						final int randomx = (int) (fraction + START);
						
						int START1 = -5;
						int END1 = 5;
						Random random1 = new Random();
						long range1 = (long) END1 - (long) START1 + 1;
						// compute a fraction of the range, 0 <= frac < range
						long fraction1 = (long) (range1 * random1.nextDouble());
						final int randomz = (int) (fraction1 + START1);
						
						final int centerx = plugin.getArenasConfig().getInt("CenterBlock.X") + randomx;
						final int centery = plugin.getArenasConfig().getInt("CenterBlock.Y");
						final int centerz = plugin.getArenasConfig().getInt("CenterBlock.Z") + randomz;
					
						final String world = plugin.getArenasConfig().getString("World");
						
						Block block1 = Bukkit.getWorld(world)
								.getBlockAt(centerx, centery,
										centerz);
						block1.setData((byte) 6);
						howmanydone++;
						}
						
						}
						}
					togo++;
				}
				
				//
				
				

		    	
		    }
		}, 110L);
		
		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);
				
		    }
		    	
		}, 200L);
		
		
	}
	public static void stopexample3(Rounds plugin) {
		plugin.getServer().getScheduler().cancelTask(example1);
		Bukkit.getScheduler().cancelTask(example1);
		time = 17;
		GetBlockUnderneath.running = false;
		Rounds.stop = "Run";
		GetBlockUnderneath.running = false;
		
		//
		
		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()) {
				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.clear();
		Rounds.blockplayer.clear();
		
		plugin.saveDataConfig();
		
	}
	public static void stopexample2(Rounds plugin) {
		plugin.getServer().getScheduler().cancelTask(example1);
		Bukkit.getScheduler().cancelTask(example1);
		time = 17;
		Rounds.stop = "Run";
		GetBlockUnderneath.running = false;

		
		//check online for everything tommorow
		
	}
	
	public static void StartArena(final Rounds plugin) {
		
		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.AQUA + "------------" + ChatColor.GOLD + " [" + ChatColor.LIGHT_PURPLE + " Rounds " + ChatColor.GOLD + "] " + ChatColor.AQUA + "----------");
				player1.sendMessage(ChatColor.GREEN + "Welcome to Rounds, Created by Craftiii4");
				player1.sendMessage(ChatColor.GREEN + "The objective is to survive for as long as possible");
				player1.sendMessage(ChatColor.GREEN + "To do this you must not touch any red wool");
				player1.sendMessage(ChatColor.GREEN + "You can not jump over red wool");
				player1.sendMessage(ChatColor.GREEN + "Going over " + ChatColor.LIGHT_PURPLE + "pink" + ChatColor.GREEN + ", " + ChatColor.DARK_BLUE + "blue " + ChatColor.GREEN + "or " + ChatColor.DARK_PURPLE + "purple" + ChatColor.GREEN + " wool will give you a bonus");
				player1.sendMessage(ChatColor.DARK_GREEN + "Green" + ChatColor.GREEN + " wool will turn to " + ChatColor.RED + "Orange");
				player1.sendMessage(ChatColor.RED + "Orange" + ChatColor.GREEN + " will then turn to " + ChatColor.DARK_RED + "Red");
				player1.sendMessage(ChatColor.GREEN + "We will play a 60 second warm up round to get you started");		
				
			}
			togo++;
		}
		
		plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

		    public void run() {
		    	
				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.AQUA + "------------" + ChatColor.GOLD + " [" + ChatColor.LIGHT_PURPLE + " Rounds " + ChatColor.GOLD + "] " + ChatColor.AQUA + "----------");
						player1.sendMessage(ChatColor.GREEN + "Starting " + ChatColor.GOLD + "Round " + ChatColor.DARK_PURPLE + "1" + ChatColor.GREEN + " - Look out in front");
					
					}
					togo++;
				}
				
				
				startRoundOne45(plugin);
				
		    }
		}, 200L);
		
		
	}
	

}
