package eu.dnetlib.client.header;

import java.util.Date;

import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.Modal;
import com.github.gwtbootstrap.client.ui.ModalFooter;
import com.github.gwtbootstrap.client.ui.constants.BackdropType;
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.http.client.URL;
import com.google.gwt.i18n.client.DateTimeFormat;
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.header.SaveWidget.SaveListener;
import eu.dnetlib.shared.ValuesOfFields;

public class NewFeatureWidget implements IsWidget{

	
	private FlowPanel flowPanel = new FlowPanel();
	private SavingInfoServiceAsync savingInfo = GWT.create(SavingInfoService.class);
	
	public interface HideListener{
		public void onEvent();
	}
	
	private HideListener hideListener;
	
	public void setHideListener(HideListener hideListener){
		this.hideListener = hideListener;
	}
	
	public NewFeatureWidget() {
		
		designPanelNew(false);
	}
	
	private void designPanelNew(boolean refresh){
		
		final Modal newChartConfirmationPopup = new Modal();

        newChartConfirmationPopup.addStyleName("confirmationModal");
//        deleteConfirmationPopup.setAnimation(true);
        newChartConfirmationPopup.setBackdrop(BackdropType.STATIC);

        newChartConfirmationPopup.setTitle("New chart");

        HTML message = new HTML("All your current data will be lost, continue anyway?");
        newChartConfirmationPopup.add(message);
        Anchor saveAnchor = new Anchor();
        saveAnchor.setText("Click here to save your progress");
    	final Date tempDate = new Date();
		final DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("dd-MM-yyyy");
        newChartConfirmationPopup.add(saveAnchor);
        saveAnchor.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				savingInfo.save(ValuesOfFields.getInstance(), Window.Location.getPath()+""+dateTimeFormat.format(tempDate)+".xml", new AsyncCallback<Boolean>() {
					
					@Override
					public void onSuccess(Boolean result) {
						Window.Location.replace(URL.encode(Window.Location.getPath()+"/DownloadFile?filename="+Window.Location.getPath()+""+dateTimeFormat.format(tempDate)+".xml"));
					}
					
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Unexpected error!");
					}
				});
			}
		});

        FlowPanel actionButtons = new FlowPanel();
        actionButtons.addStyleName("confirmationModalButtons");

        ModalFooter modalFooter = new ModalFooter();
        modalFooter.add(actionButtons);
        newChartConfirmationPopup.add(modalFooter);

        Button no = new Button("Cancel");
        no.setType(ButtonType.DEFAULT);
        no.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                newChartConfirmationPopup.hide();
                newChartConfirmationPopup.removeFromParent();
            }
        });
        actionButtons.add(no);

        Button yes = new Button("New chart");
        yes.setType(ButtonType.SUCCESS);
        yes.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
            	Window.Location.reload();
            }
        });
        actionButtons.add(yes);
        
       
        newChartConfirmationPopup.show();
        
	}
	
	private void designPanel(boolean refresh){
		
		flowPanel.addStyleName("newFeature");
		
		flowPanel.add(new HTML("<p><b>All your current data will be lost</b></p>"));
		flowPanel.add(new HTML("<br/>"));
		flowPanel.add(new HTML("Do you want to save your current data before starting a new session?"));
		
		
		final Modal tempModal = new Modal();	
		final Date tempDate = new Date();
		final DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("dd-MM-yyyy");
		Button saveDataButton = new Button();
		saveDataButton.setText("Save..");
		
		saveDataButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				savingInfo.save(ValuesOfFields.getInstance(), dateTimeFormat.format(tempDate), new AsyncCallback<Boolean>() {
					
					@Override
					public void onSuccess(Boolean result) {
						Window.Location.replace(URL.encode(Window.Location.getPath()+"/DownloadFile?filename=/var/lib/tomcat7/webapps"+Window.Location.getPath()+"saves/"+dateTimeFormat.format(tempDate)+".xml"));
					}
					
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Unexpected error!");
					}
				});
			}
		});
		
		
		
		flowPanel.add(saveDataButton);
		
		Button okButton = new Button();
		Button cancelButton = new Button();
		
		okButton.setText("OK!");
		cancelButton.setText("Cancel");
		okButton.addStyleName("newFeature-button-ok");
		cancelButton.addStyleName("newFeature-button-cancel");
		
		okButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				Window.Location.reload();
			}
		});
		
		cancelButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if(hideListener!=null){
					hideListener.onEvent();
				}
			}
		});
		
		flowPanel.add(new HTML("<br/>"));
		flowPanel.add(new HTML("<br/>"));
		flowPanel.add(okButton);
		flowPanel.add(cancelButton);
		
	}
	
	@Override
	public Widget asWidget() {
		return flowPanel;
	}

}
