import time

from .base import BaseCommand


class DefaultdelayCommand(BaseCommand):

    def __init__(self, args):
        super(DefaultdelayCommand, self).__init__(args)
        try:
            self.wait_time_ms = 10 * int(args)  # arg unit is 10ms, weird.
        except (TypeError, ValueError):
            raise ValueError("Invalid time value, should be int")

        # doc doesn't specify max, keeping same values as delay
        if not (0 < self.wait_time_ms <= 10000):
            raise ValueError("Invalid time value, should be > 0 and <= 10000")

    def _run(self, env):
        env["default_delay"] = self.wait_time_ms


Default_DelayCommand = DefaultdelayCommand


class DelayCommand(BaseCommand):

    def __init__(self, args):
        super(DelayCommand, self).__init__(args)
        try:
            self.wait_time_ms = int(args)
        except (TypeError, ValueError):
            raise ValueError("Invalid time value, should be int")

        if not (0 < self.wait_time_ms <= 10000):
            raise ValueError("Invalid time value, should be > 0 and <= 10000")

    def _run(self, env):
        # sleep is in secs, ducky in ms
        time.sleep(self.wait_time_ms / 1000.0)
