package com.rs.game.player.actions;

import com.rs.Settings;
import com.rs.game.Animation;
import com.rs.game.Graphics;
import com.rs.game.item.Item;
import com.rs.game.player.Equipment;
import com.rs.game.player.Player;
import com.rs.game.player.Skills;
import com.rs.game.player.actions.Action;
import com.rs.utils.Utils;

public class EnchantingBolts extends Action {

	public enum SmeltingBar {
	    
	    	OPAL_BOLTS(4, 9, new Item[] { new Item(879, 10), new Item(556, 2), new Item(564, 1) }, new Item(9236, 10), Skills.MAGIC),

		SAPPHIRE_BOLTS(7, 17, new Item[] { new Item(9337, 10), new Item(564, 1), new Item(555, 1), new Item(558, 1) }, new Item(9240, 10), Skills.MAGIC),

		JADE_BOLTS(14, 19, new Item[] { new Item(9335, 10), new Item(557, 2), new Item(564, 1) }, new Item(9237, 10), Skills.MAGIC),
		
		PEARL_BOLTS(24, 29.5, new Item[] { new Item(880, 10), new Item(555, 2), new Item(564, 1) }, new Item(9238, 10), Skills.MAGIC),
		
		EMERALD_BOLTS(27, 37, new Item[] { new Item(9338, 10), new Item(556, 3), new Item(561, 1), new Item(564, 1) }, new Item(9241, 10), Skills.MAGIC),
		
		TOPAZ_BOLTS(29, 33.5, new Item[] { new Item(9336, 10), new Item(554, 2), new Item(564, 1) }, new Item(9239, 10), Skills.MAGIC),
		
		RUBY_BOLTS(49, 59, new Item[] { new Item(9339, 10), new Item(554, 5), new Item(565, 1), new Item(564, 1) }, new Item(9242, 10), Skills.MAGIC),
		
		DIAMOND_BOLTS(4, 9, new Item[] { new Item(9340, 10), new Item(557, 10), new Item(563, 1), new Item(564, 1) }, new Item(9236, 10), Skills.MAGIC),
		
		DRAGON_BOLTS(4, 9, new Item[] { new Item(9341, 10), new Item(557, 15), new Item(566, 1), new Item(564, 1) }, new Item(9244, 10), Skills.MAGIC),
		
		ONYX_BOLTS(87, 97, new Item[] { new Item(9342, 10), new Item(554, 20), new Item(560, 1), new Item(564, 1) }, new Item(9245, 10), Skills.MAGIC),
		
		ASCENDRI_BOLTS(90, 120, new Item[] { new Item(31868, 10), new Item(554, 20), new Item(560, 1), new Item(564, 1) }, new Item(31881, 10), Skills.MAGIC),
		
		//USELESS
		IRON(150, 0, new Item[] { new Item(0) }, new Item(0), Skills.SMITHING),
		GOLD(150, 0, new Item[] { new Item(0) }, new Item(0), Skills.SMITHING),
		MOLTEN_GLASS(150, 0, new Item[] { new Item(0) }, new Item(0), Skills.SMITHING),
		
		;

		public static SmeltingBar getBarByProduce(int id) {
			for(SmeltingBar bar : SmeltingBar.values()) {
				if(bar.getProducedBar().getId() == id)
					return bar;
			}
			return null;
		}
		
		public static SmeltingBar getBar(int id) {
			for(SmeltingBar bar : SmeltingBar.values()) {
				for(Item item : bar.getItemsRequired())
				if(item.getId() == id)
					return bar;
			}
			return null;
		}

		private int levelRequired;
		private double experience;
		private Item[] itemsRequired;
		private Item producedBar;
		private int skillType;

		private SmeltingBar(int levelRequired, double experience, Item[] itemsRequired, Item producedBar, int skillType) {
			this.levelRequired = levelRequired;
			this.experience = experience;
			this.itemsRequired = itemsRequired;
			this.producedBar = producedBar;
			this.skillType = skillType;
		}

		public Item[] getItemsRequired() {
			return itemsRequired;
		}

		public int getLevelRequired() {
			return levelRequired;
		}

		public Item getProducedBar() {
			return producedBar;
		}

		public double getExperience() {
			return experience;
		}

		public int getSkillType() {
			return skillType;
		}
	}

	public SmeltingBar bar;
	public Player player;
	public int ticks;

	public EnchantingBolts(SmeltingBar bar, Player player, int ticks) {
		this.player = player;
		this.bar = bar;
		this.ticks = ticks;
	}

