package customformitem.client;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.EnterKeyEditAction;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.Layout;


public class CustomItemPanel extends Layout {

    DataSource ds = CountryDS.getInstance();
    
    ListGrid g = new ListGrid();
    
    public CustomItemPanel() {
        init();
    }
    
    public void init() {

        g.setDataSource(ds);
        g.setCanEdit(true);
        g.setEnterKeyEditAction(EnterKeyEditAction.DONE);
        g.setModalEditing(true);
        g.setAutoSaveEdits(true);
        g.setSelectionType(SelectionStyle.SINGLE);
        g.setHeight(500);

        this.addMember(g);
    }

    public void showAction(Map passThrough, boolean enteringFromAbove) {
        
        g.show();
        g.fetchData();
    }
    
    private static class CountryDS extends DataSource {
        // The DataSource would normally be defined external to any classes that use it.

        private static CountryDS instance = null;
        
        public static CountryDS getInstance() {
            if (instance == null) {
              instance = new CountryDS("countryDS_XML");
            }
            return instance;
        }

        public CountryDS(String id) {
            setID(id);

            this.setClientOnly(true);
            DataSourceField field1 = new DataSourceTextField("id", "ID");
            DataSourceField field2 = new DataSourceTextField("customField", "Custom");
            
            TheFormItem theFormItem = new TheFormItem("customField");
            field2.setEditorType(theFormItem);
            
            setFields(field1, field2);
            
        }
        
        protected java.lang.Object transformRequest(DSRequest dsRequest) {

            if (dsRequest.getOperationType() == DSOperationType.FETCH) {
                fetch(dsRequest);
            } else if (dsRequest.getOperationType() == DSOperationType.ADD) {
                addRow(dsRequest);
            } else if (dsRequest.getOperationType() == DSOperationType.UPDATE) {
                updateRow(dsRequest);
            } else if (dsRequest.getOperationType() == DSOperationType.REMOVE) {
                deleteRow(dsRequest);
            }

            // no need to return payload to caller
            return "transformReturnString";
        }

        private void fetch(DSRequest dsRequest) {
            List<ListGridRecord> records = new ArrayList<ListGridRecord>();

            ListGridRecord r = new ListGridRecord();
            r.setAttribute("id", "1");
            r.setAttribute("customField","custom1");
            records.add(r);
            
            r = new ListGridRecord();
            r.setAttribute("id", "2");
            r.setAttribute("customField","custom2");
            records.add(r);
            
            DSResponse response = new DSResponse();
            response.setStatus(DSResponse.STATUS_SUCCESS);
            response.setData(records.toArray(new ListGridRecord[0]));
            this.processResponse(dsRequest.getRequestId(), response);
        }
        
        // we receive a single row/record which is to be added to the database
        private void addRow(DSRequest dsRequest) {
        }
        private void updateRow(DSRequest dsRequest) {
        }
        private void deleteRow(DSRequest dsRequest) {
        }
    }

}
