package com.hp.isomorphic.client.issues;

import java.util.Date;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.DateUtil;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.DateTimeItem;
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.EditorEnterEvent;
import com.smartgwt.client.widgets.form.fields.events.EditorEnterHandler;
import com.smartgwt.client.widgets.form.fields.events.EditorExitEvent;
import com.smartgwt.client.widgets.form.fields.events.EditorExitHandler;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.form.validator.CustomValidator;
import com.smartgwt.client.widgets.form.validator.Validator;
import com.smartgwt.client.widgets.layout.Layout;

public class DateTimeItemNotADateIssue extends Layout {
	
	public DateTimeItemNotADateIssue() {
		initDateUtil();
		MyDateTimeItem dateTimeItem = new MyDateTimeItem("Test");
		
		DynamicForm form = new DynamicForm();
		form.setHeight("*");
		form.setWidth(300);
		form.setFields(dateTimeItem);
	
		addMember(form);
	}
	
	private void initDateUtil() {
		DateUtil.setDateInputFormat("DMY");
	}
	
	public class MyDateTimeItem extends DateTimeItem {
		private final Validator requiredValidator = new CustomValidator() {
			{
				setErrorMessage("Required Field Message");
			}
			
			@Override
			protected boolean condition(Object value) {
				Boolean required = true;
				if (required) {
					if (value != null && value.toString().trim().isEmpty()) {
						return false;
					}
				}
				return true;
			}
		};
		
		public MyDateTimeItem() {
			super();
			init();
		}
		
		public MyDateTimeItem(String name) {
			super(name);
			init();
		}
		
		public MyDateTimeItem(String name, String title) {
			super(name, title);
			init();
		}
		
		
		protected void init() {
			
			setTextAlign(Alignment.LEFT);
			setUseTextField(true);
			setEnforceDate(true);
			setValidators(requiredValidator);
			
			setUse24HourTime(true);
			setFormat("E MMM dd h:mm:ss a yyyy"); 
			
			addKeyPressHandler(new KeyPressHandler() {
				@Override
				public void onKeyPress(KeyPressEvent event) {
					if (event.isCtrlKeyDown() && event.getKeyName().equals("A")) { //$NON-NLS-1$
						selectValue();
						event.cancel();
					}
				}
			});
			
			addChangedHandler(new ChangedHandler() {
				@Override
				public void onChanged(ChangedEvent event) {
					if (getValidateOnChange()) {
						validate();
					}
				}
			});
			
			addEditorExitHandler(new EditorExitHandler() {
				@Override
				public void onEditorExit(EditorExitEvent event) {
					if (getValidateOnExit()) {
						validate();
					}
				}
			});
			
			
			addEditorEnterHandler(new EditorEnterHandler() {
				@Override
				public void onEditorEnter(EditorEnterEvent event) {
					setEmptyDisplayValue(""); //$NON-NLS-1$
					Object value = getValue();
					if (value == null) {
						setValue(new Date());
					}
				}
			});
			
			addEditorExitHandler(new EditorExitHandler() {
				@Override
				public void onEditorExit(EditorExitEvent event) {
					setEmptyDisplayValue("DEFAULT");
					Object value = getValue();
					if (value != null && value.toString().trim().isEmpty()) {
						setValue((Date)null);
					}
				}
			});
			
		}
	}
}
