# -*- 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

[((16, 8, 16, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((25, 8, 25, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((32, 9, 32, 17), 'astral.Astral', 'Astral', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((39, 8, 39, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((46, 8, 46, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((53, 8, 53, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((59, 8, 59, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((66, 8, 66, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((74, 8, 74, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((79, 8, 79, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((84, 8, 84, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((89, 8, 89, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((94, 8, 94, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((99, 8, 99, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((104, 8, 104, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((109, 8, 109, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((114, 8, 114, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((119, 8, 119, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((124, 8, 124, 18), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((129, 9, 129, 17), 'astral.Astral', 'Astral', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((131, 9, 131, 49), 'datetime.datetime', 'datetime.datetime', ({(131, 27, 131, 31): '2015', (131, 33, 131, 35): '12', (131, 37, 131, 39): '14', (131, 41, 131, 42): '8', (131, 44, 131, 45): '0', (131, 47, 131, 48): '0'}, {}), '(2015, 12, 14, 8, 0, 0)', False, 'import datetime\n'), ((138, 9, 138, 17), 'astral.Astral', 'Astral', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((140, 9, 140, 49), 'datetime.datetime', 'datetime.datetime', ({(140, 27, 140, 31): '2015', (140, 33, 140, 35): '12', (140, 37, 140, 39): '14', (140, 41, 140, 42): '8', (140, 44, 140, 45): '0', (140, 47, 140, 48): '0'}, {}), '(2015, 12, 14, 8, 0, 0)', False, 'import datetime\n'), ((147, 9, 147, 17), 'astral.Astral', 'Astral', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((161, 8, 161, 75), 'astral.Location', 'Location', ({(161, 17, 161, 74): "('Heidelberg', 'Germany', 49.412, -8.71, 'Europe/Berlin')"}, {}), "(('Heidelberg', 'Germany', 49.412, -8.71, 'Europe/Berlin'))", False, 'from astral import Astral, AstralError, Location\n'), ((170, 8, 170, 34), 'datetime.date', 'datetime.date', ({(170, 22, 170, 26): '2017', (170, 28, 170, 30): '12', (170, 32, 170, 33): '1'}, {}), '(2017, 12, 1)', False, 'import datetime\n'), ((171, 6, 171, 16), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((182, 9, 182, 19), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((183, 9, 183, 19), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n'), ((188, 9, 188, 67), 'astral.Location', 'Location', ({(188, 18, 188, 66): "['Oslo', 'Norway', 59.9, 10.7, 'Europe/Oslo', 0]"}, {}), "(['Oslo', 'Norway', 59.9, 10.7, 'Europe/Oslo', 0])", False, 'from astral import Astral, AstralError, Location\n'), ((189, 9, 189, 67), 'astral.Location', 'Location', ({(189, 18, 189, 66): "['Oslo', 'Norway', 59.9, 10.7, 'Europe/Oslo', 0]"}, {}), "(['Oslo', 'Norway', 59.9, 10.7, 'Europe/Oslo', 0])", False, 'from astral import Astral, AstralError, Location\n'), ((190, 9, 190, 75), 'astral.Location', 'Location', ({(190, 18, 190, 74): "['Stockholm', 'Sweden', 59.3, 18, 'Europe/Stockholm', 0]"}, {}), "(['Stockholm', 'Sweden', 59.3, 18, 'Europe/Stockholm', 0])", False, 'from astral import Astral, AstralError, Location\n'), ((54, 9, 54, 27), 'pytest.raises', 'raises', ({(54, 16, 54, 26): 'ValueError'}, {}), '(ValueError)', False, 'from pytest import raises\n'), ((60, 19, 60, 49), 'pytz.timezone', 'pytz.timezone', ({(60, 33, 60, 48): '"""Europe/London"""'}, {}), "('Europe/London')", False, 'import pytz\n'), ((62, 19, 62, 52), 'pytz.timezone', 'pytz.timezone', ({(62, 33, 62, 51): '"""Europe/Stockholm"""'}, {}), "('Europe/Stockholm')", False, 'import pytz\n'), ((69, 9, 69, 28), 'pytest.raises', 'raises', ({(69, 16, 69, 27): 'AstralError'}, {}), '(AstralError)', False, 'from pytest import raises\n'), ((151, 8, 151, 33), 'datetime.date', 'datetime.date', ({(151, 22, 151, 26): '(2016)', (151, 28, 151, 29): '(1)', (151, 31, 151, 32): '(5)'}, {}), '(2016, 1, 5)', False, 'import datetime\n'), ((151, 35, 151, 71), 'datetime.datetime', 'datetime.datetime', ({(151, 53, 151, 57): '(2016)', (151, 59, 151, 60): '(1)', (151, 62, 151, 63): '(5)', (151, 65, 151, 67): '(10)', (151, 69, 151, 70): '(0)'}, {}), '(2016, 1, 5, 10, 0)', False, 'import datetime\n'), ((176, 9, 176, 31), 'pytest.raises', 'raises', ({(176, 16, 176, 30): 'AttributeError'}, {}), '(AttributeError)', False, 'from pytest import raises\n'), ((177, 12, 177, 22), 'astral.Location', 'Location', ({}, {}), '()', False, 'from astral import Astral, AstralError, Location\n')]