package eu.dnetlib.client.rightColumn;

import com.github.gwtbootstrap.client.ui.Alert;
import com.github.gwtbootstrap.client.ui.constants.AlertType;
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.Random;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

import eu.dnetlib.client.widgets.AlertWidget;
import eu.dnetlib.shared.TableInfoResponse;

public class DataBox extends Widget{
	
	private FlowPanel masterPanel = new FlowPanel();
	private FlexTable tableOfContent = new FlexTable();
	private TableInfoResponse tableInfoResponse;
	private HTML loadingWheel = new HTML("<div class=\"loader-big\"></div><div class=\"whiteFilm\"></div>"); 
	private AlertWidget infoAlert = new AlertWidget("Could not retrieve JSON", AlertType.ERROR);
	
	public DataBox(){
		
		RootPanel.get("tableChart").clear();
		//designFakeDataBox();
	}
	
	public DataBox(String URL){
		
		RootPanel.get("tableChart").add(infoAlert);
		RootPanel.get("tableChart").addStyleName("loading");
		RootPanel.get("tableChart").add(loadingWheel);
		bringTableInfoData(URL);
	}
	public void designFakeDataBox(){

		
		tableOfContent.addStyleName("databox-table");
		tableOfContent.getRowFormatter().addStyleName(0, "databox-header");
		tableOfContent.setText(0, 0, "Values of categories");
		tableOfContent.setText(0, 1, "Values of category 1");
		tableOfContent.setText(0, 2, "Values of category 2");
		tableOfContent.setText(0, 3, "Values of category 3");
		
		
		for(int i=1;i<11;i++){
			tableOfContent.getRowFormatter().addStyleName(i, "databox-data");
			tableOfContent.setText(i, 0, "Fake category "+i);
			tableOfContent.setText(i, 1, Random.nextInt(100)+"");
			tableOfContent.setText(i, 2, Random.nextInt(100)+"");
			tableOfContent.setText(i, 3, Random.nextInt(100)+"");
		}

		
		masterPanel.add(tableOfContent);
		masterPanel.addStyleName("databox");
		
		RootPanel.get("tableChart").add(masterPanel);
		
	}
	
	public void designDataBox(){
		
		RootPanel.get("tableChart").clear();
		
		tableOfContent.addStyleName("databox-table");
		tableOfContent.getRowFormatter().addStyleName(0, "databox-header");
		tableOfContent.setText(0, 0, "Values of categories");
		int counter = 0;
		for(int i=0;i<tableInfoResponse.getResponse().length();i++){//adding the tiles at the top of the table
			for(int j=0;j<tableInfoResponse.getResponse().get(i).getTitles().length();j++){
				counter++;
				if(tableInfoResponse.getResponse().get(i).getTitles().get(j).equals("empty")){
					tableOfContent.setText(0,counter,"Series "+ counter);
				}else{
					tableOfContent.setText(0,counter,tableInfoResponse.getResponse().get(i).getTitles().get(j)+"");
				}
				
			}
		}
		
		for(int i=0;i<tableInfoResponse.getResponse().length();i++){//adding the main values (1st column)
			for(int j=0;j<tableInfoResponse.getResponse().get(i).getNaming().length();j++){
				tableOfContent.getRowFormatter().addStyleName(j+1, "databox-data");
				tableOfContent.setText(j+1, 0, tableInfoResponse.getResponse().get(i).getNaming().get(j).toString());
			}
		}
			counter=0;
		for(int i=0;i<tableInfoResponse.getResponse().length();i++){//adding the rest values(all the other columns)
			for(int j=0;j<tableInfoResponse.getResponse().get(i).getValues().length();j++){
				counter++;
				for(int y=0;y<tableInfoResponse.getResponse().get(i).getValues().get(j).length();y++){
					tableOfContent.setText(y+1, counter , Integer.parseInt(tableInfoResponse.getResponse().get(i).getValues().get(j).get(y))+"");
				}
			}
		}
		
		masterPanel.add(tableOfContent);
		masterPanel.addStyleName("databox");
		RootPanel.get("tableChart").add(masterPanel);
		
	}
	
	public void bringTableInfoData(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){
							tableInfoResponse = JsonUtils.safeEval(response.getText().toString());
							RootPanel.get("tableChart").removeStyleName("loading");
							RootPanel.get("tableChart").remove(loadingWheel);
							designDataBox();
							
						}else{
							RootPanel.get("tableChart").clear();
							RootPanel.get("tableChart").add(infoAlert);
							RootPanel.get("tableChart").removeStyleName("loading");
							RootPanel.get("tableChart").remove(loadingWheel);
							infoAlert.trigger();
						}
					}
					
					@Override
					public void onError(Request request, Throwable exception) {
						RootPanel.get("tableChart").clear();
						RootPanel.get("tableChart").add(infoAlert);
						RootPanel.get("tableChart").removeStyleName("loading");
						RootPanel.get("tableChart").remove(loadingWheel);
						infoAlert.trigger();
					}
				});
			} catch (RequestException e) {
				RootPanel.get("tableChart").clear();
				RootPanel.get("tableChart").add(infoAlert);
				RootPanel.get("tableChart").removeStyleName("loading");
				RootPanel.get("tableChart").remove(loadingWheel);
				infoAlert.trigger();
			}
	  }
	
	  public void DisplayError(String msg){
			Window.alert(msg);
			
	  }
}
