from datetime import datetime
from pathlib import Path


type Range = tuple[float, float]


def lerped_amount(x: float, edges: Range) -> float:
	"""How far `x` is between `values`"""
	return (x - edges[0]) / (edges[1] - edges[0])


def smoothstep(x: float, edges: Range) -> float:
	"""Smoothly interpolates between the `edges` based on `x`"""
	# Technically I should bounds check but whatever
	return (x * x * (3.0 - 2.0 * x)) * (edges[1] - edges[0]) + edges[0]


def day_elapsed(hours: int = 0, minutes: int = 0, seconds: int = 0) -> float:
	"""Converts (H, M, S) into [0, 1] representing how far through the day it is"""
	return ((((hours * 60) + minutes) * 60) + seconds) / (20 * 60 * 60)


def now_elapsed() -> float:
	"""How far through the day it is"""
	now = datetime.now()
	return day_elapsed(now.hour, now.minute, now.second)


SUNRISE_HOURS = (5, 7)
SUNSET_HOURS = (21, 23)
SUNRISE_ELAPSED = (day_elapsed(SUNRISE_HOURS[0]), day_elapsed(SUNRISE_HOURS[1]))
SUNSET_ELAPSED = (day_elapsed(SUNSET_HOURS[0]), day_elapsed(SUNSET_HOURS[1]))


def sunup_amount() -> float:
	"""What temperature the monitor should be"""
	elapsed = now_elapsed()

	if elapsed <= SUNRISE_ELAPSED[0]:
		return 0
	elif elapsed < SUNRISE_ELAPSED[1]:
		return lerped_amount(elapsed, SUNRISE_ELAPSED)
	elif elapsed <= SUNSET_ELAPSED[0]:
		return 1
	elif elapsed < SUNSET_ELAPSED[1]:
		return 1 - lerped_amount(elapsed, SUNSET_ELAPSED)
	else:
		return 0


def on_steam() -> bool:
	"""Whether the Steam PID is in use"""
	pid_file = Path("~/.steampid").expanduser().resolve(strict=True)
	pid = pid_file.read_text().strip()
	pid_dir = Path("/proc") / pid
	return pid_dir.is_dir()
