/**
 * Override Label to handle behavior specific to images in labels.
 * 
 * CN1's default behavior for NORTH labels is to shrink them so that the CENTER component fits. This overrides that behavior
 *    for top-of-page images that must maintain their full-allowed size
 */
package com.sharefaith.churchapp.ui;

import java.util.HashMap;

import com.codename1.ui.Component;
import com.codename1.ui.Image;
import com.codename1.ui.Label;
import com.codename1.ui.geom.Dimension;
import com.codename1.ui.plaf.Style;
import com.sharefaith.churchapp.ChurchApp;
import com.sharefaith.churchapp.SFUtil;

public class SFImageLabel extends Label
{
	/**
	 * The image to use as the icon image for the label
	 */
	public Image labelImage;
	
	/**
	 * Set the image to be used.
	 * 
	 * This needs to be called before the other methods will work as expected
	 * 
	 * @since 20140312
	 * @author costmo
	 * @param		inputImage			An instance of the image to use
	 */
	public void setImage( Image inputImage )
	{
		this.labelImage = inputImage;
	}
	
	/**
	 * Overrides the default calcPreferredSize to get the device's current full width and the proportional height of the image
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	@Override
	public Dimension calcPreferredSize()
	{
		if( this.labelImage == null )
		{
			return this.calcPreferredSize();
		}
		
		int deviceWidth = Integer.valueOf( ChurchApp.deviceInfo().get( "width" ) ).intValue();

		int imgWidth = this.labelImage.getWidth();
		int imgHeight = this.labelImage.getHeight();
		double ratio = ((double)imgHeight/(double)imgWidth);
		
		return new Dimension( deviceWidth, (int)Math.floor( (deviceWidth * ratio) ) );
	}
	
	/**
	 * Set/reset the icon image to full device width
	 * 
	 * @since 20140312
	 * @author costmo
	 * @param		inputImage			An instance of the image to use
	 */
	public void setFullWidthImage( Image inputImage  )
	{
		this.setImage( inputImage );
		
		Style imgStyle = new Style();
		imgStyle.setPadding( 0, 0, 0, 0 );
		imgStyle.setMargin( 0, 0, 0, 0 );
		imgStyle.setAlignment( Component.CENTER );
		
		this.setSelectedStyle( imgStyle );
		this.setUnselectedStyle( imgStyle );
		
		Dimension dim = this.calcPreferredSize();
		this.setIcon( this.labelImage.scaled( dim.getWidth(), dim.getHeight() ) );
	}
	
	/**
	 * A convenience wrapper to setFullWidthImage( Image )
	 * 
	 * @since 20140312
	 * @author costmo
	 */
	public void resizeToFullWidth()
	{
		this.setFullWidthImage( this.labelImage );
	}
}
