/**
 * Copyright © 2010, Res Interactive, LLC. All Rights Reserved.
 */
package com.tootsville.env;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.GregorianCalendar;

import org.starhope.appius.except.NotFoundException;
import org.starhope.appius.game.AppiusClaudiusCaecus;
import org.starhope.appius.game.RunCommands;
import org.starhope.appius.game.Zone;
import org.starhope.appius.game.actions.AutomaticDoor;
import org.starhope.appius.util.AppiusConfig;
import org.starhope.util.LibMisc;

import com.tootsville.actions.FountainWishDamage;
import com.tootsville.actions.ShadeServices;

/**
 * WRITEME: Document this type.
 * 
 * @author brpocock@star-hope.org
 */
public class TootsvilleRC implements RunCommands {
	
	/**
	 * WRITEME: Document this type.
	 * 
	 * @author brpocock@star-hope.org
	 */
	private static final class MovieRunner implements Runnable {

		/**
		 * The zone in which to be run
		 */
		private final Zone z;

		/**
		 * WRITEME: Document this constructor brpocock@star-hope.org
		 * 
		 * @param zone the zone to run against
		 */
		public MovieRunner (final Zone zone) {
			z = zone;
		}

		/**
		 * @see java.lang.Runnable#run()
		 */
		@Override
		public void run () {
			if (AppiusClaudiusCaecus.getAllZones ().contains (z)) {
				TootsvilleRC.setMovieTheater (z);
			}
		}
	}

	/**
	 * Set the movie from the database into a zone
	 * @param z zone
	 */
	public static void setMovieTheater (final Zone z) {
		Connection con = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		try {
			con = AppiusConfig.getDatabaseConnection ();
			st = con.prepareStatement ("SELECT title,filename FROM movieListings ORDER BY sequence DESC LIMIT 1");
			rs = st.executeQuery ();
			rs.next ();
			z.getRoom ("tootTheater").setVariable (
					"video1",
					"254~38~280~156~http://www.Tootsville.com/web/images/video/"
							+ rs.getString ("filename"));
			z.getRoom ("tootSquareWest").setVariable (
					"text1",
					"301~211~173~0~20~" + rs.getString ("title")
							+ "~center~Cartoonist");
		} catch (final SQLException e) {
			AppiusClaudiusCaecus.reportBug (e);
		} catch (NotFoundException e) {
			AppiusClaudiusCaecus
					.reportBug (
							"Caught a NotFoundException in TootsvilleRC.newZone ",
							e);
		} finally {
			LibMisc.closeAll (rs, st, con);
		}

		Calendar cal = new GregorianCalendar ();
		while (cal.get (Calendar.DAY_OF_WEEK) != Calendar.FRIDAY) {
			cal.add (Calendar.DAY_OF_YEAR, 1);
		}
		cal.set (Calendar.HOUR_OF_DAY, 20);
		cal.set (Calendar.MINUTE, 55);
		AppiusClaudiusCaecus.getKalendor ().schedule(cal.getTimeInMillis (), new MovieRunner (z));
	}

	/**
	 * @see org.starhope.appius.game.RunCommands#newZone(Zone)
	 */
	@Override
	public void newZone (final Zone z) {
		if (z.getName ().startsWith ("$")) {
			return;
		}
		TootsvilleRC.setMovieTheater (z);
		// new PropsServices ().newZone (z);
	}

	/**
	 * @see org.starhope.appius.game.RunCommands#run()
	 */
	@Override
	public void run () {
		AppiusClaudiusCaecus.blather ("Let's make some Noise!™");
		new AutomaticDoor ().run ();
		new ShadeServices ().run ();
		new FountainWishDamage ().run ();
	}

}
