from datetime import datetime, timedelta

from django.test import TestCase
from django_dynamic_fixture import G
import pytz

from ..models import LocalizedRecurrence, LocalizedRecurrenceQuerySet
from ..models import _replace_with_offset, _update_schedule


class LocalizedRecurrenceUpdateTest(TestCase):
    """
    Tests calling 'update' on a localized recurrence.
    """
    def test_update_creation(self):
        lr = LocalizedRecurrence()
        lr.update()
        self.assertIsNotNone(lr.id)

    def test_update_timezone(self):
        lr = G(LocalizedRecurrence)
        lr.update(timezone='US/Eastern')
        lr = LocalizedRecurrence.objects.get(id=lr.id)
        self.assertEqual(lr.timezone, pytz.timezone('US/Eastern'))

    def test_update_offset(self):
        lr = G(LocalizedRecurrence)
        lr.update(offset=timedelta(seconds=1))
        lr = LocalizedRecurrence.objects.get(id=lr.id)
        self.assertEqual(lr.offset, timedelta(seconds=1))


class LocalizedRecurrenceQuerySetTest(TestCase):
    """Simple test to ensure the custom query set is being used.
    """
    def test_isinstance(self):
        G(LocalizedRecurrence)
        recurrences = LocalizedRecurrence.objects.all()
        self.assertIsInstance(recurrences, LocalizedRecurrenceQuerySet)


class LocalizedRecurrenceQuerySetUpdateScheduleTest(TestCase):
    """Test that updates to recurrences are reflected in the DB.
    """
    def setUp(self):
        G(LocalizedRecurrence, interval='DAY', offset=timedelta(hours=12), timezone=pytz.timezone('US/Eastern'))
        G(LocalizedRecurrence, interval='MONTH', offset=timedelta(hours=15), timezone=pytz.timezone('US/Eastern'))

    def test_update_from_1970(self):
        """Start with next_scheduled of 1970, after update should be new.
        """
        time = datetime(year=2013, month=5, day=20, hour=12, minute=3)
        LocalizedRecurrence.objects.filter(interval='DAY').update_schedule(time=time)
        lr_day = LocalizedRecurrence.objects.filter(interval='DAY').first()
        self.assertGreater(lr_day.next_scheduled, time)


class LocalizedRecurrenceManagerUpdateScheduleTest(TestCase):
    def setUp(self):
        G(LocalizedRecurrence, interval='DAY', offset=timedelta(hours=12), timezone=pytz.timezone('US/Eastern'))
        G(LocalizedRecurrence, interval='MONTH', offset=timedelta(hours=15), timezone=pytz.timezone('US/Eastern'))

    def test_update_all(self):
        """Calls to the model manager to update should be passed through.
        """
        time = datetime(year=2013, month=5, day=20, hour=15, minute=3)
        LocalizedRecurrence.objects.update_schedule(time=time)
        self.assertTrue(all(r.next_scheduled > time for r in LocalizedRecurrence.objects.all()))


class LocalizedRecurrenceTest(TestCase):
    """Test the creation and querying of LocalizedRecurrence records.
    """
    def setUp(self):
        G(LocalizedRecurrence, interval='DAY', offset=timedelta(hours=12), timezone=pytz.timezone('US/Eastern'))

    def test_timedelta_returned(self):
        """Test that the Duration field is correctly returning timedeltas.
        """
        lr = LocalizedRecurrence.objects.first()