/**
 *
 */
package com.digitalage.ssb.client.datapanel;

import com.digitalage.ssb.client.core.ApplicationParameters;
import com.digitalage.ssb.client.datasources.CategorieVociBaseDS;
import com.digitalage.ssb.client.datasources.CategorieVociBaseLGR;
import com.digitalage.ssb.domain.Constants;
import com.digitalage.ssb.domain.model.base.CategorieVociBase;
import com.google.gwt.core.client.GWT;
import com.smartgwt.client.core.DataClass;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Autofit;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickHandler;
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.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;

/**
 * @author Roberto Rossi
 *
 */
public class CategorieVociPanel {

	public final String TAB_PREFIX_ID = "CategorieVociPanel";

	private static Tab _listTab = null;
	private static Tab _editTab = null;
	private static DataSource _dataSource = null;
	private static DynamicForm _form = null;

	public CategorieVociPanel() {
		init();
	}

	private void init() {
		_dataSource = CategorieVociBaseDS.getInstance();
	}

	public static DynamicForm getFormInstance() {
		if (_form == null || _editTab == null) {
			GWT.log("creating form",null);
			_form = new DynamicForm();
			_form.setDataSource(_dataSource);
		}
		else {
			GWT.log("form is not null", null);
		}

		return _form;
	}

	public Tab getListTab() {
		if (_listTab == null) {
			_listTab = new Tab("categorie voci");
			_listTab.setID(TAB_PREFIX_ID + "list");
			_listTab.setCanClose(true);

			GWT.log("creating ListGrid...",null);
			final ListGrid _listGrid = new ListGrid();
			_listGrid.setWidth100();
			_listGrid.setAlternateRecordStyles(true);
			_listGrid.setShowAllRecords(true);

			_listGrid.setCanResizeFields(true);
			_listGrid.setCanReorderFields(true);
			_listGrid.setCanSort(true);
			GWT.log("setting datasource CategorieVociBaseDS...",null);
			_listGrid.setDataSource(_dataSource);
			_listGrid.setAutoFetchData(true);
			_listGrid.setShowFilterEditor(true);
			_listGrid.setFilterOnKeypress(true);
			GWT.log("fetching data ...",null);
			_listGrid.setAutoFitData(Autofit.BOTH);
			_listGrid.fetchData();

			_listGrid.addRecordDoubleClickHandler(new RecordDoubleClickHandler() {
				public void onRecordDoubleClick(RecordDoubleClickEvent event) {
					GWT.log("clicked on record",null);
					DynamicForm form = getFormInstance();

					TabSet centerTabSet = ApplicationParameters.getInstance().getCenterTabSet();
					if (centerTabSet != null) {
						Tab editTab = getEditTab();

						centerTabSet.addTab(editTab);
						centerTabSet.selectTab(editTab.getID());
					}
					else {
						GWT.log("cannot add tab, tab set is null", null);
					}

					form.clearErrors(true);
					form.editRecord(event.getRecord());
				}

			});

			GWT.log(CategorieVociBaseDS.getInstance().getTitle(),null);
			_listTab.setPane(_listGrid);
		}

		return _listTab;
	}

	public Tab getEditTab() {
		if (_editTab == null) {
			_editTab = new Tab("modifica categoria voce");
			_editTab.setID(TAB_PREFIX_ID + "edit");
			_editTab.setCanClose(true);

			GWT.log("creating edit tab",null);

			IButton saveButton = new IButton("Save");
			saveButton.setLayoutAlign(Alignment.CENTER);
			saveButton.addClickHandler(new ClickHandler() {
			    public void onClick(ClickEvent event) {
			    	GWT.log("cliccato pulsante", null);

			    	_form.saveData(new DSCallback() {
							public void execute(DSResponse response, Object rawData, DSRequest request) {
								GWT.log("dati salvati", null);
								CategorieVociBaseLGR[] ourRecords = (CategorieVociBaseLGR[]) _dataSource.getTestData();

								if (ourRecords != null) {
									GWT.log("record trovati: " + ourRecords.length, null);
								}

								for (int i = 0; i < ourRecords.length; i++) {
									GWT.log("record value: " + ourRecords[i].getAttribute(CategorieVociBase.REF + Constants.PROPERTY_SEPARATOR + CategorieVociBase.PROP_CVB_DESCRIZIONE), null);
								}
							}
						}
			    	);
			    }
			});

			VLayout editorLayout = new VLayout(5);
			editorLayout.addMember(_form);
			editorLayout.addMember(saveButton);
			editorLayout.setWidth100();

			_editTab.setPane(editorLayout);
		}
		else {
			GWT.log("edit tab is not null",null);
		}

		return _editTab;
	}

}
