import CoreLocation
@testable import Daylight
import XCTest

class SunCalcTests: XCTestCase {

    /*
     sunCalc.nadir         2013-03-04 22:10:57 +0000
     sunCalc.nightEnd      2013-03-05 02:46:17 +0000
     sunCalc.nauticalDawn  2013-03-05 03:24:31 +0000
     sunCalc.dawn          2013-03-05 04:02:17 +0000
     sunCalc.sunrise       2013-03-05 04:34:56 +0000
     sunCalc.sunriseEnd    2013-03-05 04:38:19 +0000
     sunCalc.goldenHourEnd 2013-03-05 05:19:01 +0000
     sunCalc.solarNoon     2013-03-05 10:10:57 +0000
     sunCalc.goldenHour    2013-03-05 15:02:52 +0000
     sunCalc.sunsetStart   2013-03-05 15:43:34 +0000
     sunCalc.sunset        2013-03-05 15:46:57 +0000
     sunCalc.dusk          2013-03-05 16:19:36 +0000
     sunCalc.nauticalDusk  2013-03-05 16:57:22 +0000
     sunCalc.night         2013-03-05 17:35:36 +0000
     */
    func testAPI() {
        let coordinate = CLLocationCoordinate2D(latitude: 50.5, longitude: 30.5)

        let timeZone = TimeZone(secondsFromGMT: 0)!
        var calendar = Calendar.autoupdatingCurrent
        calendar.timeZone = timeZone

        var dateComponents = DateComponents()
        dateComponents.timeZone = timeZone
        dateComponents.day = 5
        dateComponents.month = 3
        dateComponents.year = 2013

        dateComponents.hour = 0
        dateComponents.minute = 0
        let date = calendar.date(from: dateComponents)!

        let sunPhaseTimes = SunTime(date: date, coordinate: coordinate).sunTimes

        // DAY 5

        dateComponents.hour = 4
        dateComponents.minute = 2
        dateComponents.second = 17
        let expectedDawn = calendar.date(from: dateComponents)!
        XCTAssertEqual(sunPhaseTimes.dawnTime.timeIntervalSince1970, expectedDawn.timeIntervalSince1970, accuracy: 0.99)

        dateComponents.hour = 4
        dateComponents.minute = 34
        dateComponents.second = 56
        let expectedSunrise = calendar.date(from: dateComponents)!
        XCTAssertEqual(sunPhaseTimes.sunriseTimeStart.timeIntervalSince1970, expectedSunrise.timeIntervalSince1970, accuracy: 0.99)

        dateComponents.hour = 10
        dateComponents.minute = 10
        dateComponents.second = 57
        let expectedSolarNoon = calendar.date(from: dateComponents)!
        XCTAssertEqual(sunPhaseTimes.solarNoonTime.timeIntervalSince1970, expectedSolarNoon.timeIntervalSince1970, accuracy: 0.99)

        dateComponents.hour = 17
        dateComponents.minute = 35
        dateComponents.second = 36
        let expectedNight = calendar.date(from: dateComponents)!
        XCTAssertEqual(sunPhaseTimes.nightTimeStart.timeIntervalSince1970, expectedNight.timeIntervalSince1970, accuracy: 0.99)

        dateComponents.hour = 15
        dateComponents.minute = 46
        dateComponents.second = 57
        let expectedSunset = calendar.date(from: dateComponents)!
        XCTAssertEqual(sunPhaseTimes.sunsetTimeEnd.timeIntervalSince1970, expectedSunset.timeIntervalSince1970, accuracy: 0.99)

        dateComponents.hour = 16
        dateComponents.minute = 19
        dateComponents.second = 36
        let expectedDusk = calendar.date(from: dateComponents)!
        XCTAssertEqual(sunPhaseTimes.duskTime.timeIntervalSince1970, expectedDusk.timeIntervalSince1970, accuracy: 0.99)
    }
}
