#!/usr/bin/python3
from suncalc import get_position, get_times
from datetime import datetime, timedelta, date

debug = False
#debug = True

now = datetime.now()
#now = datetime(2023, 12, 13, 0, 0)
date = now.replace(hour=12, minute=0, second=0, microsecond=0)

lon = 10.71
lat = 60.772
height = 100

sun = get_times(date, lon, lat, height)

if type(sun["nautical_dawn"]) is datetime:
  sunup = sun["nautical_dawn"] + timedelta(hours=1)
else:
  sunup = now
if type(sun["nautical_dusk"]) is datetime:
  sundown = sun["nautical_dusk"] + timedelta(hours=1)
else:
  sundown = now + timedelta(hours=23, minutes=59, seconds=50)

# If sunup is in the future
if now < sunup:
  countdown = (sunup - now).total_seconds()
  countdown_read = sunup - now
  duration = (sundown - sunup).total_seconds() * 1000
  duration_read = sundown - sunup
else:
  countdown = 0
  countdown_read = 0
  duration = (sundown - now).total_seconds() * 1000
  duration_read = sundown - now

if int(duration) < 0:
  print("Sun already set, fool")
  exit(1)

if debug:
  print("      Now is ", now)
  print("     Date is ", date)
  print("   Sun up is ", sunup)
  print(" Sun down is ", sundown)
  print("Countdown is ", countdown_read)
  print(" Duration is ", duration_read)
  print("")

print(int(countdown), int(duration), sep='\t')
