package com.smartgwt.sample.client;

import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Canvas;
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.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.ValuesManager;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;

public class MyModalWindow extends Window {
	
	private TabSet geographicInfoTabs;
	private TextItem codeItem;
	private SelectItem regionItem;
	private TextItem createdByItem;
	
	public MyModalWindow() {
		init();
	}

	private void init() {
		this.setTitle("title");
		this.setShowMinimizeButton(false);
		this.setIsModal(true);
		this.setShowModalMask(true);
		this.addItem(createContent());
		this.addCloseClickHandler(new CloseClickHandler() {
			public void onCloseClick(CloseClientEvent event) {
				MyModalWindow.this.destroy();
			}
		});
		this.setAutoSize(true);
		this.setAutoCenter(true);
		this.setShowFooter(true);
	}

	private Canvas createContent() {
		VLayout layout = new VLayout();
		layout.setWidth(492);
		layout.setHeight100();
		layout.setMargin(10);


		final ValuesManager valuesManager = new ValuesManager();
		DynamicForm employeeInfoForm = getDynamicForm(true);
		employeeInfoForm.setValuesManager(valuesManager);
		employeeInfoForm.setGroupTitle("my group");
		employeeInfoForm.setPadding(10);

		DynamicForm geographicInfoForm = getDynamicForm(false);
		geographicInfoForm.setValuesManager(valuesManager);
		geographicInfoTabs = new TabSet();
		Tab geographbicInfoTab = new Tab("tab");
		geographicInfoTabs.addTab(geographbicInfoTab);
		geographbicInfoTab.setPane(geographicInfoForm);
		
		/**
		 * This fixes the layout. 
		 */
//		geographicInfoTabs.setWidth(472);  
//		geographicInfoTabs.setHeight(70);

		/**
		 * The problematic part
		 */
		geographicInfoTabs.setWidth100();  
		geographicInfoTabs.setHeight100();

		DynamicForm systemInfoForm = getDynamicForm(true);
		systemInfoForm.setValuesManager(valuesManager);
		systemInfoForm.setGroupTitle("sys form");
		systemInfoForm.setPadding(10);
		
		codeItem = new TextItem("internalId");
		codeItem.setWidth(300);
		
		regionItem = new SelectItem("regionId", "Region");
		regionItem.setWidth(300);

		createdByItem = new TextItem("createdByDisplay", "created by");
		createdByItem.setWidth(300);
		
		ToolStripButton cancelButton = new ToolStripButton("Cancel");
		cancelButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				destroy();
			}
		});
		
		ToolStripButton saveButton = new ToolStripButton("Save");
		saveButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				valuesManager.saveData(new DSCallback() {
					@Override
					public void execute(DSResponse response, Object rawData, DSRequest request) {
						destroy();
					}
				}); 
			}
		});

		
		ToolStrip buttonsToolStrip = new ToolStrip();
		buttonsToolStrip.setWidth100();
		buttonsToolStrip.setPadding(3);
		buttonsToolStrip.addButton(cancelButton);
		buttonsToolStrip.addFill();
		buttonsToolStrip.addButton(saveButton);
		employeeInfoForm.setFields(codeItem);
		geographicInfoForm.setFields(regionItem);
		systemInfoForm.setFields(createdByItem);
		layout.addMember(employeeInfoForm);
		layout.addMember(geographicInfoTabs);
		layout.addMember(systemInfoForm);
 
		this.setFooterControls(buttonsToolStrip);
		return layout;
	}
		
	private DynamicForm getDynamicForm(boolean hasTitle) {
		DynamicForm form = new DynamicForm();
		form.setIsGroup(hasTitle);
		form.setHeight100();
		form.setWidth100();
		form.setValidateOnExit(true);
		form.setTitleAlign(Alignment.LEFT);
		form.setTitleWidth(150);
		form.setFixedColWidths(true);
		form.setTitleSuffix("");
		form.setRequiredTitleSuffix("");
		form.setLayoutAlign(VerticalAlignment.BOTTOM);
		return form;
	}
}