# coding: utf-8
import numpy as np
from numpy.testing import assert_almost_equal

from astropy import units as u

from poliastro.twobody import angles


def test_true_to_eccentric():
    # Data from NASA-TR-R-158
    data = [
        # ecc,E (deg), nu(deg)
        (0.0, 0.0, 0.0),
        (0.05, 10.52321, 11.05994),
        (0.10, 54.67466, 59.49810),
        (0.35, 142.27123, 153.32411),
        (0.61, 161.87359, 171.02189)
    ]
    for row in data:
        ecc, expected_E, nu = row
        ecc = ecc * u.one
        expected_E = expected_E * u.deg
        nu = nu * u.deg

        E = angles.nu_to_E(nu, ecc)

        assert_almost_equal(E.to(u.rad).value, expected_E.to(u.rad).value,
                            decimal=4)


def test_mean_to_true():
    # Data from Schlesinger & Udick, 1912
    data = [
        # ecc, M (deg), nu (deg)
        (0.0, 0.0, 0.0),
        (0.05, 10.0, 11.06),
        (0.06, 30.0, 33.67),
        (0.04, 120.0, 123.87),
        (0.14, 65.0, 80.50),
        (0.19, 21.0, 30.94),
        (0.35, 65.0, 105.71),
        (0.48, 180.0, 180.0),
        (0.75, 125.0, 167.57)
    ]
    for row in data:
        ecc, M, expected_nu = row
        ecc = ecc * u.one
        M = M * u.deg
        expected_nu = expected_nu * u.deg

        nu = angles.M_to_nu(M, ecc)

        assert_almost_equal(nu.to(u.rad).value, expected_nu.to(u.rad).value,
                            decimal=2)


def test_true_to_mean():
    # Data from Schlesinger & Udick, 1912
    data = [
        # ecc, M (deg), nu (deg)
        (0.0, 0.0, 0.0),
        (0.05, 10.0, 11.06),
        (0.06, 30.0, 33.67),
        (0.04, 120.0, 123.87),
        (0.14, 65.0, 80.50),
        (0.19, 21.0, 30.94),
        (0.35, 65.0, 105.71),
        (0.48, 180.0, 180.0),
        (0.75, 125.0, 167.57)
    ]
    for row in data:
        ecc, expected_M, nu = row
        ecc = ecc * u.one
        expected_M = expected_M * u.deg
        nu = nu * u.deg

        M = angles.nu_to_M(nu, ecc)

        assert_almost_equal(M.to(u.rad).value, expected_M.to(u.rad).value,
                            decimal=3)


def test_flight_path_angle():
    # Data from Curtis, example 2.5
    nu = 109.5 * u.deg
    ecc = 0.6 * u.one
    expected_gamma = 35.26 * u.deg
    gamma = angles.fp_angle(np.deg2rad(nu), ecc)
    assert_almost_equal(gamma.to(u.rad).value, expected_gamma.to(u.rad).value,
                        decimal=3)
