package com.smartgwt.sample.client;


import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.BackgroundRepeat;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.Positioning;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HTMLPane;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.HeaderItem;
import com.smartgwt.client.widgets.form.fields.LinkItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.SpacerItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.LayoutSpacer;
import com.smartgwt.client.widgets.layout.VLayout;


public abstract class LogonPage extends VLayout
{

	protected DynamicForm form;
	
	protected DynamicForm form2;
	
	protected DynamicForm form3;

	protected TextItem username;

	protected PasswordItem password;
	
	protected CheckboxItem rememberMe;
	
	protected LinkItem forgotPassword;

	protected ImgButton logonButton;

	protected HTMLPane welcomePane;
	
	protected HTMLPane footer;

	protected String pre;

	protected String post;

	protected Label errorMessageLabel;
	
	protected HTMLPane tabsPane;
	
	protected LinkItem about;
	
	protected LinkItem contactUs;
	

	public LogonPage()
	{
		setID( "LogonPage" );
		setMembersMargin( 0 );
		setShowEdges( false );
		setCanDragResize( false );
		setWidth100();
		setHeight100();
		setPadding( 0 );
		setMargin( 0 );
		setBackgroundColor( "#666" );
		setBackgroundRepeat( BackgroundRepeat.REPEAT_X );
		setAnimateMembers( true );
		setOverflow(Overflow.HIDDEN);

		setMembers( createBody() );

		addChild( createLoginLayout() );

		username.focusInItem();
	}

	public Canvas createBody()
	{
		Canvas body = new Canvas();
		body.setID( "loginBodyCanvas" );
		body.setWidth100();
		body.setHeight100();
		body.setBackgroundColor( "white" );
		
		body.setMargin(0);
		body.setPadding(0);
		body.setPosition(Positioning.ABSOLUTE);
		body.setOverflow(Overflow.HIDDEN);

		return body;
	}

	protected abstract String getProductName();

	public Canvas createLoginLayout()
	{
		VLayout loginLayout = new VLayout();
		loginLayout.setID( "loginCanvas" );
		loginLayout.setWidth( 410 );
		loginLayout.setHeight( 320 );
		loginLayout.setTop( 144 );
		loginLayout.setLeft( "5%" );
		loginLayout.setStyleName( "loginLayout" );

		form = new DynamicForm();
		form.setID( "LogonForm" );
		form.setBrowserSpellCheck( false );
		form.setTitleOrientation( TitleOrientation.TOP );
		form.setTitleSuffix("");
		form.setRequiredTitleSuffix("");
		form.setRequiredRightTitleSuffix("");
		form.setAutoFocus(true);
		
		HeaderItem loginLabel = new HeaderItem( "LoginLabel" );
		loginLabel.setValue( "Login" );

		username = new TextItem( "username");
		username.setTitle("Username");
		username.setTabIndex( 1 );
		username.setRequired( true );
		username.setSelectOnFocus( true );
		username.setHeight( 25 );

		username.setWidth( 300 );

		username.addChangedHandler( new ChangedHandler()
		{
			@Override
			public void onChanged(ChangedEvent event)
			{
				enableDisableLoginButton();
			}
		} );

		password = new PasswordItem( "password", "Password" );
		password.setTabIndex( 2 );
		password.setRequired( true );
		password.setSelectOnFocus( true );
		password.setHeight( 25 );
		password.setWidth( 300 );

		password.addChangedHandler( new ChangedHandler()
		{

			@Override
			public void onChanged(ChangedEvent event)
			{
				enableDisableLoginButton();
			}
		} );

		
		username.addKeyPressHandler( new KeyPressHandler()
		{

			@Override
			public void onKeyPress(KeyPressEvent event)
			{
				clearErrorMessage();
				if ( event.getKeyName().equals( "Enter" ) )
				{
					password.focusInItem();
				}
			}
		} );

		password.addKeyPressHandler( new KeyPressHandler()
		{

			@Override
			public void onKeyPress(KeyPressEvent event)
			{
				clearErrorMessage();
				if ( event.getKeyName().equals( "Enter" ) )
				{
					logonButton.fireEvent( new com.smartgwt.client.widgets.events.ClickEvent( logonButton.getJsObj() ) );
				}
			}
		} );

		
		rememberMe = new CheckboxItem("rememberMe", " Remember Me");
		rememberMe.setTabIndex(3);

		form.setWidth( "100%" );
		form.setNumCols( 1 );
		form.setMargin( 5 );
		form.setCellPadding( 5 );
		form.setTitleWidth( 125 );
		form.setItems( loginLabel, username, password, rememberMe );
		form.focusInItem( username );
		loginLayout.addMember( form );
		
		forgotPassword = new LinkItem();
		forgotPassword.setShowTitle(false);
		forgotPassword.setLinkTitle("Forgot username/password?");
		forgotPassword.setTarget("");
//		forgotPassword.setCellStyle("forgotpwd");
		forgotPassword.setHeight(20);
		
		form2 = new DynamicForm();
		form2.setID( "LinkForm" );
		form2.setWidth( "100%" );
		form2.setNumCols( 1 );
		form2.setMargin( 5 );
		form2.setHeight(20);
		form2.setCellPadding( 0 );
		form2.setItems(forgotPassword);
		
		Canvas spacer = new Canvas();
		spacer.setHeight( 10 );
		loginLayout.addMember( spacer );

		logonButton = new ImgButton();
		logonButton.setHeight( 41 );
		logonButton.setWidth( 199 );
		logonButton.setLayoutAlign( Alignment.CENTER );
		logonButton.setShowRollOver( true );
		logonButton.setShowDown( true );
		logonButton.setShowDisabled( true );
		logonButton.setSrc( "btn-login.png" );
		logonButton.setDisabled( true );
		
		loginLayout.addMember( logonButton );

		errorMessageLabel = new Label();
//		errorMessageLabel.setStyleName("badLoginErrorLabel");
		errorMessageLabel.setWidth100();
		errorMessageLabel.setHeight(80);
		
		loginLayout.addMember(errorMessageLabel);
	
		loginLayout.addMember(form2);
	
		username.focusInItem();
		return loginLayout;
	}

	public void enableDisableLoginButton()
	{
		if ( username.getValue() != null && password.getValue() != null && username.getValueAsString().trim().length() > 0
				&& password.getValueAsString().length() > 0 )
		{
			logonButton.enable();
		}
		else
		{
			logonButton.disable();
		}
	}

	public void clearErrorMessage()
	{
		errorMessageLabel.setIcon( null );
		errorMessageLabel.setContents( "" );
	}
	
	public void clearFields()
	{
		username.clearValue();
		password.clearValue();
	}

}
