/**
 * Provides the view for the Sermons section
 */
package com.sharefaith.churchapp.views;

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

import com.codename1.ui.Container;
import com.codename1.ui.Display;
import com.codename1.ui.Font;
import com.codename1.ui.Image;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.ui.list.DefaultListModel;
import com.codename1.ui.list.MultiList;
import com.sharefaith.churchapp.SFUtil;
import com.sharefaith.churchapp.model.ChurchAppData;
import com.sharefaith.churchapp.model.SermonsData;
import com.sharefaith.churchapp.ui.SFImageLabel;
import com.sharefaith.churchapp.views.actions.SermonsSeriesAction;

public class SermonsView  extends TabViews
{
	/**
	 * A tag to identify this section in the UI and sermons in the database
	 */
	public static final String TAG = new String( "sermons" );
	
	/**
	 * An instance of the data getter
	 */
	public ChurchAppData cad;
	
	/**
	 * Configuration options
	 */
	HashMap<String,String> config;
	
	/**
	 * An instance of the image label to use
	 */
	public SFImageLabel imageLabel;
	
	/**
	 * Class constructor.
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public SermonsView()
	{
		this.cad = new ChurchAppData( false );
	}
	
	
	
	/**
	 * Provides content for the view.
	 * 
	 * This should be the only thing that is called to get the overall view and may enveloper other components
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public Container contentComponent()
	{
		Container returnContainer = new Container( new BorderLayout() );
		
		this.imageLabel = new SFImageLabel();
		try
		{
			Image img = Image.createImage( "/banner_2048_sermons.png" );
			
			this.imageLabel.setFullWidthImage( img );
			
		}
		catch( IOException e )
		{
			e.printStackTrace();
		}
		
		// Get the raw data items
		SermonsData data = new SermonsData();
		Vector<Hashtable<String,Object>> listElements = data.getSeriesListItems();
		// Add those items to a model
		DefaultListModel<Hashtable<String,Object>> model = new DefaultListModel<Hashtable<String,Object>>( listElements );
		// Send the model to a List
		MultiList itemList = new MultiList();
		itemList.setModel( model );
		
		/*
		 * If we want to switch to a ContainerList:
		// Get the raw data items
		SermonsData data = new SermonsData();
		Vector<Hashtable<String,Object>> listElements = data.getSeriesListItems();
		// Add those items to a model
		DefaultListModel<Hashtable<String,Object>> model = new DefaultListModel<Hashtable<String,Object>>( listElements );
		// Send the model to a List
		// In this case, we want each list item to be a BoxLayout container
		ContainerList itemList = new ContainerList( new BoxLayout( BoxLayout.Y_AXIS ), model );
		// Show the List how to render
		itemList.setRenderer( new SermonsSeriesRenderer() );
				*/
		
		final MultiList finalList = itemList;
		final Container finalContainer = returnContainer;
		itemList.addActionListener(
			new ActionListener()
			{
				public void actionPerformed( ActionEvent event )
				{
					SermonsSeriesAction action = new SermonsSeriesAction( finalContainer, finalList.getSelectedItem() );
					action.perform();
				}
			}
		);
		
		Font listFont = Font.createTrueTypeFont( "Droid Sans", "DroidSans.ttf" );
		listFont = listFont.derive( 10, Font.STYLE_PLAIN );
		itemList.getStyle().setFont( listFont );
		
		// data.getSeriesListItems() will always return at least one element
		// If there is only 1 item & the item at index 1 has a 0 id, there are no records to show
		if( 2 > listElements.size() )
		{
			Hashtable<String,Object> values = listElements.elementAt( 0 );
			int idValue = Integer.valueOf( (Integer)values.get( "id" ) ).intValue();
			if( 1 > idValue )
			{
				itemList.setFocusable( false );
			}
		}
		
		returnContainer.addComponent( BorderLayout.NORTH, this.imageLabel );
		returnContainer.addComponent( BorderLayout.CENTER, itemList );
		
		return returnContainer;
	}
	
	/**
	 * Provides content for the view.
	 * 
	 * This should be the only thing that is called to get the overall view and may enveloper other components
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public Container singleSermonComponent()
	{
		Container returnContainer = new Container( new BoxLayout( BoxLayout.Y_AXIS ) );
		returnContainer.setScrollable( true );
		
		return returnContainer;
	}
	
	/**
	 * Provides icon file locations for this tab item
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public HashMap<String,String> tabIconFiles()
	{
		return this.tabIconFilesByTag( SermonsView.TAG );
	}
	
	/**
	 * Refresh this item's config options
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public HashMap<String,String> configValues()
	{
		if( this.config == null )
		{
			this.config = this.configValuesByTag( SermonsView.TAG );
		}
		return this.config;
	}
	
	/**
	 * Handle device rotation
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public void rotate()
	{
		SFUtil.p( "Rotate sermons" );
		this.imageLabel.resizeToFullWidth();
	}

}
