/**
 * Data getter for sermons
 */
package com.sharefaith.churchapp.model;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;

import com.codename1.db.Cursor;
import com.codename1.db.Row;
import com.codename1.ui.Image;
import com.codename1.ui.util.Resources;
import com.sharefaith.churchapp.views.SermonsView;

public class SermonsData
{
	/**
	 * An instance of the database
	 */
	public ChurchAppData cad;
	
	/**
	 * Class constructor.
	 * 
	 * @since 20140317
	 * @author costmo
	 */
	public SermonsData()
	{
		this.cad = new ChurchAppData( false );
	}
	
	/**
	 * Gets the series list as a Vector that can be used as a list model
	 * 
	 * @since 20140317
	 * @author costmo
	 */
	public Vector<Hashtable<String,Object>> getSeriesListItems()
	{
		Vector<Hashtable<String,Object>> returnVector = new Vector<Hashtable<String,Object>>();
		
		// parent_id > 0 are entries for specific sermons, the parent_id being the _id of the series
		String sql = new String(
				"SELECT		_id, content "
			+ 	"FROM		section_content "
			+ 	"WHERE		ondevice_section_id = (SELECT _id FROM ondevice_sections WHERE sf_tag = '" + SermonsView.TAG + "') AND "
			+ 	"			parent_id = 0 "
			+ 	"ORDER BY	position_index"
		);
		
		try
		{
			Resources res = Resources.open( "/theme.res" );
			// selection emblem
			Image emblemImg = res.getImage( "table_emblem.png" );
			Image noEmblemImg = res.getImage( "table_emblem_none.png" );
			
			// walk the results of the sermon series query
			Cursor cursor = this.cad.db.executeQuery( sql );
			while( cursor.next() )
			{
				Row row = cursor.getRow();
				int sectionId = row.getInteger( 0 );
				String sectionContent = row.getString( 1 );
				
				// create the output for the list model
				Hashtable<String,Object> values = new Hashtable<String,Object>();
				values.put( "id", Integer.valueOf( sectionId ) );
				values.put( "content", sectionContent );
				values.put( "Line1", sectionContent );
				
				// get a count of contained messages
				String innerSql = new String(
						"SELECT		COUNT(*) "
					+ 	"FROM		section_content "
					+ 	"WHERE		ondevice_section_id = (SELECT _id FROM ondevice_sections WHERE sf_tag = '" + SermonsView.TAG + "') AND "
					+ 	"			parent_id = " + sectionId + " "
					+ 	"ORDER BY	position_index"
				);
				
				Cursor innerCursor = this.cad.db.executeQuery( innerSql );
				Row innerRow = innerCursor.getRow();
				int messageCount = innerRow.getInteger( 0 );
				values.put( "messageCount", Integer.valueOf( messageCount ) );
				
				// TODO: Create a container that looks like a button for the icon rather than including so many images.
				// See CN1 demos/SocialBoo ("Followers" theme element)
				
				// If there are more than 49 messages in the series, show the "out of bounds" icon
				if( messageCount > 49 )
				{
					Image iconImg = res.getImage( "icon_sermons_count_none.png" );
					values.put( "icon", iconImg );
					values.put( "emblem", emblemImg );
				}
				else if( messageCount > 0 ) // or if there are more than 0 messages, show the appropriate number
				{
					Image iconImg = res.getImage( "icon_sermons_count_" + messageCount + ".png" );
					values.put( "icon", iconImg );
					values.put( "emblem", emblemImg );
				}
				else // or else make sure the emblem is properly marked
				{
					Image iconImg = res.getImage( "icon_sermons_count_0.png" );
					values.put( "icon", iconImg );
					values.put( "emblem", noEmblemImg );
				}
				
				returnVector.addElement( values );
			}
		}
		catch( IOException e )
		{
			e.printStackTrace();
			throw new RuntimeException( e.getMessage() );
		}
		
		// Show a single informative row if there are no sermon series' to show
		if( returnVector.size() < 1 )
		{
			Hashtable<String,Object> values = new Hashtable<String,Object>();
			values.put( "id", Integer.valueOf( 0 ) );
			values.put( "content", "There are no sermons." );
			values.put( "Line1", "There are no sermons." );
			
			try
			{
				Resources res = Resources.open( "/theme.res" );
				Image iconImg = res.getImage( "icon_sermons_count_none.png" );
				values.put( "icon", iconImg );
			}
			catch( IOException e )
			{
				e.printStackTrace();
				throw new RuntimeException( e.getMessage() );
			}
			
			returnVector.addElement( values );
		} // if( returnVector.size() < 1 )
		
		return returnVector;
	} // getSeriesListItems
	
	
}
