/*
MIT License

Copyright (c) 2017 Hamdi Kavak

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. 
 */

package com.hamdikavak.humanmobility.modeling.spatial;


/**
 * A class to handle distance that requires units.
 * @author Hamdi Kavak
 * @version 0.3.0
 */
public class DistanceWithUnit {
	
	private SpatialDistanceUnit unit;
	private double value;
	
	public SpatialDistanceUnit getUnit() {
		return unit;
	}
	
	public void setUnit(SpatialDistanceUnit unit) {
		this.unit = unit;
	}
	public double getValue() {
		return value;
	}
	public void setValue(double value) {
		this.value = value;
	}
	
	public DistanceWithUnit(){
		
	}
	
	public DistanceWithUnit(double value, SpatialDistanceUnit unit){
		this.value = value;
		this.unit = unit;
	}
	
	/**
	 * Compares this distance with the one given as a parameter.
	 * 
	 * @param distanceToCompare distance object to compare
	 * @return true if this distance is  than the one given as a parameter 
	 */
	public boolean isShortherThan(DistanceWithUnit distanceToCompare){
	
		DistanceWithUnit currentDistance = this.toDifferentUnit(SpatialDistanceUnit.Meter);
		DistanceWithUnit compareDistance = distanceToCompare.toDifferentUnit(SpatialDistanceUnit.Meter);
		
		return currentDistance.getValue() < compareDistance.getValue();
	}
	
	/**
	 * Converts current distance object to a new one based in given unit.
	 * @param unit new unit
	 * @return a new {@code DistanceWithUnit} object in given unit.
	 */
	public DistanceWithUnit toDifferentUnit(SpatialDistanceUnit unit){
		
		double ratio = getUnitConversionRatios( this.getUnit(), unit);
		return new DistanceWithUnit(this.value * ratio, unit);
	}
	
	private static double getUnitConversionRatios(SpatialDistanceUnit fromUnit, SpatialDistanceUnit toUnit) {
		
		if (fromUnit == SpatialDistanceUnit.Meter){
			
			if (toUnit == SpatialDistanceUnit.Meter){
				return 1d;
			}
			else if (toUnit == SpatialDistanceUnit.Foot){
				return 3.28084;
			}
			else if (toUnit == SpatialDistanceUnit.Yard){
				return 1.09361;
			}
			else if (toUnit == SpatialDistanceUnit.Kilometer){
				return 0.001;
			}
			else if (toUnit == SpatialDistanceUnit.Mile){
				return 0.000621371;
			}
			else if (toUnit == SpatialDistanceUnit.NauticalMile){
				return 0.000539957;
			}
		}
		else if (fromUnit == SpatialDistanceUnit.Foot){
			
			if (toUnit == SpatialDistanceUnit.Meter){
				return 0.3048;
			}
			else if (toUnit == SpatialDistanceUnit.Foot){
				return 1d;
			}
			else if (toUnit == SpatialDistanceUnit.Yard){
				return 0.333333;
			}
			else if (toUnit == SpatialDistanceUnit.Kilometer){
				return 0.0003048;
			}
			else if (toUnit == SpatialDistanceUnit.Mile){
				return 0.000189394;
			}
			else if (toUnit == SpatialDistanceUnit.NauticalMile){
				return 0.000164579;
			}
		}
		else if (fromUnit == SpatialDistanceUnit.Yard){
			
			if (toUnit == SpatialDistanceUnit.Meter){
				return 0.9144;
			}
			else if (toUnit == SpatialDistanceUnit.Foot){
				return 3d;
			}
			else if (toUnit == SpatialDistanceUnit.Yard){
				return 1d;
			}
			else if (toUnit == SpatialDistanceUnit.Kilometer){
				return 0.0009144;
			}
			else if (toUnit == SpatialDistanceUnit.Mile){
				return 0.000568182;
			}
			else if (toUnit == SpatialDistanceUnit.NauticalMile){
				return 0.000493737;
			}
		}
		else if (fromUnit == SpatialDistanceUnit.Kilometer){
			
			if (toUnit == SpatialDistanceUnit.Meter){
				return 1000d;
			}
			else if (toUnit == SpatialDistanceUnit.Foot){
				return 3280.84;
			}
			else if (toUnit == SpatialDistanceUnit.Yard){
				return 1093.61;
			}
			else if (toUnit == SpatialDistanceUnit.Kilometer){
				return 1d;
			}
			else if (toUnit == SpatialDistanceUnit.Mile){
				return 0.621371;
			}
			else if (toUnit == SpatialDistanceUnit.NauticalMile){
				return 0.539957;
			}
		}
		else if (fromUnit == SpatialDistanceUnit.Mile){
			
			if (toUnit == SpatialDistanceUnit.Meter){
				return 1609.34;
			}
			else if (toUnit == SpatialDistanceUnit.Foot){
				return 5280;
			}
			else if (toUnit == SpatialDistanceUnit.Yard){
				return 1760;
			}
			else if (toUnit == SpatialDistanceUnit.Kilometer){
				return 1.60934;
			}
			else if (toUnit == SpatialDistanceUnit.Mile){
				return 1d;
			}
			else if (toUnit == SpatialDistanceUnit.NauticalMile){
				return 0.868976;
			}
		}
		else if (fromUnit == SpatialDistanceUnit.NauticalMile){
			
			if (toUnit == SpatialDistanceUnit.Meter){
				return 1852d;
			}
			else if (toUnit == SpatialDistanceUnit.Foot){
				return 6076.12;
			}
			else if (toUnit == SpatialDistanceUnit.Yard){
				return 2025.37;
			}
			else if (toUnit == SpatialDistanceUnit.Kilometer){
				return 1.852;
			}
			else if (toUnit == SpatialDistanceUnit.Mile){
				return 1.15078;
			}
			else if (toUnit == SpatialDistanceUnit.NauticalMile){
				return 1d;
			}
		}
		
		return 0d;
	}
}
