using System;
using System.Runtime.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;

namespace Raytheon.Units
{
    /// <summary>
    /// This class represents a velocity.  
    /// 
    /// It has no constructors.  Instead, it provides multiple methods for creating objects of this 
    /// type.  Those methods are of the form FromXXX, where XXX is a unit (e.g. FromRadiansPerSecSqrd).
    /// 
    /// Properties (e.g. RadiansPerSecSqrd) are provided to convert the value to other units and
    /// return as type double.
    /// 
    /// Methods (operator overloads) are provided to perform calculations with related types (e.g. 
    /// Angle, AngularVelocity).
    /// </summary>
    [Serializable]
    [DataContract]
    public class AngularAcceleration
    {
        public enum Unit
        {
            DegreesPerSecSqrd,
            RadiansPerSecSqrd,
            MilliradiansPerSecSqrd
        }

        public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
        {
            {Unit.DegreesPerSecSqrd, new List<string>(){ "deg/sec^2" } },
            {Unit.MilliradiansPerSecSqrd, new List<string>(){ "mrad/sec^2" } },
            {Unit.RadiansPerSecSqrd, new List<string>(){ "rad/sec^2" } }
        };

        private const Unit DefaultUnits = Unit.RadiansPerSecSqrd;
        private const double DegreesPerRadian = 180 / Math.PI;

        [DataMember(Name = "AngularAcceleration", IsRequired = true)]
        private double _acceleration; // radians/seconds^2

        // map: unit => abbreviation, conversion method and property
        private static IDictionary<Unit, UnitInfo<AngularAcceleration>> _unitInfo = new Dictionary<Unit, UnitInfo<AngularAcceleration>>()
        {
            { Unit.DegreesPerSecSqrd, new UnitInfo<AngularAcceleration>(UnitAbbreviations[Unit.DegreesPerSecSqrd], FromDegreesPerSecSqrd, typeof(AngularAcceleration).GetProperty("DegreesPerSecSqrd").GetGetMethod()) },
            { Unit.MilliradiansPerSecSqrd, new UnitInfo<AngularAcceleration>(UnitAbbreviations[Unit.MilliradiansPerSecSqrd], FromMilliradiansPerSecSqrd, typeof(AngularAcceleration).GetProperty("MilliradiansPerSecSqrd").GetGetMethod()) },
            { Unit.RadiansPerSecSqrd, new UnitInfo<AngularAcceleration>(UnitAbbreviations[Unit.RadiansPerSecSqrd], FromRadiansPerSecSqrd, typeof(AngularAcceleration).GetProperty("RadiansPerSecSqrd").GetGetMethod()) }
        };

        /// <summary>
        /// Prevents a default instance of the <see cref="AngularAcceleration"/> class from being created.  
        /// Use FromXXX methods to create objects of this type.
        /// </summary>
        /// <exception cref="System.Exception">No one should be calling this, it is only here to prevent users from creating default object</exception>
        private AngularAcceleration()
        {
            throw new InvalidOperationException("No one should be calling this, it is only here to prevent users from creating default object");
        }

        /// <summary>
        /// ctor used by this class to create objects in FromXXX methods 
        /// </summary>
        /// <param name="acceleration">The acceleration - radians/sec^2.</param>
        private AngularAcceleration(double acceleration)
        {
            _acceleration = acceleration;
        }

        #region conversion properties

        /// <summary>
        /// Returns degrees/sec^2 as type double
        /// </summary>
        /// <value>
        /// degrees/sec^2
        /// </value>
        public double DegreesPerSecSqrd
        {
            get { return _acceleration * DegreesPerRadian; }
        }

        /// <summary>
        /// Returns milliradians/sec^2 as type double
        /// </summary>
        /// <value>
        /// milliradians/sec^2
        /// </value>
        public double MilliradiansPerSecSqrd
        {
            get { return _acceleration * 1000; }
        }

        /// <summary>
        /// Returns radians/sec^2 as type double
        /// </summary>
        /// <value>
        /// radians/sec^2
        /// </value>
        public double RadiansPerSecSqrd
        {
            get { return _acceleration; }
        }

