package com.hp.isomorphic.client.issues;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.DateDisplayFormatter;
import com.smartgwt.client.util.DateParser;
import com.smartgwt.client.util.DateUtil;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.fields.DateTimeItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
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.form.validator.CustomValidator;
import com.smartgwt.client.widgets.form.validator.Validator;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridEditorContext;
import com.smartgwt.client.widgets.grid.ListGridEditorCustomizer;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.EditCompleteEvent;
import com.smartgwt.client.widgets.grid.events.EditCompleteHandler;
import com.smartgwt.client.widgets.layout.Layout;

public class DateTimeItemNotValidatingInCustomGridEditor extends Layout {

	ListGrid grid ;
	public DateTimeItemNotValidatingInCustomGridEditor() {
		initDateUtil();
		
		ListGridField dateField = new ListGridField("Column1", "Date Field", 300);
		dateField.setCanEdit(true);
		grid = new ListGrid(){
			@Override
			protected Canvas createRecordComponent(ListGridRecord record, Integer colNum) {
				return null;
			}
		};
		grid.setWidth100();
		grid.setHeight100(); 
		grid.setCanReorderFields(false);
		grid.setCanFreezeFields(false);
		grid.setCanGroupBy(false);
		grid.setCanPickFields(false);
		grid.setLeaveHeaderMenuButtonSpace(false);
		grid.setHeaderHeight(30);
		grid.setWrapCells(false);
		grid.setMinHeight(100);
		
		grid.setEditorCustomizer(new ListGridEditorCustomizer() {
			@Override
			public FormItem getEditor(ListGridEditorContext context) {
				final MyDateTimeItem dateTimeItem = new MyDateTimeItem();
				return dateTimeItem; 
			}
		});
		
		grid.addEditCompleteHandler(new EditCompleteHandler() {
			@Override
			public void onEditComplete(EditCompleteEvent event) {
				grid.validateRow(0);
			}
		});
		
		grid.addEditorExitHandler(new com.smartgwt.client.widgets.grid.events.EditorExitHandler() {

			@Override
			public void onEditorExit(com.smartgwt.client.widgets.grid.events.EditorExitEvent event) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		grid.setFields(dateField);
		
		
		List<ListGridRecord> records = new ArrayList<>();
		ListGridRecord record = new ListGridRecord();
		records.add(record);
		
		grid.setData(records.toArray(new ListGridRecord[records.size()]));
		
		addMember(grid);
		
//		DateTimeItem timeItem = new DateTimeItem();
//		DynamicForm form = new DynamicForm();
//		form.setHeight("*");
//		form.setWidth(300);
//		form.setFields(timeItem);
//		addMember(form);
		
	}
	
	private void initDateUtil() {
		DateUtil.setDateInputFormat("DMY");
		
		DateUtil.setShortDateDisplayFormatter(new DateDisplayFormatter() {
			@Override
			public String format(Date date) {
				if (date == null) return null;
				DateTimeFormat format = DateTimeFormat.getFormat("yyyy/MM/dd");
				return format.format(date);
			}
		});
		
		DateUtil.setShortDatetimeDisplayFormatter(new DateDisplayFormatter() {
			@Override
			public String format(Date date) {
				if (date == null) return null;
				DateTimeFormat format = DateTimeFormat.getFormat("yyyy/MM/dd HH.mm.ss");
				return format.format(date);
			}
		});
		
		DateUtil.setShortTimeDisplayFormatter(new DateDisplayFormatter() {
			@Override
			public String format(Date date) {
				if (date == null) return null;
				DateTimeFormat format = DateTimeFormat.getFormat("HH.mm.ss");
				return format.format(date);
			}
		});
		
		DateUtil.setDateParser(new DateParser(){
			@Override
			public Date parse(String dateString) {
				final DateTimeFormat format = DateTimeFormat.getFormat("yyyy/MM/dd HH.mm.ss");
				Date date = format.parse(dateString);
				return date;
			}
		});
		
		DateUtil.setNormalDatetimeDisplayFormatter(new DateDisplayFormatter() {
			@Override
			public String format(Date date) {
				if (date == null) return null;
				final DateTimeFormat format = DateTimeFormat.getFormat("yyyy/MM/dd HH.mm.ss");
				return format.format(date);
			}
		});
		DateUtil.setNormalDateDisplayFormatter(new DateDisplayFormatter() {
			@Override
			public String format(Date date) {
				if (date == null) return null;
				final DateTimeFormat format = DateTimeFormat.getFormat("yyyy/MM/dd");
				return format.format(date);
			}
		});
		DateUtil.setNormalTimeDisplayFormatter(new DateDisplayFormatter() {
			@Override
			public String format(Date date) {
				if (date == null) return null;
				final DateTimeFormat format = DateTimeFormat.getFormat("HH.mm.ss");
				return format.format(date);
			}
		});
	}
	
	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();
		}
		
		protected void init() {
			
			setTextAlign(Alignment.LEFT);
			setUseTextField(true);
			setEnforceDate(true);
			setValidators(requiredValidator);
			setValidateOnChange(true);
			setUse24HourTime(true);
			
			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();
					}
				}
			});
		}
		
		public boolean doValidation() {
			return validate();
		}

		public boolean hasValidationErrors() {
			return hasErrors();
		}
		
		public void endEdits() {
			setValue(getEnteredValue());
		}
		
	}
	
}
