package com.smartgwt.myexamples.client.frms;

import java.util.List;
import java.util.ListIterator;
import com.google.gwt.requestfactory.shared.Receiver;
import com.smartgwt.myexamples.client.datasources.ClientesDS;
import com.smartgwt.myexamples.client.proxys.ClienteProxy;
import com.smartgwt.myexamples.client.utils.FrmBase;
import com.smartgwt.myexamples.client.utils.RFService;
import com.smartgwt.myexamples.shared.ClienteRequest;
import com.smartgwt.myexamples.shared.TDBRequestFactory;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Window;
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.ValuesManager;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;

public class FrmValuesManager extends FrmBase implements ClickHandler {

	private TDBRequestFactory rFactory;
	
	private ValuesManager vm = new ValuesManager();
	private DataSource clientDS = new DataSource();
	private TextItem txtID;
	private TextItem txtName;
	private TextItem txtLastName;
	private TextItem txtEmail;
	private TextItem txtUser;
	private TextItem txtPassword;
	private DynamicForm dfNForm;
	private DynamicForm dfNForm1;
	private Button btnPopulate;
	
	public FrmValuesManager() {
		super();
		setAutoCenter(true);
		setTitle("Using Values Manager");
		setWidth(400);
		setHeight(260);
		
		rFactory = RFService.getERF();

		initDs();		
				
		vm.setDataSource(clientDS);
		
		dfNForm = new DynamicForm();
		dfNForm.setBorder("1px solid");
		dfNForm.setWidth100();
		dfNForm.setNumCols(2);
		dfNForm.setColWidths("20%", "80%");
		dfNForm.setPadding(5);
		dfNForm.setDataSource(clientDS);
		dfNForm.setUseAllDataSourceFields(false);
		dfNForm.setFields(
			getTxtID(),
			getTxtName(),
			getTxtLastName()
		);
		
		dfNForm.setValuesManager(vm);
		
		dfNForm1 = new DynamicForm();
		dfNForm1.setBorder("1px solid");
		dfNForm1.setWidth100();
		dfNForm1.setNumCols(2);
		dfNForm1.setColWidths("20%", "80%");
		dfNForm1.setPadding(5);
		dfNForm1.setDataSource(clientDS);
		dfNForm1.setUseAllDataSourceFields(false);
		dfNForm1.setFields(
			getTxtEmail(),
			getTxtUser(),
			getTxtPassword()
		);
		dfNForm1.setValuesManager(vm);
		
		vm.addMember(dfNForm);
		vm.addMember(dfNForm1);

		getBtnPopulate().addClickHandler(this);
		
		VLayout vlayout = new VLayout();
		vlayout.setLayoutMargin(10);
		vlayout.setMembersMargin(5);
		
		vlayout.addMember(dfNForm);
		vlayout.addMember(dfNForm1);
		vlayout.addMember(getBtnPopulate());
		addItem(vlayout);
	}

	private void initDs() {
		DataSourceTextField nif = new DataSourceTextField("id","Id"); 
		nif.setPrimaryKey(true);
		DataSourceTextField name = new DataSourceTextField("name","Name");
		DataSourceTextField lastname = new DataSourceTextField("lastname","Last Name");
		DataSourceTextField email = new DataSourceTextField("email","E-mail");
		DataSourceTextField user = new DataSourceTextField("user","User");
		DataSourceTextField password = new DataSourceTextField("password","Password");
		clientDS.setFields(nif,name,lastname,email,user,password);
		clientDS.setClientOnly(true);
	}
	
	public void loadData() {
		ClienteRequest cRequest = rFactory.clienteRequest();
		cRequest.getAllClientes().to(new Receiver<List<ClienteProxy>>() {
			@Override
			public void onSuccess(List<ClienteProxy> response) {
				populateDS(response.listIterator());
				
				SC.say("Loaded  " + response.size() + " clients.");
				vm.fetchData();
			}

		}).fire();
	}
	
	private void populateDS(ListIterator<ClienteProxy> data){
		while(data.hasNext()){
			ClienteProxy c= (ClienteProxy) data.next();
			ListGridRecord fila = new ListGridRecord();
			fila.setAttribute("id", c.getNif());
			fila.setAttribute("name", c.getName());
			fila.setAttribute("lastname", c.getLastName());
			fila.setAttribute("email", c.getEmail());
			fila.setAttribute("user", c.getUser());
			fila.setAttribute("password", c.getPassword());
			clientDS.addData(fila);
		}	
	}
	
	@Override
	public void onClick(ClickEvent event) {
		if (((Button)event.getSource()).equals(getBtnPopulate())) {
			loadData();
		}
	}
	
	public TextItem getTxtID() {
		if (txtID == null) {
			txtID = new TextItem("id");
			txtID.setTitle("ID");
		}
		return txtID;
	}

	public TextItem getTxtName() {
		if (txtName == null) {
			txtName = new TextItem("name");
			txtName.setTitle("Name");
		}		
		return txtName;
	}

	public TextItem getTxtLastName() {
		if (txtLastName == null) {
			txtLastName = new TextItem("lastname");
			txtLastName.setTitle("LastName");
		}				
		return txtLastName;
	}

	public TextItem getTxtEmail() {
		if (txtEmail == null) {
			txtEmail = new TextItem("email");
			txtEmail.setTitle("E-mail");
		}	
		return txtEmail;
	}

	public TextItem getTxtUser() {
		if (txtUser == null) {
			txtUser = new TextItem("user");
			txtUser.setTitle("User");
		}				
		return txtUser;
	}

	public TextItem getTxtPassword() {
		if (txtPassword == null) {
			txtPassword = new TextItem("password");
			txtPassword.setTitle("Password");
		}			
		return txtPassword;
	}
	
	public Button getBtnPopulate() {
		if (btnPopulate==null){
			btnPopulate = new Button("Populate");
		}
		return btnPopulate;
	}
}
