package core.game.interaction.object;

import core.cache.def.impl.ItemDefinition;
import core.cache.def.impl.SceneryDefinition;
import core.plugin.Initializable;
import core.game.content.quest.tutorials.tutorialisland.TutorialSession;
import core.game.content.quest.tutorials.tutorialisland.TutorialStage;
import core.game.node.entity.skill.gather.SkillingResource;
import core.game.interaction.OptionHandler;
import core.game.node.Node;
import core.game.node.entity.player.Player;
import core.game.node.object.Scenery;
import core.game.system.task.Pulse;
import rs09.game.world.GameWorld;
import core.plugin.Plugin;

/**
 * Handles the prospecting of an ore.
 * @author 'Vexia
 */
@Initializable
public class ProspectOrePlugin extends OptionHandler {

	@Override
	public boolean handle(final Player player, Node node, String option) {
		final Scenery object = (Scenery) node;
		int tut_stage = TutorialSession.getExtension(player).getStage();
		if (tut_stage == 31 && object.getId() == 3043) {
			GameWorld.getPulser().submit(new Pulse(1) {
				int count = 0;

				@Override
				public boolean pulse() {
					if (count == 1) {
						TutorialStage.load(player, 32, false);
					} else if (count >= 2) {
						TutorialStage.load(player, 33, false);
						return true;
					}
					count++;
					return false;
				}
			});
		}
		if (tut_stage == 33 && object.getId() == 3042) {
			GameWorld.getPulser().submit(new Pulse(1) {
				int count = 0;

				@Override
				public boolean pulse() {
					if (count == 1) {
						TutorialStage.load(player, 34, false);
						return true;
					}
					count++;
					return false;
				}
			});
		}
		final SkillingResource rock = SkillingResource.forId(object.getId());
		if (rock == null) {
			player.getPacketDispatch().sendMessage("There is no ore currently available in this rock.");
			return true;
		}
		player.getPacketDispatch().sendMessage("You examine the rock for ores...");
		player.getPulseManager().run(new Pulse(3, player, object) {
			@Override
			public boolean pulse() {
				player.getPacketDispatch().sendMessage("This rock contains " + ItemDefinition.forId(rock.getReward()).getName().toLowerCase() + ".");
				return true;
			}
		});
		return true;
	}

	@Override
	public Plugin<Object> newInstance(Object arg) throws Throwable {
		SceneryDefinition.setOptionHandler("prospect", this);
		return this;
	}

}
