package eu.dnetlib.client;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;

import eu.dnetlib.client.rightColumn.DataBox;
import eu.dnetlib.shared.ValuesOfFields;

public class Generate {

	private static Generate instance = new Generate();
	private Frame theme1 = new Frame();
	private HTML loadingWheel = new HTML("<div class=\"loader-big\"></div><div class=\"whiteFilm\"></div>");
	
	private Generate(){
		theme1.addLoadHandler(new LoadHandler() {
			
			@Override
			public void onLoad(LoadEvent event) {
				RootPanel.get("highchart").removeStyleName("loading");
				RootPanel.get("highchart").remove(loadingWheel);
			}
		});
	}
	
	public static Generate getInstance() {
		return instance;
	}

	public Frame getTheme1() {
		return this.theme1;
	}


	public void setTheme1(Frame theme1) {
		this.theme1 = theme1;
	}

		public String generateURLs(ValuesOfFields fieldsValues ,int WhoToReturn){
			String URL_theme1="http://dl114.madgik.di.uoa.gr/stats/merge.php?com=query&data=[";
			String URL_theme2;
			String URL_theme3;
			ArrayList<Integer> whoToEnable = new ArrayList<Integer>();
			for(int y=0;y<ValuesOfFields.getInstance().getColumnsDataList().size();y++){
				if(fieldsValues.getColumnsDataList().get(y).isEnabled()){
					whoToEnable.add(y);
				}
			}
			for(int y=0;y<whoToEnable.size();y++){
				//if(fieldsValues.getColumnsDataList().get(y).isEnabled()){
					if(fieldsValues!=null){
					
						URL_theme1=URL_theme1.concat("{\"table\":\""+fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getDataSource()+"\",");
						URL_theme1=URL_theme1.concat("\"fields\":[");
						URL_theme1=URL_theme1.concat("{\"fld\":\""+fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getY_values()+"\",\"agg\":\""+fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getTypeOf().replaceAll(" of", "")+"\",\"type\":\"column\",\"yaxis\":"+(1)+",\"c\":"+fieldsValues.isCumulative()+"}");
						
						
						URL_theme1=URL_theme1.concat("],");
						
						URL_theme1=URL_theme1.concat("\"xaxis\":{\"name\":\""+fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getX_values()+"\",\"agg\":\""+fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getTypeOf().replaceAll(" of", "")+"\"},");
						URL_theme1=URL_theme1.concat("\"group\":\""+fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getSplitBy()+"\",\"color\":\"\",\"type\":\"chart\",\"size\":"+fieldsValues.getNumberOfResults()+",\"sort\":\"xaxis\",\"xStyle\":{\"r\":"+fieldsValues.getRotation()+",\"s\":\""+fieldsValues.getUseSteps()+"\",\"l\":\"-\",\"ft\":\"-\",\"wt\":\"-\"},");
						URL_theme1=URL_theme1.concat("\"title\":\""+fieldsValues.getTitle()+"\",\"subtitle\":\""+fieldsValues.getSubtitle() +"\",\"xaxistitle\":\""+fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getXaxisTitle()+"\",\"yaxisheaders\":[\""+fieldsValues.getYaxis()+"\"],\"generalxaxis\":\""+fieldsValues.getXaxis()+"\",");
						
					}
				
					
					if(fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getFilters().size()==0){
						URL_theme1=URL_theme1.concat("\"theme\":"+0+",\"in\":[]");
					}else{
						URL_theme1=URL_theme1.concat("\"theme\":"+0+",\"in\":[],\"filters\":[");
						
						for(int filtNum=0;filtNum<fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getFilters().size();filtNum++){
							int counter=0;
							URL_theme1=URL_theme1.concat("{\"name\":\""+fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getFilters().get(filtNum).getFieldValue()+"\",");
							if(fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getFilters().get(filtNum).getType().equals("Discrete")){
								URL_theme1 = URL_theme1.concat("\"values\":[");
								for(int discNum=0;discNum<fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getFilters().get(filtNum).getDiscreteValues().size();discNum++){
									if(counter>0){
										URL_theme1 = URL_theme1.concat(",");
									}
									URL_theme1 = URL_theme1.concat("\""+fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getFilters().get(filtNum).getDiscreteValues().get(discNum)+"\"");
									counter++;
								}
								
								URL_theme1 = URL_theme1.concat("],");
							}else if(fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getFilters().get(filtNum).getType().equals("Continuous")){
								URL_theme1 = URL_theme1.concat("\"max\":\""+fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getFilters().get(filtNum).getValueTo()+"\",\"min\":\""+fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getFilters().get(filtNum).getValueFrom()+"\",");
							}
							URL_theme1 = URL_theme1.concat("\"to\":\"-1\"}");
							if(filtNum!=fieldsValues.getColumnsDataList().get(whoToEnable.get(y)).getFilters().size()-1){
								URL_theme1 = URL_theme1.concat(",");
							}
						}
						URL_theme1=URL_theme1.concat("]");
					}
						
					
					if(y!=whoToEnable.size()-1){
						URL_theme1=URL_theme1.concat("},");
					}else{
						URL_theme1=URL_theme1.concat("}");
					}
		
			}
			
				URL_theme1=URL_theme1.concat("]");
			
			int option = fieldsValues.getOptionSelected();

			URL_theme1 = URL_theme1.concat("&info_types=[");
			boolean percent = false;
			boolean normal = false;
			boolean steps = false;
			boolean threeD = false;
			boolean donut = false;
			boolean none = false;
			
			for(int i=0;i<whoToEnable.size();i++){
				URL_theme1 = URL_theme1.concat("\""+fieldsValues.getColumnsDataList().get(whoToEnable.get(i)).getTypeOfChart()+"\"");
				if(i!=whoToEnable.size()-1){
					URL_theme1 = URL_theme1.concat(",");
				}else{
					URL_theme1 = URL_theme1.concat("]");
				}
				if(ValuesOfFields.getInstance().getColumnsDataList().get(whoToEnable.get(i)).getStacking().equals("donut")){
					donut=true;
				}else if(ValuesOfFields.getInstance().getColumnsDataList().get(whoToEnable.get(i)).getStacking().equals("normal")){
					normal=true;
				}else if(ValuesOfFields.getInstance().getColumnsDataList().get(whoToEnable.get(i)).getStacking().equals("percent")){
					percent = true;
				}else if(ValuesOfFields.getInstance().getColumnsDataList().get(whoToEnable.get(i)).getStacking().equals("steps")){
					steps = true;
				}else if(ValuesOfFields.getInstance().getColumnsDataList().get(whoToEnable.get(i)).getStacking().equals("3D")){
					threeD=true;
				}else if(ValuesOfFields.getInstance().getColumnsDataList().get(whoToEnable.get(i)).getStacking().equals("none")){
					none=true;
				}
			}
		
			if(percent){
				URL_theme1 = URL_theme1.concat("&stacking=percent");
			}else if(normal){
				URL_theme1 = URL_theme1.concat("&stacking=normal");
			}else if(donut){
				URL_theme1 = URL_theme1.concat("&stacking=donut");
			}else if(none){
				URL_theme1 = URL_theme1.concat("&stacking=");
			}
			
			percent = false;
			normal = false;
			steps = false;
			threeD = false;
			donut = false;
			
			if(steps){
				URL_theme1 = URL_theme1.concat("&steps=true");
			}else{
				URL_theme1 = URL_theme1.concat("&steps=false");
			}
			
				URL_theme1 = URL_theme1.concat("&spacing=["+fieldsValues.getSpacingTop()+","+fieldsValues.getSpacingRight()+","+fieldsValues.getSpacingBottom()+","+fieldsValues.getSpacingLeft()+"]");
				URL_theme1 = URL_theme1.concat("&style=[{\"color\":\""+fieldsValues.getColorTitle()+"\",\"size\":\""+fieldsValues.getSizeTitle()+"\"},{\"color\":\""+fieldsValues.getColorSubtitle()+"\",\"size\":\""+fieldsValues.getSizeSubtitle()+"\"},{\"color\":\""+fieldsValues.getColorXaxis()+"\",\"size\":\""+fieldsValues.getSizeXaxis()+"\"},{\"color\":\""+fieldsValues.getColorYaxis()+"\",\"size\":\""+fieldsValues.getSizeYaxis()+"\"}]");
				
				URL_theme2=URL_theme1;
				URL_theme3=URL_theme1;
				
				
				RootPanel.get("highchart").addStyleName("loading");
				RootPanel.get("highchart").add(loadingWheel);
				
				
				this.getTheme1().setUrl(URL.encode(URL_theme1));
				generateTableURLs(fieldsValues,URL_theme1);
				if(WhoToReturn==1){
					return URL_theme1;
				}else if(WhoToReturn==2){
					return URL_theme2;
				}else if(WhoToReturn==3){
					return URL_theme3;
				}else{
					return null;
				}
			
	}
		
		public String generateTableURLs(ValuesOfFields fieldsValues,String URL){
				
				new DataBox(URL.replace("merge.php", "infoForTable.php"));
			
				return URL;
			
	}
	
}
