package web.gwt.client;

import java.util.Date;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.smartgwt.client.Version;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.OperationBinding;
import com.smartgwt.client.data.fields.DataSourceDateTimeField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.DSProtocol;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.DateTimeItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class Main8 implements EntryPoint {
	public static class RestDataSource extends com.smartgwt.client.data.RestDataSource {
		public RestDataSource(final String fetchDataURL, final String restDataURL) {
 			setShowPrompt(false);
			setDataFormat(DSDataFormat.JSON);
			setDataProtocol(DSProtocol.POSTXML);
			OperationBinding[] bindings = new OperationBinding[4];
			if(fetchDataURL != null){
				bindings[0] = new OperationBinding(){{
						setOperationType(DSOperationType.FETCH);
						setDataURL(fetchDataURL);
					}};
			}
			if(restDataURL != null){
				bindings[1] = new OperationBinding(){{
					setOperationType(DSOperationType.ADD);
					setDataURL(restDataURL);
				}};
				bindings[2] = new OperationBinding(){{
					setOperationType(DSOperationType.UPDATE);
					setDataURL(restDataURL);
				}};
				bindings[3] = new OperationBinding(){{
					setOperationType(DSOperationType.REMOVE);
					setDataURL(restDataURL);
				}};
			}
			setOperationBindings(bindings);
		}
	}

	@Override
	public void onModuleLoad() {
		new VLayout(4){{
			final RestDataSource dataSource = new RestDataSource("item", "item"){{
				setFields(new DataSourceDateTimeField("date"){{
					setValueXPath("item/date");
				}});
			}};
			final DynamicForm form = new DynamicForm(){{
				setDataSource(dataSource);
				setItems(new DateTimeItem("date", "Date"){{
					setValue(new Date());
					setDataPath("item/date");
				}});
			}};
			addMember(form);
			addMember(new HLayout(4){{
				addMember(new Canvas(){{
					setContents("Version: " + Version.getVersion() + "<br/> Build date: " + DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_SHORT).format(Version.getBuildDate()));
					setAutoHeight();
					setWidth(200);
				}});
				addMember(new Button("Save"){{
					addClickHandler(new ClickHandler() {
						@Override
						public void onClick(ClickEvent event) {
							form.saveData(new DSCallback() {
								@Override
								public void execute(DSResponse dsResponse, Object data, DSRequest dsRequest) {
									form.editRecord(dsResponse.getData()[0]);
								}
							});
						}
					});
				}});
			}});
		}}.show();
	}
}
