package sso;

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

public class Wall extends Scenery implements Attackable
{
	protected int base_rating;
	protected int cur_rating;
	protected int height;			// in Z units

	/**
	 *  Protected constructor. Use a factory method instead.
	 */
	protected Wall()
	{
		super();
		propEvents = false;	// walls don't propagate events
	}

	/**
	 *  Factory method. Create a new wall.
	 */
	public static Wall createWall()
	{
		Wall w = new Wall();
		w.init();
		return w;
	}

	/**
	 *  Wall initializer.
	 */
	protected void init()
	{
		super.init();

		base_rating = 1000;
		cur_rating = base_rating;

		height = 3;			// Z units
	}

	/**
	 *  Get the current rating
	 */
	public int getRating()
	{
		return cur_rating;
	}

	/**
	 *  Set the current rating
	 */
	protected void setRating(int rating)
	{
		cur_rating = rating;

		// check to see if this has been destroyed
		if (cur_rating > base_rating)
		{
			cur_rating = base_rating;
		}
		else if (cur_rating < 0)
		{
			// destroyed
			// SEE Equipment.setRating() for hints on how to finish
			// implementing this
			unregister();
		}

		propChanged("cur_rating");
	}

	/**
	 *  Get the base rating
	 */
	public int getBaseRating()
	{
		return base_rating;
	}

	/**
	 *  Set the base rating
	 */
	public void setBaseRating(int rating)
	{
		base_rating = rating;

		propChanged("base_rating");
	}

	/**
	 *  Get the height
	 */
	public int getHeight()
	{
		return height;
	}

	/**
	 *  Set the height
	 */
	public void setHeight(int height)
	{
		this.height = height;
	
		propChanged("height");
	}

	/**
	 *  Attack the wall
	 */
	public void attack(Attack attack)
	{
		int result = attack.getResult();
		int dmg = attack.getDamageRoll();
		
		// ignore damage location

		if ((result & Attack.CRITICAL_HIT) == Attack.CRITICAL_HIT)
		{
			// multiple damage by 2
			dmg = dmg * 2;
		}

		// apply damage
		setRating(getRating() - dmg);
	}

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

		Statement stmt = null;

		try
		{
			stmt = Persistant.getStatement();

			stmt.executeUpdate("INSERT INTO Wall (wall_id, base_rating, cur_rating, height) VALUES (" + id + ", 1000, 1000, 3)");

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

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

			stmt = null;
		}
	}

	/**
	 *  Store the state of the Wall object.
	 */
	public void store()
	{
		super.store();

		Statement stmt = null;

		try
		{
			stmt = Persistant.getStatement();

			stmt.executeUpdate("UPDATE Wall SET base_rating=" + base_rating + ", cur_rating=" + cur_rating + ", height=" + height +" WHERE wall_id=" + id);

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

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

			stmt = null;
		}
	}

	/**
	 *  Load a new Wall object
	 */
	public static Wall loadWall(int id)
	{
		Wall w = null;

		if (Registry.isLoaded(id))
		{
			w = (Wall)Registry.get(id);
		}
		else
		{
			w = new Wall();
			w.id = id;
			w.load();
		}

		return w;
	}
	
	/**
	 *  Load the data into a wall object from the db.
	 */
	protected void load()
	{
		super.load();

		Statement stmt = null;
		ResultSet rs = null;

		try
		{
			stmt = Persistant.getStatement();

			rs = stmt.executeQuery("SELECT * FROM Wall WHERE wall_id=" + id);

			while (rs.next())
			{
				base_rating = rs.getInt("base_rating");
				cur_rating = rs.getInt("cur_rating");
				height = rs.getInt("height");
			}

			rs.close();
			rs = null;

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

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

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

	/**
	 *  Unregister a wall.
	 */
	public void unregister()
	{
		Statement stmt = null;

		try
		{
			stmt.executeUpdate("DELETE FROM Wall WHERE wall_id=" + id);

			stmt.close();
			stmt = null;
		}
		catch (SQLException e)
		{
			System.err.println("Trying to unregister Wall (" + id + "): " +
			e.getMessage());
		}
	
		if (stmt != null)
		{	
			try
			{
				stmt.close();
			}
			catch (SQLException e)
			{
				//
			}
		}
		
		super.unregister();
	}

	// handle events
	/**
 	 *  Walls consume all events.
	 */
	public void handleActionEvent(ActionEvent evt)
	{
		// consume all events
		evt.setTTL(0);

		// I know, this may seem unrealistic for low walls...deal
	}
	
	/**
	 *  Tester
	 */
	public static void main(String [] args)
	{
		Wall w = Wall.createWall();

		w.store();
		w = Wall.loadWall(w.getID());

		w.unregister();
	}
}
