package com.test.dbc.client;

import java.util.ArrayList;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ListBox;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class TestDbC implements EntryPoint {
	

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final DataBaseConnAsync DataBaseConn = GWT.create(DataBaseConn.class);
	ListBox listBox = new ListBox();
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		final Button sendButton = new Button("get");
		final Label errorLabel = new Label();

		// We can add style names to widgets
		sendButton.addStyleName("sendButton");

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel rootPanel = RootPanel.get("nameFieldContainer");
		RootPanel.get("sendButtonContainer").add(sendButton, 14, 114);
		RootPanel.get("errorLabelContainer").add(errorLabel);
		
		
		rootPanel.add(listBox, 0, 0);
		listBox.setSize("76px", "108px");
		listBox.setVisibleItemCount(5);

		

		// Add a handler to close the DialogBox
//		closeButton.addClickHandler(new ClickHandler() {
//			public void onClick(ClickEvent event) {
//				dialogBox.hide();
//				sendButton.setEnabled(true);
//				sendButton.setFocus(true);
//			}
//		});

		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {

			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					getdata();
				}
			}

			@Override
			public void onClick(ClickEvent event) { 
				getdata();
			}

			private void getdata() {
				DataBaseConn.getnames(new AsyncCallback<ArrayList<String>>() {
					@Override
					public void onSuccess(ArrayList<String> result) {
						for(int i = 0 ; i < result.size() ; i++)
						    listBox.setItemText(i, result.get(i));
					}
					
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("falier") ;
					}
				});
			}
			
		}

//			/**
//			 * Send the name from the nameField to the server and wait for a response.
//			 */
////			private void sendNameToServer() {
////				// First, we validate the input.
////				errorLabel.setText("");
////				String textToServer = nameField.getText();
////				if (!FieldVerifier.isValidName(textToServer)) {
////					errorLabel.setText("Please enter at least four characters");
////					return;
////				}
////
////				// Then, we send the input to the server.
////				sendButton.setEnabled(false);
////				textToServerLabel.setText(textToServer);
////				serverResponseLabel.setText("");
////				greetingService.greetServer(textToServer,new AsyncCallback<String>() {
////							public void onFailure(Throwable caught) {
////								// Show the RPC error message to the user
////								dialogBox.setText("Remote Procedure Call - Failure");
////								serverResponseLabel.addStyleName("serverResponseLabelError");
////								serverResponseLabel.setHTML(SERVER_ERROR);
////								dialogBox.center();
////								closeButton.setFocus(true);
////							}
////
////							public void onSuccess(String result) {
////								dialogBox.setText("Remote Procedure Call");
////								serverResponseLabel.removeStyleName("serverResponseLabelError");
////								serverResponseLabel.setHTML(result);
////								dialogBox.center();
////								closeButton.setFocus(true);
////							}
////						});
////			}
////		}
////
////		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
////		nameField.addKeyUpHandler(handler);
////	}
}
}
