package com.smartgwt.sample.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.data.AdvancedCriteria;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.util.PageKeyHandler;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
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.ValuesManager;
import com.smartgwt.client.widgets.form.fields.BooleanItem;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.HiddenItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.VLayout;

public class BuiltInDS implements EntryPoint {
	private VLayout mainLayout;
	private IButton recreateBtn;

	private ValuesManager myValuesManager;
	private DynamicForm shippingDateDF;
	private DynamicForm masterDataDF;

	private DataSource supplyItemDS = DataSource.get("supplyItem");

	public void onModuleLoad() {
		KeyIdentifier debugKey = new KeyIdentifier();
		debugKey.setCtrlKey(true);
		debugKey.setKeyName("D");

		Page.registerKey(debugKey, new PageKeyHandler() {
			public void execute(String keyName) {
				SC.showConsole();
			}
		});

		mainLayout = new VLayout(20);
		mainLayout.setWidth100();
		mainLayout.setHeight100();

		recreateBtn = new IButton("Recreate");
		recreateBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				recreate();
			}
		});
		mainLayout.addMember(recreateBtn);
		recreate();
		mainLayout.draw();
	}

	private void recreate() {
		Window w = new Window();
		w.setWidth(300);
		w.setHeight(300);
		w.setModalMaskOpacity(80);
		w.setTitle("ValuesManager/DynamicForm DateItem validation");
		w.setShowMinimizeButton(false);
		w.setIsModal(true);
		w.setShowModalMask(true);
		w.centerInPage();

		final TextItem itemName = new TextItem("itemName");
		final TextItem sku = new TextItem("SKU");

		final HiddenItem itemID = new HiddenItem("itemID");
		final DateItem nextShipment = new DateItem("nextShipment");
		nextShipment.setUseTextField(true);
		final BooleanItem inStock = new BooleanItem("inStock");

		myValuesManager = new ValuesManager() {
			@Override
			public void saveData() {
				Boolean doesNotTriggerRequest = super.validate();
				if (doesNotTriggerRequest) {
					super.saveData();
				}
			};

			{
			}
		};
		myValuesManager.setDataSource(supplyItemDS);

		masterDataDF = new DynamicForm();
		masterDataDF.setGroupTitle("Masterdata related");
		masterDataDF.setIsGroup(true);
		masterDataDF.setValuesManager(myValuesManager);
		masterDataDF.setDataSource(myValuesManager.getDataSource());
		masterDataDF.setFields(itemName, sku);

		shippingDateDF = new DynamicForm();
		shippingDateDF.setGroupTitle("Date related");
		shippingDateDF.setIsGroup(true);
		shippingDateDF.setValuesManager(myValuesManager);
		shippingDateDF.setDataSource(myValuesManager.getDataSource());
		shippingDateDF.setFields(itemID, nextShipment, inStock);

		IButton validate = new IButton("Validate & Save");
		validate.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				myValuesManager.saveData();
			}
		});

		w.addItem(masterDataDF);
		w.addItem(shippingDateDF);
		w.addItem(validate);

		myValuesManager.fetchData(new AdvancedCriteria("itemID", OperatorId.EQUALS, 1));

		w.show();
	}
}