# -*- coding: utf-8 -*-
from pytest import raises

from astral import Astral, AstralError, Location
import datetime
import pytz


def datetime_almost_equal(datetime1, datetime2, seconds=60):
    dd = datetime1 - datetime2
    sd = (dd.days * 24 * 60 * 60) + dd.seconds
    return abs(sd) <= seconds


def test_Location_Name():
    c = Location()
    assert c.name == 'Greenwich'
    c.name = 'London'
    assert c.name == 'London'
    c.name = 'Köln'
    assert c.name == 'Köln'


def test_Location_Country():
    c = Location()
    assert c.region == 'England'
    c.region = 'Australia'
    assert c.region == 'Australia'


def test_Location_Elevation():
    dd = Astral()
    c = dd['London']

    assert c.elevation == 24


def test_Location_TimezoneName():
    c = Location()
    assert c.timezone == 'Europe/London'
    c.name = 'Asia/Riyadh'
    assert c.name == 'Asia/Riyadh'


def test_Location_TimezoneNameNoLocation():
    c = Location()
    c._timezone_group = 'Europe'
    c._timezone_location = ''
    assert c.timezone == 'Europe'


def test_Location_TimezoneNameBad():
    c = Location()
    with raises(ValueError):
        c.timezone = 'bad/timezone'


def test_Location_TimezoneLookup():
    c = Location()
    assert c.tz == pytz.timezone('Europe/London')
    c.timezone='Europe/Stockholm'
    assert c.tz == pytz.timezone('Europe/Stockholm')


def test_Location_TimezoneLookupBad():
    c = Location()
    c._timezone_group = 'bad'
    c._timezone_location = 'timezone'
    with raises(AstralError):
        c.tz


def test_Location_Sun():
    c = Location()
    c.sun()


def test_Location_Dawn():
    c = Location()
    c.dawn()


def test_Location_DawnUTC():
    c = Location()
    c.dawn(local=False)


def test_Location_Sunrise():
    c = Location()
    c.sunrise()


def test_Location_SunriseUTC():
    c = Location()
    c.sunrise(local=False)


def test_Location_SolarNoon():
    c = Location()
    c.solar_noon()


def test_Location_SolarNoonUTC():
    c = Location()
    c.solar_noon(local=False)


def test_Location_Dusk():
    c = Location()
    c.dusk()


def test_Location_DuskUTC():
    c = Location()
    c.dusk(local=False)


def test_Location_Sunset():
    c = Location()
    c.sunset()


def test_Location_SunsetUTC():
    c = Location()
    c.sunset(local=False)


def test_Location_SolarElevation():
    dd = Astral()
    location = dd['Riyadh']
    dt = datetime.datetime(2015, 12, 14, 8, 0, 0)
    dt = location.tz.localize(dt)
    elevation = location.solar_elevation(dt)
    assert abs(elevation - 17) < 0.5


def test_Location_SolarAzimuth():
    dd = Astral()
    location = dd['Riyadh']
    dt = datetime.datetime(2015, 12, 14, 8, 0, 0)
    dt = location.tz.localize(dt)
    azimuth = location.solar_azimuth(dt)
    assert abs(azimuth - 126) < 0.5


def test_Location_TimeAtElevation():
    dd = Astral()
    location = dd['New Delhi']

    test_data = {
        datetime.date(2016, 1, 5): datetime.datetime(2016, 1, 5, 10, 0),
    }

    for day, cdt in test_data.items():
        cdt = location.tz.localize(cdt)
        dt = location.time_at_elevation(28, date=day)
        assert datetime_almost_equal(dt, cdt, seconds=600)


def test_Location_SolarDepression():
    c = Location(("Heidelberg", "Germany", 49.412, -8.71, "Europe/Berlin"))
    c.solar_depression = 'nautical'
    assert c.solar_depression == 12

    c.solar_depression = 18
    assert c.solar_depression == 18


def test_Location_Moon():
    d = datetime.date(2017, 12, 1)
    c=Location()
    assert c.moon_phase(date=d) == 11


def test_Location_TzError():
    with raises(AttributeError):
        c = Location()
        c.tz = 1


