package com.rs.game.player.controllers;

import com.rs.game.WorldObject;
import com.rs.Settings;
import com.rs.executor.GameExecutorManager;
import com.rs.game.map.MapBuilder;
import com.rs.game.World;
import com.rs.game.Animation;
import com.rs.game.WorldTile;
import com.rs.game.item.Item;
import com.rs.game.tasks.WorldTask;
import com.rs.game.tasks.WorldTasksManager;
import com.rs.utils.Utils;
import com.rs.game.player.MusicsManager;
import com.rs.game.player.Player;
import com.rs.game.npc.NPC;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class NewRiseOfSix extends Controller {
	
	/**
	 *	 @Author DuuLan
	 *	@NewAuth OnegaiSatan/Baki
	 **/
	
	public static final WorldTile HOME = new WorldTile(Settings.START_PLAYER_LOCATION);
	private int[] boundChuncks;
	@SuppressWarnings("unused")
	private boolean login;
	public boolean spawned;
	protected NPC bossNPC;
	public int Chest = 87996;
	public int Rope = 87998;
	public int Portal = 87993;
	public int NoPortal = 87995;
	public int Barrier = 31314;
	public int dharok = 18540;
	public int verac = 18545;
	public int guthan = 18541;
	public int torag = 18544;
	public int ahrim = 18538;
	public int karil = 18543;
	private int WaveId;
	
	@Override
	public void start() {
		loadRegion();
	}
	
	@Override
	public boolean login() {
		loadRegion();
		return false;
	}
	
	@Override
	public boolean sendDeath() {
		player.lock(8);
		player.stopAll();
		WorldTasksManager.schedule(new WorldTask() {
			int loop;
			@Override
			public void run() {
				if (loop == 0) {
					player.setNextAnimation(player.getDeathAnimationDbF());
				} else if (loop == 1) {
					player.getPackets().sendGameMessage("You have been defeated!");
				} else if (loop == 3) {
					player.reset();
					exitCave();
					player.setNextAnimation(new Animation(-1));
				} else if (loop == 4) {
					player.getMusicsManager().playMusicEffect(MusicsManager.DEATH_MUSIC_EFFECT);
					stop();
				}
				loop++;
			}
		}, 0, 1);
		return false;
	}

	public void loadRegion() {
		boundChuncks = MapBuilder.findEmptyChunkBound(8, 8);
		MapBuilder.copyAllPlanesMap(289, 737, boundChuncks[0], boundChuncks[1], 8); boundChuncks = MapBuilder.findEmptyChunkBound(8, 8); 
		MapBuilder.copyAllPlanesMap(289, 737, boundChuncks[0], boundChuncks[1], 8, 8); player.setNextWorldTile(getWorldTile(18, 1));
		barrowsBros1();
		barrowsBros2();
		barrowsBros3();
		barrowsBros4();
		barrowsBros5();
		barrowsBros6();
		WaveId = 0;
	}

	public void bandosBoss() {
		bossNPC = new NPC(2, getWorldTile(16, 16), -1, true, true);
		bossNPC.setForceAgressive(true);
		spawned = true;
	}

	public void exitCave() {
		player.setNextWorldTile(Settings.START_PLAYER_LOCATION);
		removeControler();
		player.setForceMultiArea(false);
		GameExecutorManager.slowExecutor.schedule(new Runnable() {
			@Override
			public void run() {
				MapBuilder.destroyMap(boundChuncks[0], boundChuncks[1], 8, 8);
			}
		}, 1200, TimeUnit.MILLISECONDS);
	}
	
	@Override
	public void forceClose() {
		exitCave();
	}

	@Override
	public boolean logout() {
		exitCave();
		return true;
	}

	@Override
	public boolean processObjectClick1(WorldObject object) {
		if (object.getId() == 88001) {
			exitCave();
		}
		if (object.getId() == Chest) {
			lootChest();
		}
		return false;
	}

	@Override
	public boolean processNPCClick1(NPC npc) {
		switch(npc.getId()) {
			default:
				player.getPackets().sendGameMessage("NPC not added to RoS Controller.");
				return true;
		}
	}
	
	@Override
	public boolean processNPCClick2(NPC npc) {
		switch(npc.getId()) {
			default:
				player.getPackets().sendGameMessage("NPC not added to RoS Controller.");
				return true;
		}
	}

	@Override
	public void magicTeleported(int type) {
		removeControler();
	}
	
	public WorldTile getWorldTile(int mapX, int mapY) {
		return new WorldTile (boundChuncks[0] * 8 + mapX, boundChuncks[1] * 8 + mapY, 0);
	}
	
	@Override
	public boolean processButtonClick(int interfaceId, int componentId, int slotId, int slotId2, int packetId) {
		if (interfaceId == 182 && (componentId == 6 || componentId == 13)) {
			player.disconnect(true, true);
			return false;
		}
		return true;
	}
	
	@Override
	public void process() {
		if (spawned) {
			List<Integer> npcsInArea = World.getRegion(player.getRegionId()).getNPCsIndexes();
			if (npcsInArea == null || npcsInArea.isEmpty()) {
				spawned = false;
				WaveId += 1;
				nextWave(WaveId);
				System.out.println("next");
			}
		}

	}

	    public static boolean handleDoor(Player player, WorldObject object, long timer) {
		if (World.isSpawnedObject(object)) {
		    return false;
		}
		WorldObject openedDoor = new WorldObject(object.getId(), object.getType(), (object.getRotation() + 1) & 0x3,
			object.getX(), object.getY(), object.getPlane());
		World.spawnObjectTemporary(openedDoor, timer);
		return false;
	    }

	    //private static boolean handleDoor(Player player, WorldObject object) {
		//return handleDoor(player, object, 20000);
	    //}
	    
	    public void barrowsBros1() {
		bossNPC = new NPC(ahrim, getWorldTile(18, 15), -1, true, true);
		bossNPC.setForceMultiArea(true);
		bossNPC.setForceAgressive(true);
		spawned = true;
	    }

	    public void barrowsBros2() {
		bossNPC = new NPC(dharok, getWorldTile(18, 16), -1, true, true);
		bossNPC.setForceMultiArea(true);
		bossNPC.setForceAgressive(true);
		spawned = true;
	    }

	    public void barrowsBros3() {
		bossNPC = new NPC(guthan, getWorldTile(18, 17), -1, true, true);
		bossNPC.setForceMultiArea(true);
		bossNPC.setForceAgressive(true);
		spawned = true;
	    }

	    public void barrowsBros4() {
		bossNPC = new NPC(karil, getWorldTile(18, 14), -1, true, true);
		bossNPC.setForceMultiArea(true);
		bossNPC.setForceAgressive(true);
		spawned = true;
	    }

	    public void barrowsBros5() {
		bossNPC = new NPC(torag, getWorldTile(18, 18), -1, true, true);
		bossNPC.setForceMultiArea(true);
		bossNPC.setForceAgressive(true);
		spawned = true;
	    }

	    public void barrowsBros6() {
		bossNPC = new NPC(verac, getWorldTile(18, 19), -1, true, true);
		bossNPC.setForceMultiArea(true);
		bossNPC.setForceAgressive(true);
		spawned = true;
	    }
		
	    public void lootChest() {
		if (Utils.random(20) == 0)
			drop(BARROW_REWARDS[Utils.random(BARROW_REWARDS.length)]);
		if (Utils.random(10) == 0)
			drop(RARE_REWARDS[Utils.random(RARE_REWARDS.length)]);
		if (Utils.random(1) == 0)
			drop(COMMUM_REWARDS[Utils.random(COMMUM_REWARDS.length)]);
			drop(COMMUM_REWARDS[Utils.random(COMMUM_REWARDS.length)]);
			drop(COMMUM_REWARDS[Utils.random(COMMUM_REWARDS.length)]);
			player.rosTrips++;
			player.getPackets().sendGameMessage("You managed to slay all the Barrows brothers and escape with some loot.");
			player.setNextWorldTile(new WorldTile(Settings.RISE_OF_SIX_EXIT));
			player.setForceMultiArea(false);
			removeControler();
	    }
	    
	    private boolean noSpaceOnInv;

	    public void drop(Item item) {
			Item dropItem = new Item(item.getId(), Utils.random(item.getDefinitions().isStackable() ? item.getAmount() : item.getAmount()) + 1);
			if (!noSpaceOnInv && player.getInventory().addItem(dropItem))
				return;
			noSpaceOnInv = true;
			player.getBank().addItem(dropItem, false);
			player.getPackets().sendGameMessage("Your loot was placed into your bank.");
	    }
	    
	    private static final Item[] COMMUM_REWARDS = {
			new Item(995, 100000),
			new Item(1748, 60), 
			new Item(29759, 3),
			new Item(9245, 55),
			new Item(1514, 200),
			new Item(1128, 7),
			new Item(452, 30),
			new Item(15271, 125),
			new Item(5303, 8),
			new Item(5302, 8),
			new Item(5316, 4),
			new Item(1392, 40)
	    };

	    private static final Item[] RARE_REWARDS = { 
			new Item(30014, 1), 
			new Item(30018, 1), 
			new Item(30022, 1),
			new Item(9245, 55)
	    };

	    Item[] BARROW_REWARDS = {
			new Item(30027, 1), 
			new Item(30031, 1),
			new Item(30032, 1), 
			new Item(30033, 1), 
			new Item(30034, 1),
			new Item(30035, 1),
			new Item(30036, 1)
	    };
	    
		private void nextWave(int waveid) {
			if (waveid == 1) {
				player.getPackets().sendGameMessage("Congratulations! You've defeated the Barrows Brothers.");
				player.getPackets().sendGameMessage("You now have access to the chest.");
				World.spawnObject(new WorldObject(Chest, 10, 0, getWorldTile(18, 20)));
				return;
			}
		}
		
}