package gwt.material.design.client.ui;

/*
 * #%L
 * GwtMaterial
 * %%
 * Copyright (C) 2015 GwtMaterialDesign
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.util.Date;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTMLPanel;

public class MaterialDatePicker extends FocusPanel{

	public interface MaterialDatePickerDelegate {
		void onCalendarClick(Date currDate);
	}
	
	private HTMLPanel panel;
	private Date date;
	private String placeholder;
	private String id;
	private MaterialDatePickerDelegate delegate;
	
	public MaterialDatePicker() {
	
	}
		
	@Override
	protected void onAttach() {
		// TODO Auto-generated method stub
		super.onAttach();
		id = String.valueOf(hashCode());
		this.clear();
		panel = new HTMLPanel("<input placeholder='"+placeholder+"' type='date' id='"+id+"' class='datepicker'>");
		this.add(panel);
		initDatePicker(id);
		onValueChange(id, this);
	}
	
	public static native String getDatePickerValue(String id)/*-{
		var color    = $wnd.jQuery('#' + id).val();  
		return color;
	}-*/;
	
	private static native void setDatePickerValue(String value, String id)/*-{
        $wnd.jQuery('#' + id).val(value);
	}-*/;
	
	public static native void initDatePicker(String id)/*-{
        $wnd.jQuery('#' + id).pickadate();
	}-*/;
	
	native void onValueChange(String id, MaterialDatePicker picker) /*-{
		$wnd.jQuery('.picker__wrap').bind('click',function(){
			picker.@gwt.material.design.client.ui.MaterialDatePicker::notifyDelegate()();
			});
	}-*/;

	public void setDelegate(MaterialDatePickerDelegate del) {
		this.delegate = del;
	}
	
	void notifyDelegate() {
		if(delegate != null) {
			delegate.onCalendarClick(getDate());
		}
	}


	
	public Date getDate() {
		return getPickerDate();
	}

	public static native void clearValues()/*-{
		$wnd.jQuery('.datepicker').val("");
	}-*/;
	
	public Date getPickerDate(){
		try{
			DateTimeFormat sdf = DateTimeFormat.getFormat("d MMM, yyyy");
			date = sdf.parse(getDatePickerValue(id));
			return date;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public void setDate(Date date) {
		this.date = date;
		DateTimeFormat sdf = DateTimeFormat.getFormat("d MMM, yyyy");
		setDatePickerValue(sdf.format(date), id);
	}

	public String getPlaceholder() {
		return placeholder;
	}

	public void setPlaceholder(String placeholder) {
		this.placeholder = placeholder;
	}
	
}

@@ -910,53 +910,256 @@
 %7B%0A%0A%09
-public interface MaterialDatePickerDelegate %7B
+/**%0A%09 * Delegate interface for handling picker events.%0A%09 */%0A%09public interface MaterialDatePickerDelegate %7B%0A%09%09%0A%09%09/**%0A%09%09 * Called as soon as a click occurs on the calendar widget. !EXPERIMENTAL!%0A%09%09 * @param currDate which is currently selected.%0A%09%09 */
 %0A%09%09v
@@ -1681,29 +1681,32 @@
 (id);%0A%09%09
-onValueChange
+initClickHandler
 (id, thi
@@ -2103,21 +2103,24 @@
 oid 
-onValueChange
+initClickHandler
 (Str
@@ -2311,16 +2311,193 @@
 %09%7D-*/;%0A%0A
+%09/**%0A%09 * A delegate which implements handling of events from date picker.%0A%09 * @param delegate which will be notified on picker events.%0A%09 * @see MaterialDatePickerDelegate %0A%09 */%0A
 %09public 
@@ -2543,16 +2543,21 @@
 gate del
+egate
 ) %7B%0A%09%09th
@@ -2573,16 +2573,21 @@
 te = del
+egate
 ;%0A%09%7D%0A%09%0A%09

Add documentation and renaming for MaterialDatePicker