package sso;

import java.sql.*;

public class Trap extends Immobile
{
	protected boolean	triggered;			// true when trap triggered
	protected boolean	disarmed;			// true when trap inactive
	protected int		detectDifficulty;	// penalty to detect this
	protected int		disarmDifficulty;	// penalty to disarm this
	protected String	effectClassname;	// what happens when this goes off	
	protected GameObject	host;			// host object

	/**
	 *  Protected constructor. Use factory methods instead.
	 */
	protected Trap()
	{
		super();
	}

	/**
	 *  Factory method to get a new trap object.
	 */
	public static Trap createTrap()
	{
		Trap t = new Trap();
		t.init();
		return t;
	}

	/**
	 *  Initializer for new trap objects.
	 */
	protected void init()
	{
		triggered = false;
		disarmed = false;
		detectDifficulty = 10;
		disarmDifficulty = 10;
		effectClassname = "sso.effect.MildPoison";
		host = null;
	}

	/**
	 *  Has the trap been triggered?
	 */
	public boolean isTriggered()
	{
		return triggered;
	}

	/**
	 *  Set the trap triggered status.
	 */
	public void setTriggered(boolean status)
	{
		triggered = status;

		propChanged("triggered");
	}

	/**
	 *  Is the trap armed?
	 */
	public boolean isArmed()
	{
		return !disarmed;
	}

	/**
	 *  Arm the trap.
	 */
	public void arm()
	{
		disarmed = false;

		propChanged("disarmed");
	}

	/**
	 *  Disarm the trap.
	 */
	public void disarm()
	{
		disarmed = true;

		propChanged("disarmed");
	}

	/**
	 *  Get the detection difficulty
	 */
	public int getDetectDifficulty()
	{
		return detectDifficulty;
	}

	/**
	 *  Set the detection difficulty
	 */
	public void setDetectDifficulty(int diff)
	{
		detectDifficulty = diff;

		propChanged("detectDifficulty");
	}

	/**
	 *  Get the disarm difficulty
	 */
	public int getDisarmDifficulty()
	{
		return disarmDifficulty;
	}

	/**
	 *  Set the disarm difficulty
	 */
	public void setDisarmDifficulty(int diff)
	{
		disarmDifficulty = diff;

		propChanged("disarmDifficulty");
	}

	/**
	 *  Get the efffect classname
	 */
	public String getEffectClassname()
	{
		return effectClassname;
	}

	/**
	 *  Set the effect classname
	 */
	public void setEffectClassname(String effect)
	{
		effectClassname = effect;

		propChanged("effectClassname");
	}

	/**
	 *  Get the trap host
	 */
	public GameObject getHost()
	{
		return host;
	}

	/**
	 *  Set the trap host
	 */
	public void setHost(GameObject host)
	{
		this.host = host;

		propChanged("host");
	}

	/**
	 *  Trigger the trap
	 */
	public void trigger(PC victim)
	{
		if (!disarmed)
		{
			try
			{
				Effect eff = (Effect)Class.forName(effectClassname).newInstance();
				eff.init();
				
				eff.addVictim(victim);

				eff.start();
			}
			catch (Exception e)
			{
				System.err.println("Error trying to create new effect (" +
				effectClassname + ") for trap (" + id + "): " +
				e.getMessage());
			}

			triggered = true;
		}
	}


	/**
	 *  Create the rows needed to store this trap
	 */
	protected void createRows()
	{
		super.createRows();

		Statement stmt = null;

		try
		{
			stmt = Persistant.getStatement();

			stmt.executeUpdate("INSERT INTO Trap (trap_id, host_id, triggered, disarmed, detect_diff, disarm_diff, effect_class) SET (" + id + ", -1, 0, 0, 10, 10, 'sso.effect.MildPoison')");

			stmt.close();
			stmt = null;
		}
		catch (SQLException e)
		{
			System.err.println("Trying to create rows for Trap (" + id + "): "
			+ e.getMessage());
		}

		if (stmt != null)
		{
			try
			{
				stmt.close();
			}
			catch (SQLException e)
			{
				//
			}

			stmt = null;
		}
	}

	/**
	 *  Store the trap
	 */
	public void store()
	{
		super.store();
	
		Statement stmt = null;
		
		try
		{
			stmt = Persistant.getStatement();

			stmt.executeUpdate("UPDATE Trap SET host_id=" + (host != null ?  String.valueOf(host.getID()) : "-1") + ", triggered=" + (triggered ? "1" : "0") + ", disarmed=" + (disarmed ? "1" : "0") + ", detect_diff=" + detectDifficulty + ", disarm_diff=" + disarmDifficulty + ", effect_class='" + effectClassname + "' WHERE trap_id=" + id);

			stmt.close();
			stmt = null;
		}
		catch (SQLException e)
		{
			System.err.println("Tring to store trap (" + id + "): " +
			e.getMessage());
		}

		if (stmt != null)
		{
			try
			{
				stmt.close();
			}
			catch (SQLException e)
			{
				//
			}

			stmt = null;
		}
	}

	/**
	 *  Load a trap object
	 */
	public static Trap loadTrap(int id)
	{
		Trap t = null;

		if (Registry.isLoaded(id))
		{
			t = (Trap)Registry.get(id);
		}
		else
		{
			t = new Trap();
			t.id = id;
			t.load();
		}

		return t;
	}

	/**
	 *  Load the data from the database into a Trap object
	 */
	protected void load()
	{
		super.load();

		Statement stmt = null;
		ResultSet rs = null;

		try
		{
			stmt = Persistant.getStatement();

			rs = stmt.executeQuery("SELECT * FROM Trap WHERE trap_id=" + id);

			while (rs.next())
			{
				host = GameObject.loadGameObject(rs.getInt("host_id"));
				triggered = rs.getInt("triggered") != 0;
				disarmed = rs.getInt("disarmed") != 0;
				detectDifficulty = rs.getInt("detect_diff");
				disarmDifficulty = rs.getInt("disarm_diff");
				effectClassname = rs.getString("effect_class");
			}

			rs.close();
			rs = null;

			stmt.close();
			stmt = null;
		}
		catch (SQLException e)
		{
			System.err.println("Trying to load trap (" + id + "): " +
			e.getMessage());
		}

		if (rs != null)
		{
			try
			{
				rs.close();
			}
			catch (SQLException e)
			{
				//
			}

			rs = null;
		}

		if (stmt != null)
		{
			try
			{
				stmt.close();
			}
			catch (SQLException e)
			{	
				//
			}

			stmt = null;
		}
	}

	/**
	 *  Unregister the Trap from the database
	 */
	public void unregister()
	{
		Statement stmt = null;

		try
		{
			stmt = Persistant.getStatement();

			stmt.executeUpdate("DELETE FROM Trap WHERE trap_id=" + id);

			stmt.close();
			stmt = null;
		}
		catch (SQLException e)
		{
			System.err.println("Trying to unregister trap (" + id + "): " +
			e.getMessage());
		}

		if (stmt != null)
		{
			try
			{
				stmt.close();
			}
			catch (SQLException e)
			{
				//
			}

			stmt = null;
		}

		super.unregister();
	}

	// Tester
	public static void main(String [] args)
	{
		Trap t = Trap.createTrap();
		t.store();
		t = Trap.loadTrap(t.getID());
		t.unregister();
	}
}			
