package cat.diba.jee.mediatecagwt.client;

import static cat.diba.jee.mediatecagwt.client.MediatecaSmart.MSG;
import static cat.diba.jee.mediatecagwt.client.model.mapper.TypeMapper.TypeMapper;
import static cat.diba.jee.mediatecagwt.client.rest.ConstantsRest.ADVANCED_SEARCH_PATH;
import static cat.diba.jee.mediatecagwt.client.rest.ConstantsRest.DICTIONARY_PATH;
import static cat.diba.jee.mediatecagwt.client.rest.ConstantsRest.PARAM_ATTRIBUTES;
import static cat.diba.jee.mediatecagwt.client.rest.ConstantsRest.PARAM_CONTENT;
import static cat.diba.jee.mediatecagwt.client.rest.ConstantsRest.PARAM_LANGUAGE;
import static cat.diba.jee.mediatecagwt.client.rest.ConstantsRest.PARAM_ORDER;
import static cat.diba.jee.mediatecagwt.client.rest.ConstantsRest.PARAM_TYPE;
import static cat.diba.jee.mediatecagwt.client.utils.UrlUtil.restUrlBuilder;
import cat.diba.jee.mediatecagwt.client.model.Attribute;
import cat.diba.jee.mediatecagwt.client.model.Type;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.UrlBuilder;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.XMLParser;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.OperationBinding;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.DSProtocol;
import com.smartgwt.client.widgets.grid.ListGrid;

public class MediatecaSmart_TEST implements EntryPoint {

	public void onModuleLoad() {
		
		Log.setUncaughtExceptionHandler();
		Log.info("Logging with gwt-log");
		
		// use a deferred command so that the handler catches onModuleLoad2() exceptions
		try {
			Scheduler.get().scheduleDeferred(new ScheduledCommand() {
				@Override
				public void execute() {
					onModuleLoad2();
				}
			});
		} catch (Exception e) {
			Log.error("Error al iniciar", e);
		}
	}

	public void onModuleLoad2() {

		Log.info("onModuleLoad2() - start");

		DataSource dataSource = createRestDataSource();

		ListGrid listGrid = createListGrid(dataSource);
		
		RootPanel.get("mediateca-smart").add(listGrid);
		
		Log.info("onModuleLoad2() - end");
	}

	private DataSource createRestDataSource() {

		UrlBuilder urlBuilder =
			restUrlBuilder(ADVANCED_SEARCH_PATH + "/test");

		RestDataSource dataSource = new RestDataSource() {
			@Override
			protected void transformResponse(DSResponse response, DSRequest request, Object data) {
				super.transformResponse(response, request, data);
				
				Log.debug("Response info:");
				Log.debug("Rows: [" + response.getStartRow() + ", " + response.getEndRow() + "] of " + response.getTotalRows());
				Log.debug("Status: " + response.getStatus());
			}
		};

		// Data request
		
		String dataUrl = urlBuilder.buildString();
		Log.info("dataUrl = "+ dataUrl);
		dataSource.setDataURL(dataUrl);
		dataSource.setDataFormat(DSDataFormat.XML);

		OperationBinding fetch = new OperationBinding();
		fetch.setOperationType(DSOperationType.FETCH);
		fetch.setDataProtocol(DSProtocol.GETPARAMS);
		dataSource.setOperationBindings(fetch);
		
		dataSource.setMetaDataPrefix("");

		// Fields
		
		DataSourceTextField idField = new DataSourceTextField("id", MSG.CAMP_ID());
		DataSourceTextField nameField = new DataSourceTextField("name", MSG.CAMP_NOM());
		DataSourceTextField tipusField = new DataSourceTextField("type", MSG.CAMP_TIPUS());
				
		dataSource.setFields(idField, nameField, tipusField);
		
		return dataSource;
	}

	private ListGrid createListGrid(DataSource dataSource) {
		
		ListGrid listGrid = new ListGrid();
		
		listGrid.setCanFreezeFields(true);
		listGrid.setHeight(200);
		listGrid.setWidth(600);
		listGrid.setDataPageSize(50);
		listGrid.setAutoFetchData(true);
		listGrid.setDataSource(dataSource);
		listGrid.setShowAllRecords(false);
		
		return listGrid;
	}
}