        #endregion

        #region creation methods

        /// <summary>
        /// Returns AngularAcceleration object interpreting passed value as degrees/sec
        /// </summary>
        /// <param name="degreesPerSecSqrd">degrees/sec</param>
        /// <returns></returns>
        public static AngularAcceleration FromDegreesPerSecSqrd(double degreesPerSecSqrd)
        {
            return new AngularAcceleration(degreesPerSecSqrd / DegreesPerRadian);
        }

        /// <summary>
        /// Returns AngularAcceleration object interpreting passed value as radians/sec^2
        /// </summary>
        /// <param name="milliradiansPerSecSqrd">radians/sec^2</param>
        /// <returns></returns>
        public static AngularAcceleration FromMilliradiansPerSecSqrd(double milliradiansPerSecSqrd)
        {
            return new AngularAcceleration(milliradiansPerSecSqrd / 1000);
        }

        /// <summary>
        /// Returns AngularAcceleration object interpreting passed value as radians/sec^2
        /// </summary>
        /// <param name="radiansPerSecSqrd">radians/sec^2</param>
        /// <returns></returns>
        public static AngularAcceleration FromRadiansPerSecSqrd(double radiansPerSecSqrd)
        {
            return new AngularAcceleration(radiansPerSecSqrd);
        }

        #endregion

        public override bool Equals(object obj)
        {
            AngularAcceleration a = obj as AngularAcceleration;

            return (a == null) ? false : (this._acceleration == a._acceleration);
        }

        public override int GetHashCode()
        {
            return _acceleration.GetHashCode();
        }

        #region binary operators

        // not implementing %, &, |, ^, <<, >>

        
        public static AngularAcceleration operator +(AngularAcceleration left, AngularAcceleration right)
        {
            if (null == left)
                throw new ArgumentNullException("left", "Cannot add a null AngularAcceleration");

            if (null == right)
                throw new ArgumentNullException("right", "Cannot add null AngularAcceleration");

            return new AngularAcceleration(left._acceleration + right._acceleration);
        }

        
        public static AngularAcceleration operator -(AngularAcceleration left, AngularAcceleration right)
        {
            if (null == left)
                throw new ArgumentNullException("left", "Cannot subtract a null AngularAcceleration");

            if (null == right)
                throw new ArgumentNullException("right", "Cannot subtract null AngularAcceleration");

            return new AngularAcceleration(left._acceleration - right._acceleration);
        }

        
        public static AngularAcceleration operator *(AngularAcceleration acceleration, double scalar)
        {
            if (null == acceleration)
                throw new ArgumentNullException("acceleration", "Cannot multiply a null AngularAcceleration");

            return new AngularAcceleration(acceleration._acceleration * scalar);
        }

        
        public static AngularAcceleration operator *(double scalar, AngularAcceleration acceleration)
        {
            if (null == acceleration)
                throw new ArgumentNullException("acceleration", "Cannot multiply a null AngularAcceleration");

            return new AngularAcceleration(scalar * acceleration._acceleration);
        }

        
        public static AngularAcceleration operator /(AngularAcceleration acceleration, double scalar)
        {
            if (null == acceleration)
                throw new ArgumentNullException("acceleration", "Cannot divide a null AngularAcceleration");

            if (0.0 == scalar)
                throw new DivideByZeroException("Cannot divide Angular Acceleration by 0");

            return new AngularAcceleration(acceleration._acceleration / scalar);
        }

        #endregion

        #region comparison operators

        public static bool operator ==(AngularAcceleration left, AngularAcceleration right)
        {
            bool bReturn = false;
            if (object.ReferenceEquals(left, null))
            {
                if (object.ReferenceEquals(right, null))
                {
                    //both are null, so we are ==
                    bReturn = true;
                }
            }
            else
            {
                if (!object.ReferenceEquals(left, null) &&
                    !object.ReferenceEquals(right, null))
                {
                    bReturn = left._acceleration == right._acceleration;
                }
            }
            return bReturn;
        }

