#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime
from recurrent import RecurringEvent

from .errors import AnswerError

class Question(object):
    def __init__(self, config):
        self.key, self.config = config.items()[0]

        parser = RecurringEvent(now_date=datetime.now())

        # set attributes
        self.unit = self.config.get('unit', 'number')
        self.interval = parser.parse(self.config['interval'])
        self.text = self.config['text']

    def hint(self):
        hint = 'default: %r' % self.default
        if self.lower and self.upper:
            hint += ', range: %r to %r' % (self.lower, self.upper)
        elif self.lower:
            hint += ', lower: %r' % self.lower
        elif self.upper:
            hint += ', upper: %r' % self.upper

        return hint

    def __repr__(self):
        return '<%s: key: %s, %s>' % (
            self.__class__.__name__, 
            self.key,
            self.hint()
        )


class NumberQuestion(Question):
    def __init__(self, config):
        super(NumberQuestion, self).__init__(config)
        self.converter = float
        self._type = 'number'

        self.lower = self.config.get('lower', None)
        self.upper = self.config.get('upper', None)
        self.default = self.config.get('default', 0)

    def parse_answer(self, ans):
        try:
            ans = self.converter(ans)
        except ValueError:
            raise AnswerError('Could not parse "%r" as a %s' % (ans, self._type))

        if self.lower and ans < self.lower:
            raise AnswerError(
                '"%r" was lower than the lower bound, %r' % (ans, self.lower)
            )

        if self.upper and ans > self.upper:
            raise AnswerError(
                '"%r" was higher than the upper bound, %r' % (ans, self.upper)
            )

        return ans


class HoursQuestion(NumberQuestion):
    def __init__(self, config):
        super(HoursQuestion, self).__init__(config)

        self.lower = self.config.get('lower', 0)
        self.default = self.config.get('default', 8)


class RatingQuestion(NumberQuestion):
    def __init__(self, config):
        super(RatingQuestion, self).__init__(config)

        self.converter = int
        self._type = 'integer'

        self.lower = self.config.get('lower', 0)
        self.upper = self.config.get('upper', 10)


class YesNoQuestion(Question):
    def __init__(self, config):
        super(YesNoQuestion, self).__init__(config)

        self.upper = None
        self.lower = None
        self.default = False

    def parse_answer(self, ans):
        if ans.lower().startswith('y'):
            return True
        elif ans.lower().startswith('f'):
            return False

        try:
            ans_i = int(ans)
            if ans_i == 1:
                return True
            elif ans_i == 0:
                return False
        except ValueError:
            pass

        raise AnswerError('Can\'t handle "%s" as Yes/No' % ans)

@@ -569,24 +569,36 @@
  self.lower 
+is not None 
 and self.upp
@@ -591,32 +591,44 @@
 e and self.upper
+ is not None
 :%0A            hi
@@ -695,32 +695,44 @@
  elif self.lower
+ is not None
 :%0A            hi
@@ -779,32 +779,44 @@
  elif self.upper
+ is not None
 :%0A            hi
@@ -1163,25 +1163,29 @@
 elf.
-converter = float
+_type_name = 'number'
 %0A   
@@ -1198,24 +1198,20 @@
 elf.
-_
 type = 
-'number'
+float
 %0A%0A  
@@ -1435,25 +1435,20 @@
  = self.
-converter
+type
 (ans)%0A  
@@ -1551,16 +1551,21 @@
 lf._type
+_name
 ))%0A%0A    
@@ -2285,17 +2285,12 @@
 elf.
-converter
+type
  = i
@@ -2310,16 +2310,21 @@
 lf._type
+_name
  = 'inte
@@ -2538,32 +2538,58 @@
 init__(config)%0A%0A
+        self.type = bool%0A%0A
         self.upp

change interface to accomadate cli parsing