/**
 * Data getter for posts
 */
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.PostsView;

public class PostsData
{
	/**
	 * An instance of the database
	 */
	public ChurchAppData cad;
	
	/**
	 * Class constructor.
	 * 
	 * @since 20140317
	 * @author costmo
	 */
	public PostsData()
	{
		this.cad = new ChurchAppData( false );
	}
	
	/**
	 * Gets the posts list as a Vector that can be used as a list model
	 * 
	 * @since 20140317
	 * @author costmo
	 */
	public Vector<Hashtable<String,Object>> getListItems()
	{
		Vector<Hashtable<String,Object>> returnVector = new Vector<Hashtable<String,Object>>();
		
		// parent_id = 0 have the title for posts content. related section_content_meta holds the posts
		String sql = new String(
				"SELECT		_id, content "
			+ 	"FROM		section_content "
			+ 	"WHERE		ondevice_section_id = (SELECT _id FROM ondevice_sections WHERE sf_tag = '" + PostsView.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" );
			
			// 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 );
				
				String article = this.cad.getValueForMetaKey( "section_content_meta", "section_content_id", sectionId, "article" );
				
				// 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 );
				values.put( "Line3", article.substring( 0, 250 ) );
				values.put( "emblem", emblemImg );
				
				returnVector.addElement( values );
			}
		}
		catch( IOException e )
		{
			e.printStackTrace();
			throw new RuntimeException( e.getMessage() );
		}
		
		// TODO: Add icon image
		
		// Show a single informative row if there are no posts' 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 posts." );
			values.put( "Line1", "There are no posts." );
			
			try
			{
				Resources res = Resources.open( "/theme.res" );
				Image noEmblemImg = res.getImage( "table_emblem_none.png" );
				values.put( "emblem", noEmblemImg );
				
			}
			catch( IOException e )
			{
				e.printStackTrace();
				throw new RuntimeException( e.getMessage() );
			}
			
			returnVector.addElement( values );
		} // if( returnVector.size() < 1 )
		
		return returnVector;
	} // getListItems
}