        public static bool operator !=(AngularAcceleration left, AngularAcceleration right)
        {
            return !(left == right);
        }

        
        public static bool operator <(AngularAcceleration left, AngularAcceleration right)
        {
            if (null == left)
                throw new ArgumentNullException("left", "Cannot compare null AngularAcceleration");

            if (null == right)
                throw new ArgumentNullException("right", "Cannot compare null AngularAcceleration");

            return left._acceleration < right._acceleration;
        }

        
        public static bool operator >(AngularAcceleration left, AngularAcceleration right)
        {
            if (null == left)
                throw new ArgumentNullException("left", "Cannot compare null AngularAcceleration");

            if (null == right)
                throw new ArgumentNullException("right", "Cannot compare null AngularAcceleration");

            return left._acceleration > right._acceleration;
        }

        
        public static bool operator <=(AngularAcceleration left, AngularAcceleration right)
        {
            if (null == left)
                throw new ArgumentNullException("left", "Cannot compare null AngularAcceleration");

            if (null == right)
                throw new ArgumentNullException("right", "Cannot compare null AngularAcceleration");

            return left._acceleration <= right._acceleration;
        }

        
        public static bool operator >=(AngularAcceleration left, AngularAcceleration right)
        {
            if (null == left)
                throw new ArgumentNullException("left", "Cannot compare null AngularAcceleration");

            if (null == right)
                throw new ArgumentNullException("right", "Cannot compare null AngularAcceleration");

            return left._acceleration >= right._acceleration;
        }

        #endregion

        #region operators with other classes

        /// <summary>
        /// Implements the operator * for AngularVelocity = AngularAcceleration * PrecisionTimeSpan
        /// </summary>
        /// <param name="acceleration">The velocity.</param>
        /// <param name="time">The time.</param>
        /// <returns>
        /// Angle
        /// </returns>
        
        public static AngularVelocity operator *(AngularAcceleration acceleration, PrecisionTimeSpan time)
        {
            if (null == acceleration)
                throw new ArgumentNullException("acceleration", "Cannot multiply with null AngularAcceleration");

            if (null == time)
                throw new ArgumentNullException("time", "Cannot multiply with null PrecisionTimeSpan");

            return AngularVelocity.FromRadiansPerSec(acceleration.RadiansPerSecSqrd * time.Seconds);
        }

        /// <summary>
        /// Implements the operator * for AngularVelocity = PrecisionTimeSpan * AngularAcceleration
        /// </summary>
        /// <param name="acceleration">The acceleration.</param>
        /// <param name="time">The time.</param>
        /// <returns>
        /// AngularVelocity 
        /// </returns>
        
        public static AngularVelocity operator *(PrecisionTimeSpan time, AngularAcceleration acceleration)
        {
            if (null == acceleration)
                throw new ArgumentNullException("acceleration", "Cannot multiply with null AngularAcceleration");

            if (null == time)
                throw new ArgumentNullException("time", "Cannot multiply with null PrecisionTimeSpan");

            return AngularVelocity.FromRadiansPerSec(time.Seconds * acceleration.RadiansPerSecSqrd);
        }

        #endregion

        #region parsing

        public static AngularAcceleration Parse(string s)
        {
            return Common.Parse(s, _unitInfo);
        }

        public static bool TryParse(string s, out AngularAcceleration value)
        {
            try
            {
                value = AngularAcceleration.Parse(s);
                return true;
            }
            catch (Exception)
            {
                value = null;
                return false;
            }
        }

        #endregion

        /// <summary>
        /// Returns a string appended with default units (e.g. "1.23 deg/sec^2").
        /// </summary>
        /// <returns>
        /// A string that represents this instance
        /// </returns>
        public override string ToString()
        {
            return ToString(DefaultUnits);
        }

        /// <summary>
        /// overload that returns a string appended with specified units (e.g. "1.23 deg/sec^2").
        /// </summary>
        /// <param name="units"></param>
        /// <returns></returns>
        public string ToString(Unit units)
        {
            double value = (double)_unitInfo[units].PropertyGetter.Invoke(this, null);

            return $"{value} {_unitInfo[units].Abbreviation[0]}";
        }
    }
}
