package com.thingtrack.konekti.view.web.form.field;

import java.io.Serializable;

import org.vaadin.addon.customfield.CustomField;

import com.thingtrack.konekti.view.addon.ui.UploadViewForm;

import com.vaadin.data.Property;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Window.CloseEvent;
import com.vaadin.ui.Window.CloseListener;

@SuppressWarnings("serial")
public class FileField extends CustomField {
	
	private VerticalLayout mainLayout;
	private Button btnAttach;
	
	private byte[] file;
	private String fileName;
	
	// navigator button listeners
	private AttachmentChangeListener listenerChangeAttachment = null;
		
	public FileField() {
		buildMainLayout();
		setCompositionRoot(mainLayout);
		
		// TODO add user code here
		btnAttach.addListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				final UploadViewForm uploadViewForm = new UploadViewForm(file);
				
				//uploadViewForm.setWidth("300px");
				uploadViewForm.setWidth("-1px");
				uploadViewForm.setHeight("-1px");
				uploadViewForm.setClosable(false);
				uploadViewForm.addListener(new CloseListener() {				
					@Override
					public void windowClose(CloseEvent event) {						
						file = uploadViewForm.getFile();
						fileName = uploadViewForm.getFileName();
							
						if (uploadViewForm.getFile() != null) 
							btnAttach.setIcon(new ThemeResource("../konekti/images/icons/servicedesigner-module/tick.png"));						
						else 							
							btnAttach.setIcon(null);																				
						
						if (listenerChangeAttachment != null)
							listenerChangeAttachment.attachmentChange(new AttachmentChangeEvent(event.getComponent(), file , fileName));
						
						//setValue(file);
					}
				});
				
				getApplication().getMainWindow().addWindow(uploadViewForm);
				
			}
		});
	}

	@Override
	public void setCaption(String caption) {
		btnAttach.setCaption(caption);
		
	}
	
	@Override
	public void setPropertyDataSource(Property newDataSource) {
		file = (byte[])newDataSource.getValue();
		
		if (file != null)
			btnAttach.setIcon(new ThemeResource("../konekti/images/icons/servicedesigner-module/tick.png"));
		
		super.setPropertyDataSource(newDataSource);
	}
	
	@Override
	public void setValue(Object file) {		
		if (file != null)
			btnAttach.setIcon(new ThemeResource("../konekti/images/icons/servicedesigner-module/tick.png"));
		
		super.setValue(file);
	}
	
	@Override
	public Class<?> getType() {
		return byte[].class;
	}

	@Override
	public Object getValue() {	
		return file;
		
	}
	
	public String getFileName() {
		return this.fileName;
		
	}		
	
	public void addListener(AttachmentChangeListener listener) {
		this.listenerChangeAttachment = listener;
		
	}
	
	public interface AttachmentChangeListener extends Serializable {
        public void attachmentChange(AttachmentChangeEvent event);

    }
	
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("-1px");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("-1px");
		
		// btnAttach
		btnAttach = new Button();
		btnAttach.setCaption("Adjuntar Fichero");
		btnAttach.setImmediate(false);
		btnAttach.setWidth("100.0%");
		btnAttach.setHeight("-1px");
		mainLayout.addComponent(btnAttach);
		
		return mainLayout;
	}
	
	public class AttachmentChangeEvent extends Event {
		private byte[] attachment;
		private String attachmentName;

		 public AttachmentChangeEvent(Component source) {
	            super(source);
	            
	            this.attachment = null;
	            this.attachmentName = null;
	     }
		 
		public AttachmentChangeEvent(Component source, byte[] attachment, String attachmentName) {
			super(source);
			
			this.attachmentName = attachmentName;
			this.attachment = attachment;
		}

		public String getAttachmentName() {
			return this.attachmentName;
			
		}
		
		public byte[] getAttachment() {
			return this.attachment;
			
		}
		
	  }
}

@@ -840,16 +840,71 @@
 eld() %7B%0A
+%09%09this(null);%0A%09%09%0A%09%7D%0A%09%0A%09public FileField(byte%5B%5D file) %7B%0A
 %09%09buildM
@@ -916,16 +916,16 @@
 yout();%0A
-
 %09%09setCom
@@ -982,16 +982,39 @@
 de here%0A
+%09%09this.file = file;%0A%09%09%0A
 %09%09btnAtt
@@ -1176,16 +1176,31 @@
 iewForm(
+FileField.this.
 file);%0A%09
@@ -1486,24 +1486,39 @@
 %09%09%09%09%09%0A%09%09%09%09%09%09
+FileField.this.
 file = uploa
@@ -1821,25 +1821,31 @@
 %09%09%09%09%09%0A%09%09%09%09%09%09
+%09%09%09%09%09%09
 %0A
-
 %09%09%09%09%09%09if (li
@@ -1973,16 +1973,31 @@
 nent(), 
+FileField.this.
 file , f
@@ -2531,32 +2531,32 @@
 %09%7D%0A%09%0A%09@Override%0A
-
 %09public void set
@@ -2575,16 +2575,41 @@
  file) %7B
+%09%0A%09%09file = (byte%5B%5D)file;%0A
 %09%09%0A%09%09if 

update bugfixes