package com.rs.game.player.actions;

import com.rs.Settings;
import com.rs.game.item.Item;
import com.rs.game.npc.NPC;
import com.rs.game.player.Equipment;
import com.rs.game.player.Player;
import com.rs.game.player.Skills;
import com.rs.utils.Utils;

public class Tanning extends Action {

	public enum SmeltingBar {
	    
	    	LEATHER(4, 9, new Item[] { new Item(1739) }, new Item(1741), Skills.CRAFTING),
		    
		HARD_LEATHER(7, 17, new Item[] { new Item(1739), new Item(995, 3) }, new Item(1743), Skills.CRAFTING),
	    
		SNAKESKIN(80, 50, new Item[] { new Item(6287), new Item(995, 15) }, new Item(6289), Skills.CRAFTING),
		
		SNAKESKIN_TEMPLE_TREK(80, 50, new Item[] { new Item(7801), new Item(995, 20) }, new Item(25611), Skills.CRAFTING),
		
		GREEN_DRAGON_LEATHER(89, 150, new Item[] { new Item(1753), new Item(995, 20) }, new Item(1745), Skills.CRAFTING),
		
		BLUE_DRAGON_LEATHER(89, 150, new Item[] { new Item(1751), new Item(995, 20) }, new Item(2505), Skills.CRAFTING),
		
		RED_DRAGON_LEATHER(89, 150, new Item[] { new Item(1749), new Item(995, 20) }, new Item(2507), Skills.CRAFTING),
		
		BLACK_DRAGON_LEATHER(89, 150, new Item[] { new Item(1747), new Item(995, 20) }, new Item(2509), Skills.CRAFTING),
		
		ROYAL_DRAGON_LEATHER(89, 150, new Item[] { new Item(24372), new Item(995, 20) }, new Item(24374), Skills.CRAFTING),
		
		//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 NPC npc;
	public int ticks;

	public Tanning(SmeltingBar bar, NPC npc, int ticks) {
		this.npc = npc;
		this.bar = bar;
		this.ticks = ticks;
	}

	@Override
	public boolean start(Player player) {
		if (bar == null || player == null || npc == 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 create a " + 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 create a " + bar.getProducedBar().getDefinitions().getName() + ".");
				return false;
			}
		}
		if (player.getSkills().getLevel(bar.getSkillType()) < bar.getLevelRequired()) {
			player.getPackets().sendGameMessage("You need a Crafting level of at least " + bar.getLevelRequired() + " to tan some " + bar.getProducedBar().getDefinitions().getName());
			return false;
		}
		player.getPackets().sendGameMessage("You give the tanner some hides " + bar.getProducedBar().getDefinitions().getName().toLowerCase().replace("", "") + ".", true);
		return true;
	}

	@Override
	public boolean process(Player player) {
		if (bar == null || player == null || npc == 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 tan 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 tan some " + bar.getProducedBar().getDefinitions().getName() + ".");
				return false;
			}
		}
		if (player.getSkills().getLevel(bar.getSkillType()) < bar.getLevelRequired()) {
			player.getPackets().sendGameMessage("You need a Crafting level of at least " + bar.getLevelRequired() + " to tan some " + 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--;
		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 retrieve some tanned " + bar.getProducedBar().getDefinitions().getName().toLowerCase().replace(" bar", "") + ".", true);
		} else {
			player.getPackets().sendGameMessage("The leather is too impure and you fail to refine it.", true);
		}
		if (ticks > 0) {
			return 1;
		}
		return -1;
	}

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