"""Animation utilities for the transformation example."""

import time
from typing import Self


class Animation:
    """An animation iterator."""

    def __init__(self, start: float, stop: float, duration: float, frequency: float = 25) -> None:
        """Initializes the animation iterator.

        Args:
            start: The start value for the animation.
            stop: The end value for the animation.
            duration: The animation duration in seconds.
            frequency: The frequency for animation steps. Defaults to 25 Hz.
        """
        self.frequency = frequency
        self.steps = round(frequency * duration)
        self.values = iter(start + (i * ((stop - start) / self.steps)) for i in range(self.steps + 1))

    def __iter__(self) -> Self:
        """Returns self as an iterator."""
        return self

    def __next__(self) -> float:
        """Returns the next iteration step of the animation and sleeps."""
        time.sleep(1 / self.frequency)
        return next(self.values)
