/**
 * Provides the view for the Welcome section
 */
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.BrowserComponent;
import com.codename1.ui.Container;
import com.codename1.ui.Display;
import com.codename1.ui.Image;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.plaf.Border;
import com.codename1.ui.plaf.Style;
import com.sharefaith.churchapp.SFUtil;
import com.sharefaith.churchapp.model.*;
import com.sharefaith.churchapp.ui.SFImageLabel;

public class WelcomeView extends TabViews
{
	/**
	 * A tag to identify this section in the UI
	 */
	public static final String TAG = new String( "welcome" );
	
	/**
	 * An instance of the data getter
	 */
	public ChurchAppData cad;
	
	/**
	 * Configuration options
	 */
	public HashMap<String,String> config;
	
	/**
	 * An instance of the image label to use
	 */
	public SFImageLabel imageLabel;
	
	/**
	 * The content of the main body, of course
	 */
	public String bodyContent;
	
	/**
	 * Class constructor.
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public WelcomeView()
	{
		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 bannerImage = Image.createImage( "/banner_2048_welcome.png" );
			this.imageLabel.setFullWidthImage( bannerImage );
		}
		catch( IOException e )
		{
			e.printStackTrace();
		}

		BrowserComponent bc = new BrowserComponent();
		bc.setPage( this.getContent(), "/" );
		
		Style bcStyle = new Style();
		bcStyle.setBorder( Border.createEmpty() );
		bcStyle.setMargin( 3, 3, 3, 3 );
		bcStyle.setFgColor( 0xffffff );
		
		bc.setSelectedStyle( bcStyle );
		bc.setUnselectedStyle( bcStyle );
		
		returnContainer.addComponent( BorderLayout.NORTH, this.imageLabel );
		returnContainer.addComponent( BorderLayout.CENTER, bc );
		
		return returnContainer;
	}
	
	/**
	 * Provides icon file locations for this tab item
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public HashMap<String,String> tabIconFiles()
	{
		return this.tabIconFilesByTag( WelcomeView.TAG );
	}
	
	/**
	 * Refresh this item's config options
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public HashMap<String,String> configValues()
	{
		if( this.config == null )
		{
			this.config = this.configValuesByTag( WelcomeView.TAG );
		}
		return this.config;
	}
	
	/**
	 * Gets the current content for this screen if the appropriate member variable has not been set
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public String getContent()
	{
		if( this.bodyContent != null && this.bodyContent.length() > 0 )
		{
			return this.bodyContent;
		}
		
		this.bodyContent = this.refreshContent();
		
		return this.bodyContent;
	}
	
	/**
	 * Initial load or forced refresh of the current content for this screen
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public String refreshContent()
	{
		String returnValue = new String( "" );
		
		String sql = new String(
				"SELECT		content "
			+ 	"FROM		section_content "
			+ 	"WHERE		ondevice_section_id = " + Integer.valueOf( this.configValues().get( "id" ) ).intValue()
		);
		
		try
		{
			Cursor cursor = this.cad.db.executeQuery( sql );
			while( cursor.next() )
			{
				Row row = cursor.getRow();
				returnValue = row.getString( 0 );
			}
		}
		catch( IOException e )
		{
			e.printStackTrace();
			throw new RuntimeException( e.getMessage() );
		}
		HashMap<String,String> htmlContent = WelcomeView.htmlEnvelope();
		
		this.bodyContent = htmlContent.get( "head" ) + returnValue + htmlContent.get( "tail" );
		
		return this.bodyContent;
	}
	
	/**
	 * Handle device rotation
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public void rotate()
	{
		SFUtil.p( "Rotate welcome" );
		this.imageLabel.resizeToFullWidth();
	}
	
	/**
	 * Get the HTML envelope (header and footer) for a web component.
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public static HashMap<String,String> htmlEnvelope()
	{
		HashMap<String,String> returnValue = new HashMap<String,String>();
		
		returnValue.put(  "head",
			  "<html>"
			+ "	<head>"
			+ "		<style>"
			+ "			body{ font-size:28px;font-family:DroidSans,Arial,sans-serif;color:#000000;background:#FFFFFF; }"
			+ "		</style>"
			+ "	</head>"
			+ "	<body height='100%'>"
		);
		returnValue.put(  "tail",
			  "	</body>"
			+ "</html>"
		);
		
		return returnValue;
	}
}
