package main

import (
	"log"
	"math"
	"time"
)

func nextSunriseSunsetTime(t time.Time) (sunrise, sunset time.Time, err error) {
	var (
		nextSR time.Time
		nextSS time.Time
	)

	s := config.SunRiseSet

	s.Date = t
	currentSR, currentSS, err := s.GetSunriseSunset()
	if err != nil {
		return time.Time{}, time.Time{}, err
	}

	s.Date = t.Add(24 * time.Hour)
	nextSR, nextSS, err = s.GetSunriseSunset()
	if err != nil {
		return time.Time{}, time.Time{}, err
	}

	nSR := currentSR
	nSS := currentSS

	if t.After(currentSR) {
		nSR = nextSR
	}

	if t.After(currentSS) {
		nSS = nextSS
	}

	log.Printf("[TRACE] Calculated Time: %s\n", t.Format(config.TimeFormat))
	log.Printf("[TRACE] Next calculated sunrise: %s (%v)\n", nSR.Format(config.TimeFormat), math.Round(nSR.Sub(time.Now()).Seconds()))
	log.Printf("[TRACE] Next calculated sunset : %s (%v)\n", nSS.Format(config.TimeFormat), math.Round(nSS.Sub(time.Now()).Seconds()))

	return nSR, nSS, nil
}