from datetime import datetime, timedelta
import pytz

from django.test import TestCase
from django.test.client import Client
from django.core.urlresolvers import reverse
from django.core.management import call_command
from django.utils.timezone import utc

from django.contrib.auth.models import User, Group
from django.contrib.auth import login
from ed_news.models import UserProfile
from ed_news.views import KARMA_LEVEL_ACTIVE_MEMBERS
from ed_news.views import increment_karma, decrement_karma, is_active_member
import ed_news.views as views

from ed_news.models import Submission, Article, Comment

import random

from test_utilities import create_user_with_profile


class EdNewsViewTests(TestCase):

    def test_join_active_members(self):
        """
        If a member crosses the active_member threshold,
        they should be put in the active_member group automatically.
        """
        new_user = create_user_with_profile('paulo', 'password')

        # Make active_members group.
        #  This should run the make_groups.py script.
        active_members = Group(name='Active Members')
        active_members.save()

        # Set karma just below critical level.
        #  Test that user is not in active_members.
        new_user.userprofile.karma = KARMA_LEVEL_ACTIVE_MEMBERS - 1
        self.assertEqual(is_active_member(new_user), False)

        # Increment karma past threshold, not just to threshold.
        #  Test that user is now in active_members.
        increment_karma(new_user)
        increment_karma(new_user)
        self.assertEqual(is_active_member(new_user), True)

        # Decrement karma below threshold.
        #  Test that user is no longer in active_members.
        decrement_karma(new_user)
        decrement_karma(new_user)
        self.assertEqual(is_active_member(new_user), False)


    def test_undo_upvote_submission(self):
        # Create a user, make a submission.
        # Create a user. Upvote submission.
        #  Verify user 2 in submission.upvotes.
        #  Verify user 1 karma increases.
        # Undo upvote (resubmit upvote request.)
        #  Verify user 2 not in submission.upvotes.
        #  Verify user 1 karma back to original.

        # DEV: unfinished

        # Create 2 users.
        user_0 = create_user_with_profile('user_0', 'user_0')
        user_1 = create_user_with_profile('user_1', 'user_1')
        # Log user_0 in, and make a submission.
        c = Client()
        #response = c.post('/login/', {'username': user_0.username, 'password': 'user_0'})
        #self.assertEqual(response.status_code, 200)
        login = c.login(username=user_0.username, password='user_0')
        self.assertEqual(login, True)

        response = c.post('/submit_link/', {'url': 'http://google.com', 'title': 'google'})
        self.assertEqual(response.status_code, 200)

        original_karma = user_0.userprofile.karma
        
        login = c.login(username=user_1.username, password='user_1')
        self.assertEqual(login, True)

        #print Submission.objects.all()[0].id
        return 0
        response = c.get('/upvote_submission/', {'submission_id': 5})
        self.assertEqual(response.status_code, 200)


    def test_login_client_user(self):
        # Prove that I know how to log in a user using the test client.
        c = Client()
        user = create_user_with_profile('user_0', 'password')
        login = c.login(username=user.username, password='password')
        self.assertEqual(login, True)


    def test_login_page(self):
        pass
        # Can't rely on status_code==200 to verify /login/ page successful.
        #  Failed login attempt still returns a proper html response.
        #response = c.post('/login/', {'username': user.username, 'password': 'password'})
        #print 'login page successful: ', response
        # Make sure all users can log in.
        num_users = 5
        for x in range(0,num_users):
            # Each user's password is their username.
            new_user = create_user_with_profile('user_%d' % x, 'user_%d' %x)

        c = Client()
        for user in User.objects.all():