package ee.ut.cs.rum.workspaces.internal.ui.task.details;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import com.google.gson.Gson;
import ee.ut.cs.rum.database.domain.Plugin;
import ee.ut.cs.rum.database.domain.Task;
import ee.ut.cs.rum.database.util.PluginAccess;
import ee.ut.cs.rum.database.util.TaskAccess;
import ee.ut.cs.rum.plugins.development.description.PluginInfo;
import ee.ut.cs.rum.plugins.development.ui.PluginConfigurationUi;
import ee.ut.cs.rum.workspaces.internal.ui.task.newtask.SelectedPluginInfo;
import ee.ut.cs.rum.workspaces.internal.ui.workspace.WorkspaceTabFolder;
import ee.ut.cs.rum.workspaces.internal.util.PluginUtils;

public class TaskDetails extends Composite {
	private static final long serialVersionUID = 5855252537558430818L;
	
	private Long taskId;
	private SelectedPluginInfo selectedPluginInfo;
	PluginConfigurationUi pluginConfigurationUi;
	
	public TaskDetails(WorkspaceTabFolder workspaceTabFolder, Long taskId) {
		super(workspaceTabFolder, SWT.CLOSE);
		
		this.taskId=taskId;
		this.setLayout(new GridLayout(2, false));
		
		Task task = TaskAccess.getTaskDataFromDb(taskId);
		
		createContents(task);
		
		//TODO: Should look into a better way of doing this (maybe a feature request for Eclipse RAP)
		this.addListener(SWT.Resize, new Listener() {
			private static final long serialVersionUID = -8815015925218184274L;

			public void handleEvent(Event e) {
				int selectedPluginInfoSizeX = selectedPluginInfo.getContent().getSize().x;
				int pluginConfigurationUiSizeX = pluginConfigurationUi.getContent().getSize().x;
				
				if (TaskDetails.this.getSize().x > selectedPluginInfoSizeX+pluginConfigurationUiSizeX) {
					if (((GridData)selectedPluginInfo.getLayoutData()).grabExcessHorizontalSpace) {
						selectedPluginInfo.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, true));						
					}
				} else {
					if (!((GridData)selectedPluginInfo.getLayoutData()).grabExcessHorizontalSpace) {
						selectedPluginInfo.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true));						
					}
				}
				TaskDetails.this.layout();
			}
		});
		
	}

	@SuppressWarnings("unchecked")
	private void createContents(Task task) {
		selectedPluginInfo = new SelectedPluginInfo(this);
		Plugin plugin = PluginAccess.getPluginDataFromDb(task.getPluginId());
		selectedPluginInfo.updateSelectedPluginInfo(plugin);
		
		PluginInfo pluginInfo = PluginUtils.deserializePluginInfo(plugin);
		
		pluginConfigurationUi = new PluginConfigurationUi(this, pluginInfo);
		pluginConfigurationUi.setEnabled(false);
		
		Gson gson = new Gson();
		Map<String,String> configurationValues = new HashMap<String,String>();
		configurationValues = gson.fromJson(task.getConfigurationValues(), configurationValues.getClass());
		pluginConfigurationUi.setConfigurationValues(configurationValues);
	}
	
	public Long getTaskId() {
		return taskId;
	}
}

@@ -1018,19 +1018,18 @@
 0818L;%0D%0A
-%09
 %0D%0A
+
 %09private
@@ -1139,19 +1139,18 @@
 ionUi;%0D%0A
-%09
 %0D%0A
+
 %09public 
@@ -1257,20 +1257,18 @@
 LOSE);%0D%0A
-%09%09
 %0D%0A
+
 %09%09this.t
@@ -1327,20 +1327,18 @@
 lse));%0D%0A
-%09%09
 %0D%0A
+
 %09%09Task t
@@ -1382,20 +1382,18 @@
 skId);%0D%0A
-%09%09
 %0D%0A
+
 %09%09create
@@ -1409,20 +1409,18 @@
 task);%0D%0A
-%09%09
 %0D%0A
+
 %09%09//TODO
@@ -1836,22 +1836,18 @@
 e().x;%0D%0A
-%09%09%09%09
 %0D%0A
+
 %09%09%09%09if (
@@ -2016,32 +2016,43 @@
 Space) %7B%0D%0A%09%09%09%09%09%09
+((GridData)
 selectedPluginIn
@@ -2046,33 +2046,33 @@
 ectedPluginInfo.
-s
+g
 etLayoutData(new
@@ -2072,60 +2072,43 @@
 ata(
-new GridData(SWT.LEFT, SWT.TOP, false, true));%09%09%09%09%09%09
+)).grabExcessHorizontalSpace=false;
 %0D%0A%09%09
@@ -2220,16 +2220,27 @@
 %0D%0A%09%09%09%09%09%09
+((GridData)
 selected
@@ -2250,17 +2250,17 @@
 ginInfo.
-s
+g
 etLayout
@@ -2268,59 +2268,42 @@
 ata(
-new GridData(SWT.LEFT, SWT.TOP, true, true));%09%09%09%09%09%09
+)).grabExcessHorizontalSpace=true;
 %0D%0A%09%09
@@ -2360,20 +2360,18 @@
 %0A%09%09%7D);%0D%0A
-%09%09
 %0D%0A
+
 %09%7D%0D%0A%0D%0A%09@
@@ -2627,20 +2627,18 @@
 ugin);%0D%0A
-%09%09
 %0D%0A
+
 %09%09Plugin
@@ -2699,20 +2699,18 @@
 ugin);%0D%0A
-%09%09
 %0D%0A
+
 %09%09plugin
@@ -2821,12 +2821,10 @@
 );%0D%0A
-%09%09
 %0D%0A
+
 %09%09Gs
@@ -3097,17 +3097,16 @@
 );%0D%0A%09%7D%0D%0A
-%09
 %0D%0A%09publi

Modify existing GridData instance instead of creating a new one