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 longUrl = new TextBox();
	private TextBox tinyUrlTextBox = new TextBox();
	private TinyURL tinyURL ;
	private AlertWidget responseAlert;
	
	public PublishWidget() {
		Form urlForm = new Form();
		urlForm.setType(FormType.HORIZONTAL);
		generateTiny();
		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/>"));
		longUrl.setAlternateSize(AlternateSize.LARGE);
		tinyUrlTextBox.setAlternateSize(AlternateSize.LARGE);
		tinyUrlTextBox.setPlaceholder("Loading tiny url..");
		longUrl.setText(URL.encode(Generate.getInstance().generateURLs(ValuesOfFields.getInstance(), 1)));
		urlForm.add(new FormFieldSet("Long url", longUrl));
		urlForm.add(new FormFieldSet("Tiny url",tinyUrlTextBox));
		
		
		masterFlowPanel.add(urlForm);
		
	}
	
	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);
	}
	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) {
				tinyUrlTextBox.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;
	}

}
