/**
 * Provides the view for the Posts 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.Component;
import com.codename1.ui.Container;
import com.codename1.ui.Font;
import com.codename1.ui.Image;
import com.codename1.ui.Label;
import com.codename1.ui.TextArea;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.ui.list.DefaultListModel;
import com.codename1.ui.list.MultiList;
import com.codename1.ui.plaf.Style;
import com.sharefaith.churchapp.ChurchApp;
import com.sharefaith.churchapp.SFUtil;
import com.sharefaith.churchapp.model.*;
import com.sharefaith.churchapp.views.actions.PostsAction;
import com.sharefaith.churchapp.views.actions.SermonsSeriesAction;

public class PostsView extends TabViews
{
	/**
	 * A tag to identify this section in the UI
	 */
	public static final String TAG = new String( "posts" );
	
	/**
	 * An instance of the data getter
	 */
	public ChurchAppData cad;
	
	/**
	 * Configuration options
	 */
	public HashMap<String,String> config;
	
	/**
	 * Class constructor.
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public PostsView()
	{
		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 BoxLayout( BoxLayout.Y_AXIS ) );
		
		// Add the top image
		Label lbl = new Label();
		try
		{
			Image img = Image.createImage( "/banner_2048_posts.png" );
			
			Style imgStyle = new Style();
			imgStyle.setPadding( 0, 0, 0, 0 );
			imgStyle.setMargin( 0, 0, 0, 0 );
			imgStyle.setAlignment( Component.CENTER );
			
			lbl.setSelectedStyle( imgStyle );
			lbl.setUnselectedStyle( imgStyle );
			int deviceWidth = Integer.valueOf( ChurchApp.deviceInfo().get( "width" ) ).intValue();
			lbl.setIcon( img.scaledWidth( deviceWidth ) );
			
		}
		catch( IOException e )
		{
			e.printStackTrace();
		}
		
		// Get the raw data items
		PostsData data = new PostsData();
		Vector<Hashtable<String,Object>> listElements = data.getListItems();
		// 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 );
		
		final MultiList finalList = itemList;
		final Container finalContainer = returnContainer;
		itemList.addActionListener(
			new ActionListener()
			{
				public void actionPerformed( ActionEvent event )
				{
					PostsAction action = new PostsAction( 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( lbl );
		returnContainer.addComponent( itemList );
		
		returnContainer.forceRevalidate();
		
		return returnContainer;
	}
	
	/**
	 * Provides icon file locations for this tab item
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public HashMap<String,String> tabIconFiles()
	{
		return this.tabIconFilesByTag( PostsView.TAG );
	}
	
	/**
	 * Refresh this item's config options
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public HashMap<String,String> configValues()
	{
		if( this.config == null )
		{
			this.config = this.configValuesByTag( PostsView.TAG );
		}
		return this.config;
	}
	
	/**
	 * Handle device rotation
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public void rotate()
	{
		SFUtil.p( "Rotate posts" );
	}
}
