package com.curiam.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.XMLParser;

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

/**
 * 
 * @author Manolo Carrasco Moñino
 * 
 */
public class GettingStarted {

  // A panel where the thumbnails of uploaded images will be shown
  private FlowPanel panelImages = new FlowPanel();
  
  I18nConstants c = GWT.create(I18nConstants.class);

 


public GettingStarted(VerticalPanel vp) {
    // Attach the image viewer to the document
	  
	  vp.add(panelImages);
    
    // Create a new multiuploader and attach it to the document
    MultiUploader defaultUploader = new MultiUploader(FileInputType.LABEL);
    vp.add(defaultUploader);

    // You could change the internationalization creating your own Constants file
    defaultUploader.setI18Constants(c);
    // Enable/disable the component
    defaultUploader.setEnabled(true);
    
    // Add a finish handler which will load the image once the upload finishes
    defaultUploader.addOnFinishUploadHandler(onFinishUploaderHandler);
  }

  // Load the image in the document and in the case of success attach it to the viewer
  private 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("Client-Side: The server sent information about the uploaded file -> size: " + size + " Bytes, type: " + type);
      }
    }
  };

  // Attach an image to the pictures viewer
  private OnLoadPreloadedImageHandler showImage = new OnLoadPreloadedImageHandler() {
    public void onLoad(PreloadedImage image) {
      image.setWidth("75px");
      panelImages.add(image);
    }
  };


}
