package com.curiam.client;

import gwtupload.client.IUploader;
import gwtupload.client.MultiUploader;
import gwtupload.client.PreloadedImage;
import gwtupload.client.IUploadStatus.Status;
import gwtupload.client.IUploader.Utils;
import gwtupload.client.PreloadedImage.OnLoadPreloadedImageHandler;

import com.google.gwt.core.client.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.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.XMLParser;

	/*****************************/
	/*****************************/
	/** Análisis de un paciente **/
	/*****************************/
	/*****************************/


@SuppressWarnings("deprecation")
public class Analisis {
	
	
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);
	
	
	public Analisis(final String usuario){
		final String user = "config/us_" + usuario + ".xml";
		Acceso a = new Acceso();
		a.analizar_xml(user);
		
		HorizontalPanel hPanel = new HorizontalPanel();
	    hPanel.setSpacing(5);

		
		final Button b_nuevo = new Button("<font size=2>Nuevo Analisis</font>",
				
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						Widget panel = RootPanel.get().getWidget(0);
						RootPanel.get().clear();
						RootPanel.get().add(panel, 30, 10);
						Acceso a = new Acceso();
						a.analizar_xml(user);
					}
	    });
		
		
		final DialogBox db = new DialogBox();
		final Grid vp = new Grid(5,1);
		
		final CellFormatter izq = vp.getCellFormatter();
		izq.setHorizontalAlignment(4, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		VerticalPanel vp_aux = new VerticalPanel();
		
		final FlowPanel panelImages = new FlowPanel();
		
		// Attach an image to the pictures viewer
		  final OnLoadPreloadedImageHandler showImage = new OnLoadPreloadedImageHandler() {
		    public void onLoad(PreloadedImage image) {
		      image.setWidth("75px");
		      panelImages.add(image);
		    }
		  };
		
		
		IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
		    public void onFinish(IUploader uploader) {
		      if (uploader.getStatus() == Status.SUCCESS) {

		        new PreloadedImage(uploader.fileUrl(), showImage);
		        
		        // The server can send information to the client.
		        // You can parse this information using XML or JSON libraries
		        Document doc = XMLParser.parse(uploader.getServerResponse());
		        String size = Utils.getXmlNodeValue(doc, "file-1-size");
		        String type = Utils.getXmlNodeValue(doc, "file-1-type");
		        System.out.println("["+size+"]" + " " + "["+type+"]");
		      }
		    }
		  };
		
		
		
		

		    
		    // Create a new uploader panel and attach it to the document
		    MultiUploader defaultUploader = new MultiUploader();
		

		    // Add a finish handler which will load the image once the upload finishes
		    defaultUploader.addOnFinishUploadHandler(onFinishUploaderHandler);
		  

		  

		  

		
		
		
		
		
		
		
		
		
		
		
		
	    //RootPanel.get().add(form);
	  

		
		//FileUploader fup = new FileUploader(vp_aux);
		
		
	    // Add a button to upload the file
	  /*  Button uploadButton = new Button("<font size=2>Cargar</font>");
	    uploadButton.addClickHandler(new ClickHandler() {
	      public void onClick(ClickEvent event) {
	        String filename = fileUpload.getFilename();
	        
	        if (filename.length() == 0) {
	          Window.alert("Error al subir la imagen");
	        } else {
	        	
	        	String path = fileUpload.getElement().getDir();
	        	System.out.println("····" + path + filename);
	        	String fich2 = "img_cer/" + filename;
	        	
	        	System.out.println(filename);
	        	System.out.println(fich2);
	        	
	        	GreetingServiceAsync acc = GWT.create(GreetingService.class);
	    		acc.subir_img(path + filename,fich2, new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(Boolean result) {
						// TODO Auto-generated method stub
						Window.alert("Imagen subida");
						
					}
	    		
	        });
	       
	        	}
	        }
	      });  */
	    ////////
		    final VerticalPanel vpp = new VerticalPanel();
		    Button b_form = new Button("<font size=2>getting Started</font>",
			        new ClickHandler() {
			          public void onClick(ClickEvent event) {
			        	  GettingStarted gs = new GettingStarted(vpp);
			          }
			});    
		    
		    
	   Button b_cerrar = new Button("<font size=2>Cerrar</font>",
		        new ClickHandler() {
		          public void onClick(ClickEvent event) {
		            db.hide();
		          }
		});
	  
		 //////	
	    vp.setCellSpacing(5);
		//vp.setWidget(0, 0, panelImages);
		//vp.setWidget(1, 0, defaultUploader);
	    vp.setWidget(2, 0, b_form);
	    vp.setWidget(3, 0, vpp);
		vp.setWidget(4, 0, b_cerrar);
		
		db.setText("Panel Imagen");
	    db.setGlassEnabled(true);
	    db.setAnimationEnabled(true);
	    db.add(vp);
	    
	    
		
		final Button b_imagen = new Button("<font size=2>Im&aacute;genes</font>",
				
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						db.center();
						db.show();
						/*Widget panel = RootPanel.get().getWidget(0);
						RootPanel.get().clear();
						RootPanel.get().add(panel, 30, 10);
						Acceso a = new Acceso();
						a.analizar_xml(user);*/
					}
	    });
		
		hPanel.add(b_nuevo);
		hPanel.add(b_imagen);
		hPanel.addStyleName("borde_base");
		
		RootPanel.get().clear();
		RootPanel.get().setStyleName("fondo_blanco");
		RootPanel.get().add(hPanel, 30, 10);
	
		}
	
}