package com.rs.game.player.controllers;

import java.util.List;

import com.rs.Settings;
import com.rs.game.Animation;
import com.rs.game.World;
import com.rs.game.WorldObject;
import com.rs.game.WorldTile;
import com.rs.game.item.Item;
import com.rs.game.map.MapBuilder;
import com.rs.game.minigames.riseofthesix.RiseOfTheSix;
import com.rs.game.npc.NPC;
import com.rs.utils.Utils;

import javafx.scene.layout.RegionBuilder;

public class RiseOfTheSixController extends Controller {

	/**
	 * @author: Pax M
	 */

	private int[] regionBase;
	public WorldTile base;

	public boolean spawned;
	protected NPC bossNPC;

	private int WaveId;

	public int Chest = 18804;
	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 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)
	};

	@Override
	public void start() {
		//player.getPackets().sendGameMessage("todo.");
		//removeControler();
		 regionBase = MapBuilder.findEmptyChunkBound(8, 8); 
		 MapBuilder.copyAllPlanesMap(418, 1176, regionBase[0], regionBase[1], 8); regionBase = MapBuilder.findEmptyChunkBound(8, 8); 
		 MapBuilder.copyAllPlanesMap(418, 1176, regionBase[0], regionBase[1], 8, 8); player.setNextWorldTile(getWorldTile(14, 15));
		 player.getInventory().deleteItem(30004, 1);
		 player.getPackets().sendGameMessage("As you enter a Barrow totem gets destroyed...");
		 player.getPackets().sendGameMessage("Enter the barrier to begin.");
		 player.setNextAnimation(new Animation(21910));
		 WaveId = 0;
	}

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

	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.");
	}

	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();
	}

	@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");
			}
		}

	}

	private void passBarrier() {
		player.setNextWorldTile(getWorldTile(14, 21));
		player.setForceMultiArea(true);
		barrowsBros1();
		barrowsBros2();
		barrowsBros3();
		barrowsBros4();
		barrowsBros5();
		barrowsBros6();
	}

	public void barrowsBros1() {
		bossNPC = new NPC(ahrim, getWorldTile(10, 27), -1, true, true);
		bossNPC.setForceMultiArea(true);
		bossNPC.setForceAgressive(true);
		spawned = true;
	}

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

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

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

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

	public void barrowsBros6() {
		bossNPC = new NPC(verac, getWorldTile(13, 26), -1, true, true);
		bossNPC.setForceMultiArea(true);
		bossNPC.setForceAgressive(true);
		spawned = true;
	}

	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(14, 32)));
			return;
		}
	}

	public WorldTile getWorldTile(int mapX, int mapY) {
		return new WorldTile(regionBase[0] * 8 + mapX, regionBase[1] * 8 + mapY, 0);
	}

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

	@Override
	public boolean sendDeath() {
		player.getPackets().sendGameMessage("Oh dear! You just died.");
		removeControler();
		return true;
	}

	@Override
	public void magicTeleported(int type) {
		removeControler();
	}
}