package org.mwc.cmap.tote.calculations;

import org.mwc.cmap.core.CorePlugin;
import org.mwc.debrief.core.preferences.PrefsPage;

import Debrief.Tools.Tote.Watchable;
import Debrief.Tools.Tote.Calculations.rangeCalc;
import MWC.GenericData.HiResDate;

public class SlantRangeCalc extends rangeCalc
{

	private static final String SLANT_RANGE_MARKER = " \\";

	/** override our paren't calculate method so we can choose whether to show slant range
	 * 
	 * @param primary
	 * @param secondary
	 * @param thisTime
	 * @return
	 */
	public double calculate(Watchable primary, Watchable secondary, HiResDate thisTime)
	{
		double range = 0.0;
		double theRng = 0.0;
		if ((primary != null) && (secondary != null) && (primary != secondary))
		{
			range = primary.getLocation().rangeFrom(secondary.getLocation());

			// ok, are we after slant range?
			if (doSlant())
			{
				// right, we've got to change the distance calculation to a slant
				// separation

				// what's the depth separation in metres
				double depthSep = Math.abs(primary.getLocation().getDepth()
						- secondary.getLocation().getDepth());

				// convert to degs
				double sep = MWC.Algorithms.Conversions.m2Degs(depthSep);

				// now calc the slant range
				double range2 = Math.sqrt(sep * sep + range * range);

				range = range2;
			}

			// we output the range value according to the currently selected range
			// units
			String theUnits = getUnits();

			theRng = convertRange(range, theUnits);

		}
		return theRng;
	}

	/**
	 * @return
	 */
	private boolean doSlant()
	{
		String calcSlantStr = CorePlugin.getToolParent().getProperty(
				PrefsPage.PreferenceConstants.CALC_SLANT_RANGE);
		boolean slantStr = Boolean.parseBoolean(calcSlantStr);
		return slantStr;
	}

	/** override our parent's update method so we can annotate the range if we're actually calculating the slant range.
	 * 
	 * @param primary
	 * @param secondary
	 * @param time
	 * @return
	 */
	public String update(Watchable primary, Watchable secondary, HiResDate time)
	{
		String res = super.update(primary, secondary, time);

		// are we calculating slant range?
		if (doSlant())
		{
			// do we have anything to show?
			if (!res.equals(rangeCalc.NOT_APPLICABLE))
			{
					// yes, include marker against value
					res = res + SLANT_RANGE_MARKER;
			}
		}

		return res;

	}

}

@@ -2331,10 +2331,138 @@
 s;%0A%0A%09%7D%0A%0A
+%09public String getTitle()%0A%09%7B%0A%09%09String res = super.getTitle();%0A%09%09if(doSlant())%0A%09%09%7B%0A%09%09%09res = %22Slant %22 & res;%0A%09%09%7D%0A%09%09return res;%0A%09%7D%0A
 %7D%0A

Include variant calc name