package com.planetmayo.debrief.satc.model.contributions;

import java.beans.PropertyChangeListener;
import java.util.Date;

import com.planetmayo.debrief.satc.model.states.BaseRange.IncompatibleStateException;
import com.planetmayo.debrief.satc.model.states.ProblemSpace;
import com.planetmayo.debrief.satc.model.states.State;

public class CompositeStraightLegForecastContribution extends
		StraightLegForecastContribution
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * the speed forecast
	 * 
	 */
	private final SpeedForecastContribution _speed = new SpeedForecastContribution();

	/**
	 * the course forecast
	 * 
	 */
	private final CourseForecastContribution _course = new CourseForecastContribution();

	@Override
	public void addPropertyChangeListener(PropertyChangeListener listener)
	{
		super.addPropertyChangeListener(listener);
		_course.addPropertyChangeListener(listener);
		_speed.addPropertyChangeListener(listener);

	}

	@Override
	public void addPropertyChangeListener(String propertyName,
			PropertyChangeListener listener)
	{
		super.addPropertyChangeListener(propertyName, listener);
		_course.addPropertyChangeListener(propertyName, listener);
		_speed.addPropertyChangeListener(propertyName, listener);
	}

	@Override
	public void removePropertyChangeListener(PropertyChangeListener listener)
	{
		super.removePropertyChangeListener(listener);
		_course.removePropertyChangeListener(listener);
		_speed.removePropertyChangeListener(listener);
	}

	@Override
	public void removePropertyChangeListener(String propertyName,
			PropertyChangeListener listener)
	{
		super.removePropertyChangeListener(propertyName, listener);
		_course.removePropertyChangeListener(propertyName, listener);
		_speed.removePropertyChangeListener(propertyName, listener);
	}

	@Override
	public void setFinishDate(final Date newFinishDate)
	{
		// set the parent value
		super.setFinishDate(newFinishDate);

		// now set the child values
		_course.setFinishDate(newFinishDate);
		_speed.setFinishDate(newFinishDate);
	}

	@Override
	public void setStartDate(final Date newStartDate)
	{
		// set the parent value
		super.setStartDate(newStartDate);

		// now set the child values
		_course.setStartDate(newStartDate);
		_speed.setStartDate(newStartDate);
	}

	/**
	 * get the course constraint
	 * 
	 * @return
	 */
	public CourseForecastContribution getCourse()
	{
		return _course;
	}

	/**
	 * get the speed constraint
	 * 
	 * @return
	 */
	public SpeedForecastContribution getSpeed()
	{
		return _speed;
	}

	@Override
	public void actUpon(ProblemSpace space) throws IncompatibleStateException
	{
		// handle the straight leg component first
		super.actUpon(space);

		// now apply the constraints from the speed forecast
		_speed.actUpon(space);

		// and the course forecast
		_course.actUpon(space);
	}

	@Override
	protected double calcError(State thisState)
	{
		// combine the three errors
		return super.calcError(thisState) + _course.calcError(thisState)
				+ _speed.calcError(thisState);
	}

}

@@ -565,17 +565,16 @@
 ibution 
-_
 speed = 
@@ -688,17 +688,16 @@
 ibution 
-_
 course =
@@ -857,33 +857,32 @@
 er(listener);%0A%09%09
-_
 course.addProper
@@ -903,33 +903,32 @@
 er(listener);%0A%09%09
-_
 speed.addPropert
@@ -1126,25 +1126,24 @@
 istener);%0A%09%09
-_
 course.addPr
@@ -1186,25 +1186,24 @@
 istener);%0A%09%09
-_
 speed.addPro
@@ -1382,33 +1382,32 @@
 er(listener);%0A%09%09
-_
 course.removePro
@@ -1431,33 +1431,32 @@
 er(listener);%0A%09%09
-_
 speed.removeProp
@@ -1666,17 +1666,16 @@
 ner);%0A%09%09
-_
 course.r
@@ -1725,25 +1725,24 @@
 istener);%0A%09%09
-_
 speed.remove
@@ -1949,33 +1949,32 @@
  child values%0A%09%09
-_
 course.setFinish
@@ -1988,33 +1988,32 @@
 wFinishDate);%0A%09%09
-_
 speed.setFinishD
@@ -2196,17 +2196,16 @@
 alues%0A%09%09
-_
 course.s
@@ -2233,17 +2233,16 @@
 ate);%0A%09%09
-_
 speed.se
@@ -2387,17 +2387,16 @@
 %09return 
-_
 course;%0A
@@ -2512,17 +2512,16 @@
 %09return 
-_
 speed;%0A%09
@@ -2739,17 +2739,16 @@
 ecast%0A%09%09
-_
 speed.ac
@@ -2793,17 +2793,16 @@
 ecast%0A%09%09
-_
 course.a
@@ -2947,17 +2947,16 @@
 tate) + 
-_
 course.c
@@ -2985,9 +2985,8 @@
 %09%09+ 
-_
 spee
@@ -3013,10 +3013,11 @@
 e);%0A%09%7D%0A%0A
+%0A
 %7D%0A

refactor variable names, to give more legible XML for persisted scenario