def test_Location_equality():
    c1 = Location()
    c2 = Location()
    t = (c1, c2)
    assert c1 == c2
    assert len(set(t)) == 1

    c1 = Location(["Oslo", "Norway", 59.9, 10.7, "Europe/Oslo", 0])
    c2 = Location(["Oslo", "Norway", 59.9, 10.7, "Europe/Oslo", 0])
    c3 = Location(["Stockholm", "Sweden", 59.3, 18, "Europe/Stockholm", 0])
    t1 = (c1, c2)
    t2 = (c1, c3)
    assert c1 == c2
    assert len(set(t1)) == 1
    assert c1 != c3
    assert len(set(t2)) == 2

[((335, 345), 'astral.Location', 'Location', ([], {}), '()\n', (343, 345), False, 'from astral import Astral, AstralError, Location\n'), ((518, 528), 'astral.Location', 'Location', ([], {}), '()\n', (526, 528), False, 'from astral import Astral, AstralError, Location\n'), ((666, 674), 'astral.Astral', 'Astral', ([], {}), '()\n', (672, 674), False, 'from astral import Astral, AstralError, Location\n'), ((770, 780), 'astral.Location', 'Location', ([], {}), '()\n', (778, 780), False, 'from astral import Astral, AstralError, Location\n'), ((938, 948), 'astral.Location', 'Location', ([], {}), '()\n', (946, 948), False, 'from astral import Astral, AstralError, Location\n'), ((1093, 1103), 'astral.Location', 'Location', ([], {}), '()\n', (1101, 1103), False, 'from astral import Astral, AstralError, Location\n'), ((1215, 1225), 'astral.Location', 'Location', ([], {}), '()\n', (1223, 1225), False, 'from astral import Astral, AstralError, Location\n'), ((1412, 1422), 'astral.Location', 'Location', ([], {}), '()\n', (1420, 1422), False, 'from astral import Astral, AstralError, Location\n'), ((1569, 1579), 'astral.Location', 'Location', ([], {}), '()\n', (1577, 1579), False, 'from astral import Astral, AstralError, Location\n'), ((1628, 1638), 'astral.Location', 'Location', ([], {}), '()\n', (1636, 1638), False, 'from astral import Astral, AstralError, Location\n'), ((1691, 1701), 'astral.Location', 'Location', ([], {}), '()\n', (1699, 1701), False, 'from astral import Astral, AstralError, Location\n'), ((1765, 1775), 'astral.Location', 'Location', ([], {}), '()\n', (1773, 1775), False, 'from astral import Astral, AstralError, Location\n'), ((1834, 1844), 'astral.Location', 'Location', ([], {}), '()\n', (1842, 1844), False, 'from astral import Astral, AstralError, Location\n'), ((1913, 1923), 'astral.Location', 'Location', ([], {}), '()\n', (1921, 1923), False, 'from astral import Astral, AstralError, Location\n'), ((1987, 1997), 'astral.Location', 'Location', ([], {}), '()\n', (1995, 1997), False, 'from astral import Astral, AstralError, Location\n'), ((2064, 2074), 'astral.Location', 'Location', ([], {}), '()\n', (2072, 2074), False, 'from astral import Astral, AstralError, Location\n'), ((2127, 2137), 'astral.Location', 'Location', ([], {}), '()\n', (2135, 2137), False, 'from astral import Astral, AstralError, Location\n'), ((2200, 2210), 'astral.Location', 'Location', ([], {}), '()\n', (2208, 2210), False, 'from astral import Astral, AstralError, Location\n'), ((2267, 2277), 'astral.Location', 'Location', ([], {}), '()\n', (2275, 2277), False, 'from astral import Astral, AstralError, Location\n'), ((2351, 2359), 'astral.Astral', 'Astral', ([], {}), '()\n', (2357, 2359), False, 'from astral import Astral, AstralError, Location\n'), ((2397, 2437), 'datetime.datetime', 'datetime.datetime', (['(2015)', '(12)', '(14)', '(8)', '(0)', '(0)'], {}), '(2015, 12, 14, 8, 0, 0)\n', (2414, 2437), False, 'import datetime\n'), ((2599, 2607), 'astral.Astral', 'Astral', ([], {}), '()\n', (2605, 2607), False, 'from astral import Astral, AstralError, Location\n'), ((2645, 2685), 'datetime.datetime', 'datetime.datetime', (['(2015)', '(12)', '(14)', '(8)', '(0)', '(0)'], {}), '(2015, 12, 14, 8, 0, 0)\n', (2662, 2685), False, 'import datetime\n'), ((2845, 2853), 'astral.Astral', 'Astral', ([], {}), '()\n', (2851, 2853), False, 'from astral import Astral, AstralError, Location\n'), ((3223, 3290), 'astral.Location', 'Location', (["('Heidelberg', 'Germany', 49.412, -8.71, 'Europe/Berlin')"], {}), "(('Heidelberg', 'Germany', 49.412, -8.71, 'Europe/Berlin'))\n", (3231, 3290), False, 'from astral import Astral, AstralError, Location\n'), ((3464, 3490), 'datetime.date', 'datetime.date', (['(2017)', '(12)', '(1)'], {}), '(2017, 12, 1)\n', (3477, 3490), False, 'import datetime\n'), ((3497, 3507), 'astral.Location', 'Location', ([], {}), '()\n', (3505, 3507), False, 'from astral import Astral, AstralError, Location\n'), ((3691, 3701), 'astral.Location', 'Location', ([], {}), '()\n', (3699, 3701), False, 'from astral import Astral, AstralError, Location\n'), ((3711, 3721), 'astral.Location', 'Location', ([], {}), '()\n', (3719, 3721), False, 'from astral import Astral, AstralError, Location\n'), ((3797, 3855), 'astral.Location', 'Location', (["['Oslo', 'Norway', 59.9, 10.7, 'Europe/Oslo', 0]"], {}), "(['Oslo', 'Norway', 59.9, 10.7, 'Europe/Oslo', 0])\n", (3805, 3855), False, 'from astral import Astral, AstralError, Location\n'), ((3865, 3923), 'astral.Location', 'Location', (["['Oslo', 'Norway', 59.9, 10.7, 'Europe/Oslo', 0]"], {}), "(['Oslo', 'Norway', 59.9, 10.7, 'Europe/Oslo', 0])\n", (3873, 3923), False, 'from astral import Astral, AstralError, Location\n'), ((3933, 3999), 'astral.Location', 'Location', (["['Stockholm', 'Sweden', 59.3, 18, 'Europe/Stockholm', 0]"], {}), "(['Stockholm', 'Sweden', 59.3, 18, 'Europe/Stockholm', 0])\n", (3941, 3999), False, 'from astral import Astral, AstralError, Location\n'), ((1113, 1131), 'pytest.raises', 'raises', (['ValueError'], {}), '(ValueError)\n', (1119, 1131), False, 'from pytest import raises\n'), ((1245, 1275), 'pytz.timezone', 'pytz.timezone', (['"""Europe/London"""'], {}), "('Europe/London')\n", (1258, 1275), False, 'import pytz\n'), ((1329, 1362), 'pytz.timezone', 'pytz.timezone', (['"""Europe/Stockholm"""'], {}), "('Europe/Stockholm')\n", (1342, 1362), False, 'import pytz\n'), ((1500, 1519), 'pytest.raises', 'raises', (['AstralError'], {}), '(AstralError)\n', (1506, 1519), False, 'from pytest import raises\n'), ((2912, 2937), 'datetime.date', 'datetime.date', (['(2016)', '(1)', '(5)'], {}), '(2016, 1, 5)\n', (2925, 2937), False, 'import datetime\n'), ((2939, 2975), 'datetime.datetime', 'datetime.datetime', (['(2016)', '(1)', '(5)', '(10)', '(0)'], {}), '(2016, 1, 5, 10, 0)\n', (2956, 2975), False, 'import datetime\n'), ((3586, 3608), 'pytest.raises', 'raises', (['AttributeError'], {}), '(AttributeError)\n', (3592, 3608), False, 'from pytest import raises\n'), ((3622, 3632), 'astral.Location', 'Location', ([], {}), '()\n', (3630, 3632), False, 'from astral import Astral, AstralError, Location\n')]