	@Override
	public boolean start(Player player) {
		if (bar == null || player == null) {
			return false;
		}
		if (!player.getInventory().containsItemToolBelt(bar.getItemsRequired()[0].getId(), bar.getItemsRequired()[0].getAmount())) {
			player.getPackets().sendGameMessage("You need " + bar.getItemsRequired()[0].getDefinitions().getName() + " to enchant some " + bar.getProducedBar().getDefinitions().getName() + ".");
			return false;
		}
		if (bar.getItemsRequired().length > 1) {
			if (!player.getInventory().containsItemToolBelt(bar.getItemsRequired()[1].getId(), bar.getItemsRequired()[1].getAmount())) {
				player.getPackets().sendGameMessage("You need " + bar.getItemsRequired()[1].getDefinitions().getName() + " to enchant some " + bar.getProducedBar().getDefinitions().getName() + ".");
				return false;
			}
		}
		if (player.getSkills().getLevel(bar.getSkillType()) < bar.getLevelRequired()) {
			player.getPackets().sendGameMessage("You need a Magic level of at least " + bar.getLevelRequired() + " to enchant " + bar.getProducedBar().getDefinitions().getName());
			return false;
		}
		player.getPackets().sendGameMessage("You weave a magical sign to enchant some " + bar.getProducedBar().getDefinitions().getName().toLowerCase().replace("", "") + ".", true);
		return true;
	}

	@Override
	public boolean process(Player player) {
		if (bar == null || player == null) {
			return false;
		}
		if (!player.getInventory().containsItemToolBelt(bar.getItemsRequired()[0].getId(), bar.getItemsRequired()[0].getAmount())) {
			player.getPackets().sendGameMessage("You need " + bar.getItemsRequired()[0].getDefinitions().getName() + " to enchant some " + bar.getProducedBar().getDefinitions().getName() + ".");
			return false;
		}
		if (bar.getItemsRequired().length > 1) {
			if (!player.getInventory().containsItemToolBelt(bar.getItemsRequired()[1].getId(), bar.getItemsRequired()[1].getAmount())) {
				player.getPackets().sendGameMessage("You need " + bar.getItemsRequired()[1].getDefinitions().getName() + " to enchant some " + bar.getProducedBar().getDefinitions().getName() + ".");
				return false;
			}
		}
		if (player.getSkills().getLevel(bar.getSkillType()) < bar.getLevelRequired()) {
			player.getPackets().sendGameMessage("You need a Magc level of at least " + bar.getLevelRequired() + " to enchant " + bar.getProducedBar().getDefinitions().getName());
			return false;
		}
		return true;
	}

	public boolean isSuccessFull(Player player) {
		if (bar == SmeltingBar.IRON) {
			if (player.getEquipment().getItem(Equipment.SLOT_RING) != null && player.getEquipment().getItem(Equipment.SLOT_RING).getId() == 2568) {
				return true;
			} else {
				return Utils.getRandom(100) <= (player.getSkills().getLevel(bar.getSkillType()) >= 45 ? 80 : 50);
			}
		}
		return true;
	}

	@Override
	public int processWithDelay(Player player) {
		ticks--;
		player.setNextAnimation(new Animation(4462));
		player.setNextGraphics(new Graphics(759));
		double xp = bar.getExperience();
		if (bar == SmeltingBar.GOLD && player.getEquipment().getGlovesId() == 776)
			xp *= 2;
		player.getSkills().addXp(bar.getSkillType(), xp);
		for (Item required : bar.getItemsRequired()) {
			if (required.getId() == 4 || required.getId() == 2976 || required.getId() == 1594 || required.getId() == 1599 || required.getId() == 5523)
				continue;
			player.getInventory().deleteItem(required.getId(), required.getAmount());
		}
		if (isSuccessFull(player)) {
			int amount = bar.getProducedBar().getAmount();
			if (bar.getProducedBar().getDefinitions().isStackable())
				amount *= Settings.getCraftRate();
			if (bar == SmeltingBar.MOLTEN_GLASS)
				player.getInventory().addItem(new Item(1925));
			player.getInventory().addItem(bar.getProducedBar().getId(), amount);
			player.getPackets().sendGameMessage("You enchant some " + bar.getProducedBar().getDefinitions().getName().toLowerCase().replace(" bar", "") + ".", true);
		} else {
			player.getPackets().sendGameMessage("You enchant a little too hard and break the bolts.", true);
		}
		if (ticks > 0) {
			return 1;
		}
		return -1;
	}

	@Override
	public void stop(Player player) {
		setActionDelay(player, 3);
	}
}