import logging
from threading import Timer
from datetime import timedelta

from rx.core import Scheduler, Disposable
from rx.disposables import SingleAssignmentDisposable, CompositeDisposable

from .schedulerbase import SchedulerBase

log = logging.getLogger("Rx")


class TimeoutScheduler(SchedulerBase):
    """A scheduler that schedules work via a timed callback based upon platform."""

    def schedule(self, action, state=None):
        """Schedules an action to be executed."""

        disposable = SingleAssignmentDisposable()

        def interval():
            disposable.disposable = self.invoke_action(action, state)
        timer = Timer(0, interval)
        timer.setDaemon(True)
        timer.start()

        def dispose():
            timer.cancel()
        return CompositeDisposable(disposable, Disposable.create(dispose))

    def schedule_relative(self, duetime, action, state=None):
        """Schedules an action to be executed after duetime."""

        scheduler = self
        timespan = self.to_timedelta(duetime)
        if timespan == timedelta(0):
            return scheduler.schedule(action, state)

        disposable = SingleAssignmentDisposable()

        def interval():
            disposable.disposable = self.invoke_action(action, state)

        seconds = timespan.total_seconds()
        log.debug("timeout: %s", seconds)
        timer = Timer(seconds, interval)
        timer.setDaemon(True)
        timer.start()

        def dispose():
            timer.cancel()

        return CompositeDisposable(disposable, Disposable.create(dispose))

    def schedule_absolute(self, duetime, action, state=None):
        """Schedules an action to be executed after duetime."""

        duetime = self.to_datetime(duetime)
        return self.schedule_relative(duetime - self.now, action, state)

    def _start_timer(self, period, action):
        timer = Timer(period, action)
        timer.setDaemon(True)
        timer.start()

        return timer


timeout_scheduler = TimeoutScheduler()

[((10, 6, 10, 29), 'logging.getLogger', 'logging.getLogger', ({(10, 24, 10, 28): '"""Rx"""'}, {}), "('Rx')", False, 'import logging\n'), ((19, 21, 19, 49), 'rx.disposables.SingleAssignmentDisposable', 'SingleAssignmentDisposable', ({}, {}), '()', False, 'from rx.disposables import SingleAssignmentDisposable, CompositeDisposable\n'), ((23, 16, 23, 34), 'threading.Timer', 'Timer', ({(23, 22, 23, 23): '0', (23, 25, 23, 33): 'interval'}, {}), '(0, interval)', False, 'from threading import Timer\n'), ((39, 21, 39, 49), 'rx.disposables.SingleAssignmentDisposable', 'SingleAssignmentDisposable', ({}, {}), '()', False, 'from rx.disposables import SingleAssignmentDisposable, CompositeDisposable\n'), ((46, 16, 46, 40), 'threading.Timer', 'Timer', ({(46, 22, 46, 29): 'seconds', (46, 31, 46, 39): 'interval'}, {}), '(seconds, interval)', False, 'from threading import Timer\n'), ((62, 16, 62, 37), 'threading.Timer', 'Timer', ({(62, 22, 62, 28): 'period', (62, 30, 62, 36): 'action'}, {}), '(period, action)', False, 'from threading import Timer\n'), ((29, 47, 29, 73), 'rx.core.Disposable.create', 'Disposable.create', ({(29, 65, 29, 72): 'dispose'}, {}), '(dispose)', False, 'from rx.core import Scheduler, Disposable\n'), ((36, 23, 36, 35), 'datetime.timedelta', 'timedelta', ({(36, 33, 36, 34): '(0)'}, {}), '(0)', False, 'from datetime import timedelta\n'), ((53, 47, 53, 73), 'rx.core.Disposable.create', 'Disposable.create', ({(53, 65, 53, 72): 'dispose'}, {}), '(dispose)', False, 'from rx.core import Scheduler, Disposable\n')]