package eu.dnetlib.client.header;



import com.github.gwtbootstrap.client.ui.Form;
import com.github.gwtbootstrap.client.ui.TextBox;
import com.github.gwtbootstrap.client.ui.constants.AlertType;
import com.github.gwtbootstrap.client.ui.constants.AlternateSize;
import com.github.gwtbootstrap.client.ui.constants.FormType;
import com.google.gwt.http.client.URL;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;

import eu.dnetlib.client.Generate;
import eu.dnetlib.client.widgets.AlertWidget;
import eu.dnetlib.client.widgets.FormFieldSet;
import eu.dnetlib.shared.TinyURL;
import eu.dnetlib.shared.ValuesOfFields;

public class PublishWidget implements IsWidget{

	private FlowPanel masterFlowPanel = new FlowPanel();
	private TextBox longUrlChart = new TextBox();
	private TextBox tinyUrlChartTextBox = new TextBox();
	private TinyURL tinyURLChart ;
	private TextBox longUrlTable = new TextBox();
	private TextBox tinyUrlTableTextBox = new TextBox();
	private TinyURL tinyURLTable;
	private AlertWidget responseAlert;
	
	public PublishWidget() {
		Form urlFormChart = new Form();
		urlFormChart.setType(FormType.HORIZONTAL);
		
		Form urlFormTable = new Form();
		urlFormTable.setType(FormType.HORIZONTAL);
		
		generateTiny();
		generateTinyForTable();
		responseAlert = new AlertWidget("Could not retrieve JSON", AlertType.ERROR);
		masterFlowPanel.add(responseAlert.asWidget());
		masterFlowPanel.add(new HTML("<h3 style='text-align:center;'>Chart's generated URL</h3>"));
		masterFlowPanel.add(new HTML("<br/>"));
		longUrlChart.setAlternateSize(AlternateSize.LARGE);
		
		tinyUrlChartTextBox.setAlternateSize(AlternateSize.LARGE);
	
		tinyUrlChartTextBox.setPlaceholder("Loading tiny url for chart..");

		
		longUrlChart.setText(URL.encode(Generate.getInstance().generateURLs(ValuesOfFields.getInstance(), 2)));
		
		urlFormChart.add(new FormFieldSet("Long url for chart", longUrlChart));
		urlFormChart.add(new FormFieldSet("Tiny url for chart",tinyUrlChartTextBox));
		
		
		masterFlowPanel.add(urlFormChart);
		
		masterFlowPanel.add(new HTML("<h3 style='text-align:center;'>Table's generated URL</h3>"));
		masterFlowPanel.add(new HTML("<br/>"));
		longUrlTable.setAlternateSize(AlternateSize.LARGE);
		tinyUrlTableTextBox.setAlternateSize(AlternateSize.LARGE);
		tinyUrlTableTextBox.setPlaceholder("Loading tiny url for table..");
		String finalUrlForTable = new String();
		
		finalUrlForTable = URL.encode(Generate.getInstance().generateURLs(ValuesOfFields.getInstance(), 1).replace("merge.php", "infoForTable_new.php"));
		
		longUrlTable.setText(finalUrlForTable);
		
		urlFormTable.add(new FormFieldSet("Long url", longUrlTable));
		urlFormTable.add(new FormFieldSet("Tiny url", tinyUrlTableTextBox));
		
		masterFlowPanel.add(urlFormTable);
	}
	
	public void generateTiny(){

		
		String URL = new String();
		URL = "http://tiny-url.info/api/v1/create";
		URL = URL.concat("?format=json");
		URL = URL.concat("&apikey=D5A5A7AFID7009C9AE3A");
		URL = URL.concat("&provider=tinyurl_com");
		URL = URL.concat("&url="+Generate.getInstance().generateURLs(ValuesOfFields.getInstance(), 1).replaceAll("&", "%26"));
		
		//com.google.gwt.http.client.URL.encode(

		AjaxRequest(URL);
	}
	
	public void generateTinyForTable(){

		
		String URL = new String();
		URL = "http://tiny-url.info/api/v1/create";
		URL = URL.concat("?format=json");
		URL = URL.concat("&apikey=D5A5A7AFID7009C9AE3A");
		URL = URL.concat("&provider=tinyurl_com");
		URL = URL.concat("&url="+Generate.getInstance().generateURLs(ValuesOfFields.getInstance(), 1).replaceAll("&", "%26").replace("merge.php", "infoForTable_new.php"));
		
		//com.google.gwt.http.client.URL.encode(

		AjaxRequestForTable(URL);
	}
	private void AjaxRequest (final String URL){
		

		JsonpRequestBuilder builder = new JsonpRequestBuilder();
	
		builder.requestObject(URL, new AsyncCallback<TinyURL>() {

			@Override
			public void onFailure(Throwable caught) {
				responseAlert.trigger();
			}

			@Override
			public void onSuccess(TinyURL result) {
				tinyUrlChartTextBox.setText(result.getShorturl());
			}
		});

	}
	
	private void AjaxRequestForTable (final String URL){
		

		JsonpRequestBuilder builder = new JsonpRequestBuilder();
	
		builder.requestObject(URL, new AsyncCallback<TinyURL>() {

			@Override
			public void onFailure(Throwable caught) {
				responseAlert.trigger();
			}

			@Override
			public void onSuccess(TinyURL result) {
				tinyUrlTableTextBox.setText(result.getShorturl());
			}
		});

	}
	
	public String replaceSpecialChars(String originalString){
		
		originalString.replaceAll("}", "\\}");
		originalString.replaceAll("{", "\\{");
		originalString.replaceAll("]", "\\]");
		originalString.replaceAll("[", "\\[");
		originalString.replaceAll("&", "\\&");
		
		
		return originalString;
	}
	
	@Override
	public Widget asWidget() {
		// TODO Auto-generated method stub
		return masterFlowPanel;
	}

}
