package eu.dnetlib.client.header;

import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.Form;
import com.github.gwtbootstrap.client.ui.Paragraph;
import com.github.gwtbootstrap.client.ui.TextBox;
import com.github.gwtbootstrap.client.ui.constants.ButtonType;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
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.widgets.FormFieldSet;
import eu.dnetlib.shared.ValuesOfFields;

public class SaveWidget implements IsWidget{

	private FlowPanel flowPanel = new FlowPanel();
	private SavingInfoServiceAsync savingInfo = GWT.create(SavingInfoService.class);
	
	
	public interface SaveListener {
		public void onEvent();
	}
	
	private SaveListener saveListener;
	
	public void setSaveListener(SaveListener saveListener){
		this.saveListener = saveListener;
	}
	
	public SaveWidget() {
		designPanel();
	}
	
	public void designPanel() {
		
		final Paragraph infoParagraph = new Paragraph();
		infoParagraph.setVisible(false);
		infoParagraph.addStyleName("infoParagraph");
		final Anchor getFile = new Anchor();
		getFile.setText("Download file");
		
		flowPanel.addStyleName("saveFeature");

		final Button saveButton = new Button();
		saveButton.setText("Save!");
		saveButton.setType(ButtonType.DANGER);
		saveButton.setEnabled(false);
	
		
		Form nameForm = new Form();
		final TextBox nameTextBox = new TextBox();
		nameTextBox.addKeyDownHandler(new KeyDownHandler() {
			
			@Override
			public void onKeyDown(KeyDownEvent event) {
				infoParagraph.setVisible(true);
				String filename = nameTextBox.getText();
				filename=filename.concat(".xml");
				if(nameTextBox.getText().equals("")){
					infoParagraph.setText("");
					nameTextBox.getElement().getStyle().setBackgroundColor("white");
					saveButton.setEnabled(false);
					saveButton.setType(ButtonType.DANGER);
				}else{
					savingInfo.checkFilename(filename, new AsyncCallback<Boolean>() {
						
						@Override
						public void onSuccess(Boolean result) {
							if(result){
								saveButton.setType(ButtonType.DANGER);
								saveButton.setEnabled(false);
								infoParagraph.setText("A file with this name already exists!");
								nameTextBox.getElement().getStyle().setBackgroundColor("#EBA3A3");
								getFile.setHref(Window.Location.getHost()+Window.Location.getPath()+"DownloadFile?filename=/var/lib/tomcat7/webapps"+Window.Location.getPath()+"saves/"+nameTextBox.getText()+".xml");
							}else{
								saveButton.setType(ButtonType.SUCCESS);
								saveButton.setEnabled(true);
								infoParagraph.setText("Filename available!");
								nameTextBox.getElement().getStyle().setBackgroundColor("#92FAC6");
							}
						}
						
						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Unexcepted error!"+caught.toString());
						}
					});
				}
			}
		});
		
		saveButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				savingInfo.save(ValuesOfFields.getInstance(), nameTextBox.getText(), new AsyncCallback<Boolean>() {
					
					@Override
					public void onSuccess(Boolean result) {
						if(saveListener!=null){
							saveListener.onEvent();
						}
						Window.Location.replace(URL.encode(Window.Location.getPath()+"/DownloadFile?filename=/var/lib/tomcat7/webapps"+Window.Location.getPath()+"saves/"+nameTextBox.getText()+".xml"));
					}
					
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Unexpected error!");
					}
				});
			}
		});
		
		
		FormFieldSet nameFieldSet = new FormFieldSet("Name..", nameTextBox);
		nameForm.add(nameFieldSet);
		
		
		flowPanel.add(new HTML("<h3>Save your data</h3>"));
		flowPanel.add(new HTML("<br/><br/>"));
		flowPanel.add(nameForm);
		flowPanel.add(infoParagraph);
		flowPanel.add(new HTML("<br/>"));
		flowPanel.add(saveButton);
		flowPanel.add(new HTML("<br/>"));
		flowPanel.add(getFile);
		
		
	}
	
	@Override
	public Widget asWidget() {
		return flowPanel;
	}

}
