package gov.nasa.arc.mct.scenario.component;

import gov.nasa.arc.mct.components.AbstractComponent;
import gov.nasa.arc.mct.components.JAXBModelStatePersistence;
import gov.nasa.arc.mct.components.ModelStatePersistence;

import java.util.concurrent.atomic.AtomicReference;

public class ScenarioComponent extends CostFunctionComponent implements DurationCapability {
	private final AtomicReference<ActivityModelRole> model = new AtomicReference<ActivityModelRole>(new ActivityModelRole());
	
	public ActivityData getData() {
		return getModel().getData();
	}
	
	public String getDisplay(){
		return this.getDisplayName();
	}
	
	@Override
	protected <T> T handleGetCapability(Class<T> capability) {
		if (capability.isAssignableFrom(getClass())) {
			return capability.cast(this);
		}
		if (capability.isAssignableFrom(ModelStatePersistence.class)) {
		    JAXBModelStatePersistence<ActivityModelRole> persistence = new JAXBModelStatePersistence<ActivityModelRole>() {

				@Override
				protected ActivityModelRole getStateToPersist() {
					return model.get();
				}

				@Override
				protected void setPersistentState(ActivityModelRole modelState) {
					model.set(modelState);
				}

				@Override
				protected Class<ActivityModelRole> getJAXBClass() {
					return ActivityModelRole.class;
				}
		        
			};
			
			return capability.cast(persistence);
		}
		return null;
	}
	
	public ActivityModelRole getModel() {
		return model.get();
	}

	@Override
	public long getStart() {		
		// Time is measured as "milliseconds since start of timeline", so this is always 0
		return 0l;
	}
	@Override
	public long getEnd() {
		// The end of a Timeline is the end of its last activity
		long end = 0;
		for (AbstractComponent child : getComponents()) {
			DurationCapability dc = child.getCapability(DurationCapability.class);
			if (dc != null) {
				long childEnd = dc.getEnd();
				if (childEnd > end) {
					end = childEnd;
				}
			}
		}
		return end;
	}
	@Override
	public void setStart(long start) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void setEnd(long end) {
		// TODO Auto-generated method stub
		
	}


}
@@ -214,16 +214,64 @@
 tence;%0A%0A
+import java.util.HashSet;%0Aimport java.util.Set;%0A
 import j
@@ -1490,16 +1490,535 @@
 et();%0A%09%7D
+%0A%09%0A%09@Override%0A%09public Set%3CAbstractComponent%3E getAllModifiedObjects() %7B%0A%09%09// Note that this is necessary to support Save All%0A%09%09// (%22all modified objects%22 is the All in Save All;%0A%09%09//  typically, this should be all dirty children.)%09%09%0A%09%09%0A%09%09// TODO: What about cycles?%0A%09%09Set%3CAbstractComponent%3E modified = new HashSet%3CAbstractComponent%3E();%0A%09%09for (AbstractComponent child : getComponents()) %7B%0A%09%09%09if (child.isDirty()) %7B%0A%09%09%09%09modified.add(child);%0A%09%09%09%7D%0A%09%09%09modified.addAll(child.getAllModifiedObjects());%0A%09%09%7D%0A%09%09return modified;%0A%09%7D
 %0A%0A%09@Over

Support 'Save All' for Scenario components