package sso;

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

public abstract class Scenery extends Immobile
{
	protected boolean passable;
	protected boolean propEvents;

	/**
	 *  Protected constructor. Use a factory method, instead.
	 */
	protected Scenery()
	{
		super();
	}

	
	/**
	 *  Initializer.
	 */
	protected void init()
	{
		super.init();

		passable = false;
	}

	/**
	 *  Is this passable by PCs?
	 */
	public boolean isPassable()
	{
		return passable;
	}

	/**
	 *  Set passable.
	 */
	public void setPassable(boolean passable)
	{
		this.passable = passable;

		propChanged("passable");
	}

	/**
 	 *  Will this propagate events?
	 */
	public boolean doesPropagate()
	{
		return propEvents;
	}

	/**
	 *  Set propagate events flag.
	 */
	public void setPropagate(boolean prop)
	{
		propEvents = prop;

		propChanged("passable");
	}
	

	/**
	 *  Allow moves into this if this is passable.
	 */
	public boolean accept(GameObject go)
	{
		boolean acc = false;
	
		if (isPassable())
		{
			acc = super.accept(go);
		}
		
		return acc;
	}


	// DATABASE METHODS

	/**
	 *  Create the rows needed to store the Passable info.
	 */
	protected void createRows()
	{
		super.createRows();
	
		Statement stmt = null;
		
		try
		{
			stmt = Persistant.getStatement();

			stmt.executeUpdate("INSERT INTO Scenery (scenery_id, passable, prop_events) VALUES (" + id + ", 0, 0)");

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

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

			stmt = null;
		}
	}

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

			stmt.executeUpdate("UPDATE Scenery SET passable=" + (passable ? 1 : 0) + ", prop_events=" + (propEvents ? 1 : 0) + " WHERE scenery_id=" + id);

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

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

			stmt = null;
		}
	}

	/**
	 *  Load the scenery data.
	 */
	protected void load()
	{
		super.load();

		Statement stmt = null;
		ResultSet rs = null;

		try
		{
			stmt = Persistant.getStatement();

			rs = stmt.executeQuery("SELECT * FROM Scenery WHERE scenery_id=" +
			id);

			while (rs.next())
			{
				passable = rs.getInt("passable") != 0;
				propEvents = rs.getInt("prop_events") != 0;
			}

			rs.close();
			rs = null;

			stmt.close();
			stmt = null;
		}
		catch (SQLException e)
		{
			System.err.println("Trying to load Scenery (" + 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 Scenery from the database.
	 */
	public void unregister()
	{
		Statement stmt = null;

		try
		{
			stmt = Persistant.getStatement();

			stmt.executeUpdate("DELETE FROM Scenery WHERE scenery_id=" + id);

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

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

			stmt = null;
		}

		super.unregister();
	}
}
			
			

