import datetime
import mock

from django.contrib.auth import get_user_model
from django.test import TestCase
from django.test.utils import override_settings
from django.utils import timezone

from gcm.gcm import GCMNotRegisteredException

from pushy.models import (
    PushNotification,
    Device,
    get_filtered_devices_queryset
)

from pushy.tasks import (
    check_pending_push_notifications,
    send_push_notification_group,
    send_single_push_notification,
    create_push_notification_groups,
    clean_sent_notifications
)


class TasksTestCase(TestCase):
    def setUp(self):
        self.payload = {
            'key1': 'value1',
            'key2': 'value2',
        }

    def test_get_filtered_devices_queryset_on_type(self):
        notification = PushNotification.objects.create(
            title='test',
            payload=self.payload,
            active=PushNotification.PUSH_ACTIVE,
            sent=PushNotification.PUSH_NOT_SENT,
            filter_type=Device.DEVICE_TYPE_IOS
        )

        for i in range(0, 3):
            Device.objects.create(
                key='TEST_DEVICE_KEY_{}'.format(i),
                type=Device.DEVICE_TYPE_IOS
            )

        for i in range(0, 10):
            Device.objects.create(
                key='TEST_DEVICE_KEY_{}'.format(i),
                type=Device.DEVICE_TYPE_ANDROID
            )

        devices = get_filtered_devices_queryset(notification)
        self.assertEqual(devices.count(), 3)

    def test_get_filtered_devices_queryset_on_user(self):
        for i in range(0, 3):
            get_user_model().objects.create_user(
                username='test_user_%d' % i,
                email='test_user_%d@django-pushy.com' % i,
                password=i
            )

        user1 = get_user_model().objects.get(pk=1)
        user2 = get_user_model().objects.get(pk=2)

        # Add 5 devices to user2
        for i in range(0, 5):
            Device.objects.create(
                key='TEST_DEVICE_KEY_{}'.format(i),
                type=Device.DEVICE_TYPE_ANDROID,
                user=user2
            )

        # Check that user2 has 5 devices
        notification = PushNotification.objects.create(
            title='test',
            payload=self.payload,