from datetime import datetime
from typing import (
    Iterator,
    Optional,
)

from timeutil import Clock
from ._SleepDateIterator import SleepDateIterator


class ClockStub(Clock):
    def __init__(self, now=None, utc_now=None):
        # type: (Iterator[datetime], Iterator[datetime]) -> None
        self.now_value = now
        self._last_returned_now: Optional[datetime] = None
        self.utc_now_value = utc_now
        self._last_returned_utc_now: Optional[datetime] = None

    def now(self):
        self._last_returned_now = next(self.now_value)
        return self._last_returned_now

    def utc_now(self):
        self._last_returned_utc_now = next(self.utc_now_value)
        return self._last_returned_utc_now

    @property
    def last_returned_now(self):
        return self._last_returned_now

    @property
    def last_returned_utc_now(self):
        return self._last_returned_utc_now

    def sleep_for(self, seconds: float):
        if isinstance(self.now_value, SleepDateIterator):
            self.now_value.sleep_for(seconds=seconds)
        if isinstance(self.utc_now_value, SleepDateIterator):
            self.utc_now_value.sleep_for(seconds=seconds)
