package us.nineworlds.xstreamer.property;

import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;

import com.github.xws.Pilot;

import us.nineworlds.xstreamer.eventbus.EventBus;
import us.nineworlds.xstreamer.eventbus.GenerateSquadJobEvent;
import us.nineworlds.xstreamer.model.PilotTreeNode;

public class PilotPropertySource implements IPropertySource {

	private Pilot pilot;
	private EventBus eventBus;
	
	public PilotPropertySource(PilotTreeNode pilot) {
		this.pilot = (Pilot) pilot.getValue();
		eventBus = EventBus.getInstance();
	}

	@Override
	public Object getEditableValue() {
		return this;
	}

	@Override
	public IPropertyDescriptor[] getPropertyDescriptors() {
		return new IPropertyDescriptor[] {
				new TextPropertyDescriptor("skill", "Skill"),
				new TextPropertyDescriptor("id", "Unique ID"),
				new TextPropertyDescriptor("hull", "Hull"),
				new TextPropertyDescriptor("shields", "Shields"),
				new CheckboxPropertyDescriptor("critical", "Critical Damage")
		};
	}

	@Override
	public Object getPropertyValue(Object id) {
		if ("skill".equals(id)) {
			return pilot.getPilotSkill() == null || pilot.getPilotSkill().isEmpty() ? " " : pilot.getPilotSkill();
		}
		
		if ("id".equals(id)) {
			return pilot.getPilotId() == null || pilot.getPilotId().isEmpty() ? " " : pilot.getPilotId();
		}
		
		if ("hull".equals(id)) {
			return pilot.getHull().toString();
		}
		
		if ("shields".equals(id)) {
			return pilot.getShields().toString();
		}
		
		if ("critical".equals(id)) {
			return pilot.hasCriticalDamage();
		}
		
		return null;
	}

	private void postGenerateJobEvent() {
		eventBus.post(new GenerateSquadJobEvent());
	}

	@Override
	public boolean isPropertySet(Object id) {
		return false;
	}

	@Override
	public void resetPropertyValue(Object id) {
		
	}

	@Override
	public void setPropertyValue(Object id, Object value) {
		String newValue = (String) value;
		boolean postJob = false;
		if ("skill".equals(id)) {
			postJob = true;
			pilot.setPilotSkill(newValue);
		}
		
		if ("id".equals(id)) {
			postJob = true;
			pilot.setPilotId(newValue);
			
		}
		
		if ("hull".equals(id)) {
			postJob = true;
			pilot.setHull(Integer.parseInt(newValue));
		}
		
		if ("shields".equals(id)) {
			postJob = true;
			pilot.setShields(Integer.parseInt(newValue));
		}
		
		if ("critical".equals(id) && value instanceof Boolean) {
			pilot.setCriticalDamage(Boolean.valueOf((Boolean) value));
		}
		
		if (postJob) {
			postGenerateJobEvent();
		}
	}

}

@@ -1973,16 +1973,80 @@
 alue) %7B%0A
+%09%09boolean postJob = false;%0A%09%09%0A%09%09if (value instanceof String) %7B%0A%09
 %09%09String
@@ -2078,34 +2078,8 @@
 e;%0A%09
-%09boolean postJob = false;%0A
 %09%09if
@@ -2097,32 +2097,33 @@
 quals(id)) %7B%0A%09%09%09
+%09
 postJob = true;%0A
@@ -2114,32 +2114,33 @@
 postJob = true;%0A
+%09
 %09%09%09pilot.setPilo
@@ -2155,31 +2155,34 @@
 ewValue);%0A%09%09
+%09
 %7D%0A%09%09
+%09
 %0A%09%09
+%09
 if (%22id%22.equ
@@ -2187,32 +2187,33 @@
 quals(id)) %7B%0A%09%09%09
+%09
 postJob = true;%0A
@@ -2204,32 +2204,33 @@
 postJob = true;%0A
+%09
 %09%09%09pilot.setPilo
@@ -2247,24 +2247,28 @@
 ue);%0A%09%09%09
-%0A
+%09%0A%09
 %09%09%7D%0A%09%09
-%0A
+%09%0A%09
 %09%09if (%22h
@@ -2278,32 +2278,33 @@
 %22.equals(id)) %7B%0A
+%09
 %09%09%09postJob = tru
@@ -2301,32 +2301,33 @@
 tJob = true;%0A%09%09%09
+%09
 pilot.setHull(In
@@ -2349,31 +2349,34 @@
 newValue));%0A
+%09
 %09%09%7D%0A%09%09
-%0A
+%09%0A%09
 %09%09if (%22shiel
@@ -2389,24 +2389,25 @@
 uals(id)) %7B%0A
+%09
 %09%09%09postJob =
@@ -2412,24 +2412,25 @@
  = true;%0A%09%09%09
+%09
 pilot.setShi
@@ -2459,31 +2459,44 @@
 newValue));%0A
+%09
 %09%09%7D
+%09%09%09%0A%09%09%7D else %7B
 %0A%09
-%09%0A
 %09%09if (%22criti
@@ -2538,24 +2538,45 @@
  Boolean) %7B%0A
+%09%09%09%09postJob = true;%0A%09
 %09%09%09pilot.set
@@ -2625,16 +2625,24 @@
 alue));%0A
+%09%09%09%7D%09%09%09%0A
 %09%09%7D%0A%09%09%0A%09

Fix issue with Critical Damage toggle.