package com.digitalage.ssb.client.datasources;

import java.util.List;

import com.digitalage.ssb.client.ClientUtil;
import com.digitalage.ssb.client.GwtRpcDataSource;
import com.digitalage.ssb.client.GwtRpcDataSourceEndpoint;
import com.digitalage.ssb.client.login.DataSourceRemote;
import com.digitalage.ssb.client.login.DataSourceRemoteAsync;
import com.digitalage.ssb.domain.Constants;
import com.digitalage.ssb.domain.Util;
import com.digitalage.ssb.domain.model.base.CategorieVociBase;
import com.digitalage.ssb.domain.model.base.CategorieVociKey;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.widgets.grid.ListGridRecord;

/**
 * @author roberto
 *
 */
public class CategorieVociBaseDS extends GwtRpcDataSource<CategorieVociBase> {

	public static String DATA_SOURCE_ID = "CategorieVociBaseDS";

	private static CategorieVociBaseDS _instance = null;

	public static CategorieVociBaseDS getInstance() {
		if (_instance == null) {
			_instance = new CategorieVociBaseDS(DATA_SOURCE_ID);
		}

		return _instance;
	}

//	@Override
//	protected Object transformRequest(DSRequest dsRequest) {
//		return super.transformRequest(dsRequest);
//	}

//	@Override
//	protected void transformResponse(DSResponse response, DSRequest request, Object data) {
//		if (request.getOperationType().equals(DSOperationType.UPDATE)) {
//			response.setJsObj(request.getJsObj());
//
//			GWT.log("dentro la transformResponse", null);
//			ListGridRecord[] records = response.getData();
//
//			GWT.log("numero records: " + records.length, null);
//
//			for (ListGridRecord record : records) {
//				GWT.log("record: " + record.getAttribute(CategorieVociBase.REF + Constants.PROPERTY_SEPARATOR + CategorieVociBase.PROP_CVB_DESCRIZIONE), null);
//				//updateData(record, null, request);
//				super.updateData(record, null, new DSRequest());
//			}
//
//			response.setStatus(-1);
//		}
//		super.transformResponse(response, request, data);
//	}

	public CategorieVociBaseDS(String p_id) {
		super();

		setFetchEndpoint(new GwtRpcDataSourceEndpoint<CategorieVociBase>() {

			@Override
			public void callEndpoint(AsyncCallback callback, Object... parameters) {
				DataSourceRemoteAsync service = DataSourceRemote.Util.getInstance();

				service.findCategorieVociBase(callback);

				GWT.log("dentro fetchEndpoint", null);
			}});

		setUpdateEndpoint(new GwtRpcDataSourceEndpoint<CategorieVociBase>() {

			@Override
			public void callEndpoint(AsyncCallback callback, Object... parameters) {
				GWT.log("dentro updateEndpoint", null);
				ListGridRecord record = (ListGridRecord) parameters[0];
			}});

		setRemoveEndpoint(new GwtRpcDataSourceEndpoint<CategorieVociBase>() {

			@Override
			public void callEndpoint(AsyncCallback callback, Object... parameters) {
				GWT.log("dentro removeEndpoint", null);
				ListGridRecord record = (ListGridRecord) parameters[0];
			}});

		setAddEndpoint(new GwtRpcDataSourceEndpoint<CategorieVociBase>() {
			@Override
			public void callEndpoint(AsyncCallback callback, Object... parameters) {
				GWT.log("dentro addEndpoint", null);
				ListGridRecord record = (ListGridRecord) parameters[0];
			}});

		setID(p_id);

		DataSourceTextField _idTxtF = new DataSourceTextField(CategorieVociKey.REF + Constants.PROPERTY_SEPARATOR + CategorieVociKey.PROP_CVB_ID, CategorieVociKey.LBL_CVB_ID);
		addField(_idTxtF);
		_idTxtF.setPrimaryKey(true);
		DataSourceTextField _descrizioneTxtF = new DataSourceTextField(CategorieVociBase.REF + Constants.PROPERTY_SEPARATOR + CategorieVociBase.PROP_CVB_DESCRIZIONE, CategorieVociBase.LBL_CVB_DESCRIZIONE);
		addField(_descrizioneTxtF);

		//setClientOnly(true);

		//DataSourceRemoteAsync service = DataSourceRemote.Util.getInstance();

		Util.messaggio("Caricamento Categorie Voci...");

	}

	@Override
	protected void populateRecord(ListGridRecord p_record, CategorieVociBase p_bean) {
		GWT.log("populating record", null);

		//ListGridRecord _record = new CategorieVociBaseLGR(p_bean);
		ClientUtil.appendToListGridRecord(p_bean.toHashMap(), p_record);

		/* populating given record with the created record */
//		String[] attributes = _record.getAttributes();
//		for (String attribute : attributes) {
//			p_record.setAttribute(attribute, _record.getAttributeAsObject(attribute));
//		}

		if (p_bean != null) {
			GWT.log(p_bean.getDescrizione(), null);
		}
		//p_record = new CategorieVociBaseLGR(p_bean);

		GWT.log("record dentro populate: " + p_record.getAttribute("CategorieVociBase._descrizione"), null);
	}

}
