package com.sbs.jpm.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.rpc.RPCResponse;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSProtocol;
import com.smartgwt.client.util.JSOHelper;
import com.smartgwt.client.widgets.Button;
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.CheckboxItem;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;

public class TestCase implements EntryPoint {

	private SettingsPanel wbsPanel;
	private SettingsDataSource dataSource;

	public void onModuleLoad() {

		dataSource = new SettingsDataSource();

		wbsPanel = new SettingsPanel();
		wbsPanel.setWidth100();
		wbsPanel.setHeight100();

		RootPanel.get().add(wbsPanel);
	}

	public class SettingsPanel extends VLayout {

		private DynamicForm settingsForm;
		private CheckboxItem checkboxItem;

		private Button saveSettingsButton;

		public SettingsPanel() {

			saveSettingsButton = new Button();
			saveSettingsButton.setTitle("Save");
			saveSettingsButton.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					settingsForm.submit();
				}
			});

			checkboxItem = new CheckboxItem();
			checkboxItem.setTitle("Setting");
			checkboxItem.setName(SettingsDataSource.CHECKBOX);
			checkboxItem.setColSpan(2);

			settingsForm = new DynamicForm();
			settingsForm.setDataSource(dataSource);
			settingsForm
					.setUpdateOperation(SettingsDataSource.UPDATE_SETTINGS_OPERATION);
			settingsForm.setAutoFetchData(true);
			settingsForm.setFields(checkboxItem);

			this.setMembers(settingsForm, saveSettingsButton);
		}
	}

	public class SettingsDataSource extends GwtRpcDataSource {
		private static final String DS_UNIQUE_ID = "_SettingsDataSource";

		public static final String CHECKBOX = "checkbox" + DS_UNIQUE_ID;

		public static final String UPDATE_SETTINGS_OPERATION = "updateSettingsOperation";

		private SettingsDataSource() {
			this.setID(DS_UNIQUE_ID);
		}

		@Override
		protected void executeFetch(final String requestId, DSRequest request,
				final DSResponse response) {

			ListGridRecord settingsData = new ListGridRecord();
			settingsData.setAttribute(CHECKBOX, true);

			response.setData(new ListGridRecord[] { settingsData });
			response.setStatus(RPCResponse.STATUS_SUCCESS);
			processResponse(requestId, response);
		}

		@Override
		protected void executeAdd(final String requestId, DSRequest request,
				final DSResponse response) {
		}

		@Override
		protected void executeUpdate(final String requestId,
				final DSRequest request, final DSResponse response) {

			final ListGridRecord editedRecord = getEditedRecord(request);
			final ListGridRecord oldRecord = getOldRecord(request);

			if (request.getOperationId().equals(UPDATE_SETTINGS_OPERATION)) {
				System.out.println("===========================");
				System.out.println("Old value: "
						+ oldRecord.getAttributeAsBoolean(CHECKBOX));
				System.out.println("New value: "
						+ editedRecord.getAttributeAsBoolean(CHECKBOX));

				response.setData(new ListGridRecord[] { editedRecord });
				response.setStatus(DSResponse.STATUS_SUCCESS);
				processResponse(requestId, response);
			}
		}

		@Override
		protected void executeRemove(final String requestId, DSRequest request,
				final DSResponse response) {
		}
	}

	public abstract class GwtRpcDataSource extends DataSource {

		public GwtRpcDataSource() {
			setDataProtocol(DSProtocol.CLIENTCUSTOM);
			setDataFormat(DSDataFormat.CUSTOM);
			setClientOnly(false);
		}

		@Override
		protected Object transformRequest(DSRequest request) {
			String requestId = request.getRequestId();
			DSResponse response = new DSResponse();
			response.setAttribute("clientContext",
					request.getAttributeAsObject("clientContext"));
			response.setStatus(0);

			switch (request.getOperationType()) {
			case FETCH:
				executeFetch(requestId, request, response);
				break;
			case ADD:
				executeAdd(requestId, request, response);
				break;
			case UPDATE:
				executeUpdate(requestId, request, response);
				break;
			case REMOVE:
				executeRemove(requestId, request, response);
				break;
			default:
				break;
			}
			return request.getData();
		}

		protected abstract void executeFetch(String requestId,
				DSRequest request, DSResponse response);

		protected abstract void executeAdd(String requestId, DSRequest request,
				DSResponse response);

		protected abstract void executeUpdate(String requestId,
				DSRequest request, DSResponse response);

		protected abstract void executeRemove(String requestId,
				DSRequest request, DSResponse response);

	}

	protected ListGridRecord getEditedRecord(DSRequest request) {
		ListGridRecord newRecord = new ListGridRecord();
		newRecord = getOldRecord(request);
		JavaScriptObject data = request.getData();
		JSOHelper.apply(data, newRecord.getJsObj());
		return newRecord;
	}

	protected ListGridRecord getOldRecord(DSRequest request) {
		JavaScriptObject oldValues = request
				.getAttributeAsJavaScriptObject("oldValues");
		ListGridRecord oldRecord = new ListGridRecord();
		JSOHelper.apply(oldValues, oldRecord.getJsObj());
		return oldRecord;
	}
}
