package con.docFlow.vaadin.fileExplorer;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.vaadin.easyuploads.FileBuffer;
import org.vaadin.easyuploads.FileFactory;
import org.vaadin.easyuploads.MultiFileUpload;
import org.vaadin.easyuploads.UploadField;
import org.vaadin.easyuploads.UploadField.FieldType;
import org.vaadin.easyuploads.UploadField.StorageMode;

import com.vaadin.Application;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.terminal.FileResource;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.StreamResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Upload;
import com.vaadin.ui.Upload.FailedListener;
import com.vaadin.ui.Upload.Receiver;
import com.vaadin.ui.Upload.StartedEvent;
import com.vaadin.ui.Upload.StartedListener;
import com.vaadin.ui.Upload.SucceededListener;
import com.vaadin.ui.Window;

@SuppressWarnings("serial")
public class UploadExampleApplication extends Application 
implements Receiver, SucceededListener, FailedListener, StartedListener {

  Window root;         // Root element for contained components.
  Panel imagePanel;   // Panel that contains the uploaded image.
  File  file;         // File to write to.

  /** {@inheritDoc}
   */
  @Override
  public void init() {
    System.out.println("________________ INIT APP");
    
    root = new Window("My Upload Component");
    setMainWindow(root);

    
    
    // Create the Upload component.
    final Upload upload = new Upload("Upload the file here", this);

    // Use a custom button caption instead of plain "Upload".
    upload.setButtonCaption("Upload Now");

    // Listen for events regarding the success of upload.
    upload.addListener((Upload.SucceededListener) this);
    upload.addListener((Upload.FailedListener) this);
    upload.addListener((Upload.StartedListener) this);

    root.addComponent(upload);
    root.addComponent(new Label("Click 'Browse' to "+
            "select a file and then click 'Upload'."));

    // Create a panel for displaying the uploaded image.
    imagePanel = new Panel("Uploaded image");
    imagePanel.addComponent(
                     new Label("No image uploaded yet"));
    root.addComponent(imagePanel);
  }

  // Callback method to begin receiving the upload.
  public OutputStream receiveUpload(String filename,
                                    String MIMEType) {
    System.out.println("________________ RECEIVED UPLOAD");
      FileOutputStream fos = null; // Output stream to write to
      file = new File("/tmp/uploads/" + filename);
      try {
          // Open the file for writing.
          fos = new FileOutputStream(file);
      } catch (final java.io.FileNotFoundException e) {
          // Error while opening the file. Not reported here.
          e.printStackTrace();
          return null;
      }

      return fos; // Return the output stream to write to
  }

  // This is called if the upload is finished.
  public void uploadSucceeded(Upload.SucceededEvent event) {
    System.out.println("________________ UPLOAD SUCCEEDED");
      // Log the upload on screen.
      root.addComponent(new Label("File " + event.getFilename()
              + " of type '" + event.getMIMEType()
              + "' uploaded."));
      
      // Display the uploaded file in the image panel.
      final FileResource imageResource = new FileResource(file, this);
      imagePanel.removeAllComponents();
      imagePanel.addComponent(new Embedded("", imageResource));
  }

  // This is called if the upload fails.
  public void uploadFailed(Upload.FailedEvent event) {
    System.out.println("________________ UPLOAD FAILED");
      // Log the failure on screen.
      root.addComponent(new Label("Uploading "
              + event.getFilename() + " of type '"
              + event.getMIMEType() + "' failed."));
  }

  /** {@inheritDoc}
   */
  @Override
  public void uploadStarted(StartedEvent event) {
    System.out.println("________________ UPLOAD STARTET");
  }

  
}
