package com.rs.game.player.actions;

import com.rs.game.Animation;
import com.rs.game.item.Item;
import com.rs.game.player.Player;
import com.rs.game.player.Skills;

public class GlassBlowing extends Action {

	public enum SmeltingBar {
		
		BEER_GLASS(1, 17.5, new Item[] { new Item(1775) }, new Item(1919), Skills.CRAFTING),
		
		CANDLE_LANTERN(4, 19, new Item[] { new Item(1775) }, new Item(4527), Skills.CRAFTING),
		
		EMPTY_OIL_LAMP(12, 25, new Item[] { new Item(1775) }, new Item(4525), Skills.CRAFTING),
		
		VIAL(33, 35, new Item[] { new Item(1775) }, new Item(229), Skills.CRAFTING),
		
		FISHBOWL(42, 42.5, new Item[] { new Item(1775) }, new Item(6667), Skills.CRAFTING),
		
		UNPOWERED_ORB(46, 52.5, new Item[] { new Item(1775) }, new Item(567), Skills.CRAFTING),
		
		LANTERN_LENS(49, 55, new Item[] { new Item(1775) }, new Item(4542), Skills.CRAFTING),
		
		EMPTY_LIGHT_ORB(87, 70, new Item[] { new Item(1775) }, new Item(10980), Skills.CRAFTING),
		
		POTION_FLASK(89, 100, new Item[] { new Item(23193) }, new Item(23191), Skills.CRAFTING),
		
		CRYSTAL_FLASK(89, 100, new Item[] { new Item(32845) }, new Item(32843), Skills.CRAFTING),
		
		;

		public static SmeltingBar getBarByProduce(int id) {//SewingLeather
			for(SmeltingBar bar : SmeltingBar.values()) {//SeweingLeather
				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 GlassBlowing(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 blow 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 blow 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 blow some " + bar.getProducedBar().getDefinitions().getName());
			return false;
		}
		player.getPackets().sendGameMessage("You give the glass shit " + 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 blow 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 blow 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 blow some " + bar.getProducedBar().getDefinitions().getName());
			return false;
		}
		return true;
	}

	@Override
	public int processWithDelay(Player player) {
		ticks--;
		double xp = bar.getExperience();
		player.getSkills().addXp(bar.getSkillType(), xp);
		player.setNextAnimation(new Animation(884));
		for (Item required : bar.getItemsRequired()) {
			player.getInventory().deleteItem(required.getId(), required.getAmount());
		}
			int amount = bar.getProducedBar().getAmount();
			player.getInventory().addItem(bar.getProducedBar().getId(), amount);
			player.getPackets().sendGameMessage("You blow a " + bar.getProducedBar().getDefinitions().getName());
			//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);
	}
}