/**
 * Defines a factory and the (class) interface for view component classes
 */
package com.sharefaith.churchapp.views;

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

import com.codename1.db.Cursor;
import com.codename1.db.Row;
import com.codename1.ui.Container;
import com.sharefaith.churchapp.model.ChurchAppData;

public abstract class TabViews
{
	/**
	 * Get configuration values for an implementing child
	 * @return HashMap<String,String>
	 */
	public abstract HashMap<String,String> configValues();
	
	/**
	 * Get the overall view for an implementing child
	 * @return Container
	 */
	public abstract Container contentComponent();
	
	/**
	 * Get the icon files for an implementing child (theme choice and device-specific)
	 * @return HashMap<String,String>
	 */
	public abstract HashMap<String,String> tabIconFiles();
	
	/**
	 * Handle rotation
	 */
	public abstract void rotate();
	
	public TabViews currentView;
	
	/**
	 * A generic implementation for getting values from ondevice_sections
	 * 
	 * @since 20140312
	 * @author costmo
	 * @param		findTag			The tag value to lookup
	 */
	public HashMap<String,String> configValuesByTag( String findTag )
	{
		HashMap<String,String> returnValue = new HashMap<String,String>();
		
		String sql = new String(
				"SELECT		_id, sf_tag, display_label, ui_location, position_index, last_update "
			+ 	"FROM		ondevice_sections "
			+ 	"WHERE		sf_tag = '" + findTag  + "'"
		);
		
		ChurchAppData cad = new ChurchAppData( false );
		
		try
		{
			Cursor cursor = cad.db.executeQuery( sql );
			while( cursor.next() )
			{
				Row row = cursor.getRow();
				returnValue.put( "id", String.valueOf( row.getInteger( 0 ) ) );
				returnValue.put( "sf_tag", row.getString( 1 ) );
				returnValue.put( "display_label", row.getString( 2 ) );
				returnValue.put( "ui_location", String.valueOf( row.getInteger( 3 ) ) );
				returnValue.put( "position_index", String.valueOf( row.getInteger( 4 ) ) );
				returnValue.put( "last_update", String.valueOf( row.getInteger( 5 ) ) );
			}
		}
		catch( IOException e )
		{
			e.printStackTrace();
			throw new RuntimeException( e.getMessage() );
		}
		
		return returnValue;
	} // configValuesByTag
	
	/**
	 * A generic implementation for getting icon files for sections based on device and theme.
	 * 
	 * Returns a hashmap with keys "normal" and "selected"
	 * 
	 * @since 20140312
	 * @author costmo
	 * @param	findTag		The tag of the child for which the icon file should be gotten
	 */
	public HashMap<String,String> tabIconFilesByTag(  String findTag  )
	{
		HashMap<String,String> returnValue = new HashMap<String,String>();
		ChurchAppData cad = new ChurchAppData( false );
		
		int sectionId = Integer.valueOf( this.configValues().get( "id" ) ).intValue();
		
		String[] iconKeys = { "icon", "icon_selected" };
		
		HashMap<String,String> iconHash = cad.getValueForMetaKey( "ondevice_section_meta", "ondevice_section_data_id", sectionId, iconKeys );
		
		String iconFile = cad.theme + "_" + cad.weight + "_" + iconHash.get( iconKeys[0] );
		String selectedIconFile = cad.theme + "_" + cad.weight + "_" + iconHash.get( iconKeys[1] );
		
		iconFile = iconFile + ".png";
		selectedIconFile = selectedIconFile + ".png";
		
		returnValue.put( "normal", iconFile );
		returnValue.put( "selected", selectedIconFile );
		
		return returnValue;
	} // tabIconFilesByTag
	
	/**
	 * Factory method to get an implementer of this class by its tag.
	 * 
	 * @since 20140312
	 * @author costmo
	 * @param	childTag		The tag of the child for which the action is being performed
	 */
	public static TabViews getChild( String childTag )
	{
		if( childTag.equals( "welcome" ) )
		{
			return new WelcomeView();
		}
		else if( childTag.equals( "sermons" ) )
		{
			return new SermonsView();
		}
		else if( childTag.equals( "posts" ) )
		{
			return new PostsView();
		}
		else if( childTag.equals( "more" ) )
		{
			return new MoreView();
		}
		else if( childTag.equals( "bible" ) ) // left off implementation here
		{
			return new WelcomeView();
		}
		else if( childTag.equals( "calendar" ) )
		{
			return new CalendarView();
		}
		else if( childTag.equals( "about" ) )
		{
			return new WelcomeView();
		}
		else if( childTag.equals( "contact" ) )
		{
			return new WelcomeView();
		}
		else if( childTag.equals( "map" ) )
		{
			return new WelcomeView();
		}
		else if( childTag.equals( "social" ) )
		{
			return new WelcomeView();
		}
		else if( childTag.equals( "documents" ) )
		{
			return new WelcomeView();
		}
		
		
		return new WelcomeView();
		
	} // getChild
}
