package sso;

import sso.event.*;
import java.sql.*;
import java.util.*;

public class Resistance extends Persistant
{
	protected int		resistance_type;
	protected int		resistance_percentage;
	protected Vector	propertyChangeListeners;

	// RESISTANCE TYPES
	public static final int	PHYSICAL	= 0x01;
	public static final int	PSIONIC		= 0x02;
	public static final int	MAGIC		= 0x04;
	public static final int	BLIND		= 0x08;
	public static final int	DISEASE		= 0x10;
	public static final int	PARALYZE	= 0x20;
	public static final int	FIRE		= 0x40;
	public static final int	COLD		= 0x80;
	public static final int	LIGHTNING	= 0x100; 

	/**
	 *  Constructor. Only called by factory methods.
	 */
	protected Resistance()
	{
		super();
		propertyChangeListeners = new Vector();
	}

	/**
	 *  Factory method. Create a new Resistance object.
	 */
	public static Resistance createResistance()
	{
		Resistance ro = new Resistance();

		ro.init();

		return ro;
	}

	/**
	 *  Initialization for factory method.
	 */
 	protected void init()
	{
		super.init();

		resistance_type = 0;
		resistance_percentage = 0;
	}

	/**
	 *  Gets resistance type.
	 */
	public int getResistanceType()
	{
		return resistance_type;
	}

	/**
	 *  Sets resistance type.
	 */
	public void setResistanceType(int resistance_type)
	{
		this.resistance_type = resistance_type;
		propChange("resistance_type");
	}

	/**
	 *  Gets resistance percentage.
	 */
	public int getResistancePercentage()
	{
		return resistance_percentage;
	}

	/**
	 *  Gets resistance percentage.
	 */
	public void setResistancePercentage(int perc)
	{
		resistance_percentage = perc;
		propChange("resistance_percentage");
	}

	/**
	 *  Create a database entry for this object.
	 */
	protected void createRows()
	{
		super.createRows();

		Statement stmt = null;

		try
		{
			stmt = Persistant.getStatement();

			stmt.executeUpdate("INSERT INTO Resistance (resistance_id, resistance_type, resistance_perc) VALUES (" + id + ", 0, 0)");

			stmt.close();
			stmt = null;
		}
		catch (SQLException e)
		{
			System.err.println("Trying to add a resistance row (" + id + "): " + e.getMessage());
		}

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

			stmt = null;
		}
	}

	/**
	 *  Save the Resistance object.
	 */
	public void Store()
	{
		Statement stmt = null;

		try
		{
			stmt = Persistant.getStatement();

			stmt.executeUpdate("UPDATE Resistance SET resistance_type=" + resistance_type + ", resistance_perc=" + resistance_percentage + " WHERE resistance_id=" + id);

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

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

			stmt = null;
		}

		super.store();
	}

	/**
	 *  Load the resistance object.
	 */
	public static Resistance loadResistance(int id)
	{
		Resistance r = null;

		if (Registry.isLoaded(id))
		{
			r = (Resistance)Registry.get(id);
		}
		else
		{
			r = new Resistance();
			r.id = id;
			r.load();

		}

		return r;
	}

	/**
	 *  Load the resistance object. Internals.
	 */
	protected void load()
	{
		super.load();

		Statement stmt = null;
		ResultSet rs = null;

		try
		{
			stmt = Persistant.getStatement();

			rs = stmt.executeQuery("SELECT * FROM Resistance WHERE resistance_id=" + getID());

			while (rs.next())
			{
				resistance_type = rs.getInt("resistance_type");
				resistance_percentage = rs.getInt("resistance_perc");
			}

			rs.close();
			rs = null;

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

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

			rs = null;
		}

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

			stmt = null;
		}
	}

	/**
	 *  Remove resistance from database.
	 */
	public void unregister()
	{
		Statement stmt = null;
		ResultSet rs = null;
		
		try
		{
			stmt = Persistant.getStatement();

			// first, remove any lingering references in GameObjects
			rs = stmt.executeQuery("SELECT gameobject_id from GameObject_Resistances WHERE resistance_id=" + id);

			GameObject go;

			while (rs.next())
			{
				go = GameObject.loadGameObject(rs.getInt("gameobject_id"));
				go.removeResistance(this);
			}

			go = null;

			rs.close();
			rs = null;

			// clean out references
			stmt.executeUpdate("DELETE FROM GameObject_Resistances WHERE resistance_id=" + id);

			// now, remove this Resistance
			stmt.executeUpdate("DELETE FROM Resistance WHERE resistance_id=" + id);

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

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

			rs = null;
		}

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

	// EVENTS
	/**
	 * Add a property change listener.
	 */
	public void addPropertyChangeListener(PropertyChangeListener pl)
	{
		propertyChangeListeners.addElement(pl);
	}

	/**
	 *  Remove a listener.
	 */
	public void removePropertyChangeListener(PropertyChangeListener pl)
	{
		propertyChangeListeners.removeElement(pl);
	}

	/**
	 *  Fire a property change event.
	 */
	protected void propChange(String prop)
	{
		PropertyChangeEvent pe = new PropertyChangeEvent(this, prop);

		Vector vpl = (Vector)propertyChangeListeners.clone();

		for (int i = 0; i < vpl.size(); i++)
		{
			((PropertyChangeListener)vpl.elementAt(i)).notifyPropertyChange(pe);
		}
	}
}

