package com.wipro.SmartGWTProject.client;

import java.util.HashMap;
import java.util.List;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Widget;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.data.ResultSet;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Label;
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.FormItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.VStack;
import com.wipro.SmartGWTProject.shared.FieldVerifier;
import com.wipro.SmartGWTProject.server.LoginDMI;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class SmartGWT implements EntryPoint {

	// initialising error label

	final Label errorLabel = new Label("");
	DataSource loginDS;

	public void onModuleLoad() {

		/*
		 * for List Grid
		 */

		// System.out.println(DataSource.get("login"));
		// final ListGrid loginGrid = new ListGrid();
		// loginGrid.setWidth(100); loginGrid.setHeight(100);
		// loginGrid.setDataSource(DataSource.get("login"));
		// loginGrid.setShowAllRecords(true);
		// loginGrid.setAutoFetchData(true);
		// loginGrid.draw();

		// creating header
		final Label heading = new Label();
		heading.setContents("<b>LOGIN</b>");
		heading.setHeight(30);

		// creating form
		final DynamicForm dynamicForm = new DynamicForm();
		dynamicForm.setWidth(100);
		dynamicForm.setHeight(100);

		// creating textitem for username
		final TextItem userNameItem = new TextItem();
		userNameItem.setTitle("UserName:");
		userNameItem.setName("username");
		userNameItem.setLength(40);

		// creating textitem for password
		final PasswordItem passwordItem = new PasswordItem();
		passwordItem.setTitle("Password:");
		passwordItem.setName("password");
		passwordItem.setLength(40);

		// defining error Label
		errorLabel.setStyleName("serverResponseLabelError");
		errorLabel.setWidth(200);
		errorLabel.setHeight(3);

		// defining button
		final Button submit = new Button();
		submit.setTitle("Submit");

		dynamicForm.setFields(new FormItem[] { userNameItem, passwordItem });

		// getting Data Source
		try {
			loginDS = DataSource.getDataSource("login");
		} catch (Exception e) {
			System.out.println("exception thrown" + e.getMessage());
		}

		// setting Data Source
		dynamicForm.setDataSource(loginDS);
		dynamicForm.setHeight(50);

		// setting layout
		final VStack vStack = new VStack();
		vStack.setMembers(heading, dynamicForm, errorLabel, submit);
		vStack.setMargin(10);
		vStack.setAlign(Alignment.RIGHT);
		vStack.setStyleName("tableContainer-2");
		vStack.show();
		// dynamicForm.getValuesAsCriteria();
		// registering listener
		submit.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

			
				  if (validate(dynamicForm.getValueAsString("username"),
				  dynamicForm.getValueAsString("password")))
				  loginDS.fetchData(new
				  Criteria("username",dynamicForm.getValueAsString
				  ("username")), new DSCallback() {
				  
				  @Override public void execute(
				  com.smartgwt.client.data.DSResponse response, Object rawData,
				  com.smartgwt.client.data.DSRequest request) {
				  Record[] record=response.getData();
				    
					  
//				  ResultSet rs = new ResultSet(loginDS);
//				  rs.setAllRows(response.getData()); if(rs.getLength()==0) {
//				  errorLabel .setContents("<p>Username does not exist</p>");
//				  return; } if
//				  (rs.find("password",dynamicForm.getValueAsString("password")
//				  )!=null) { dynamicForm.hide(); submit.hide();
//				  errorLabel.hide(); heading.hide(); vStack.addMember(new
//				  Label("<b>WELCOME</b>")); } else { errorLabel
//				  .setContents("<p>Authentication Failed</p>"); }
				  
				  } });
				 

				

			}
		});

	}

	// validating text items in form
	boolean validate(String userName, String pasword) {
		FieldVerifier fieldVerifier = new FieldVerifier();
		if (!fieldVerifier.isValidId(userName)) {
			errorLabel.setContents("<p>Username cannot be empty</p>");
			return false;
		} else {
			errorLabel.setContents("");
		}
		if (!fieldVerifier.isValidId(pasword)) {
			errorLabel.setContents("<p>Password cannot be empty</p>");
			return false;
		} else {
			errorLabel.setContents("");
		}
		return true;
	}

}
