package eu.dnetlib.client;

import com.github.gwtbootstrap.client.ui.Alert;
import com.github.gwtbootstrap.client.ui.constants.AlertType;
import com.google.gwt.canvas.dom.client.Context2d.TextAlign;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JsonUtils;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;

import eu.dnetlib.client.header.SiteHeader;
import eu.dnetlib.shared.SchemaAnswer;
import eu.dnetlib.shared.ValuesOfFields;

public class StatsConfig implements EntryPoint {

	private static String URL_FOR_SCHEMA = new String("http://vatopedi.di.uoa.gr/testMVN-1.0-SNAPSHOT/test.php");
	private SchemaAnswer schemaFromServer;
	private Alert infoAlert = new Alert();
	
  public void onModuleLoad() {
        
	  infoAlert.setType(AlertType.ERROR);
	  infoAlert.setText("Could not retrieve json");
	  infoAlert.setVisible(false);
	  infoAlert.getElement().getStyle().setProperty("text-align", "center");
	  RootPanel.get("block-header").add(infoAlert);
	  
	  bringDatData(/*FROM*/URL_FOR_SCHEMA);

  }
  
  public void bringDatData(String url){
	  
	  RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
		
		try {
			
			
			Request request = builder.sendRequest(null, new RequestCallback() {
				
				@Override
				public void onResponseReceived(Request request, Response response) {
										
					if(response.getStatusCode()==200){
						
						schemaFromServer = JsonUtils.safeEval(response.getText().toString());
						
						new SiteHeader(schemaFromServer);
						new StartingModal(schemaFromServer);

					}else{
						//DisplayError("Could not retrieve JSON");
						infoAlert.setVisible(true);
					}
				}
				
				@Override
				public void onError(Request request, Throwable exception) {
					// TODO Auto-generated method stub
					//DisplayError("Could not retrieve JSON ");
					infoAlert.setVisible(true);
				}
			});
		} catch (RequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//DisplayError("Could not retrieve JSON");
			infoAlert.setVisible(true);
		}
  }
  
  public void DisplayError(String msg){
		Window.alert(msg);
		
	}
}
