from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from .statements import *

nu = datetime.datetime.now()

from django.http import Http404

from rowers.views import get_workout

class TestDateTime(TestCase):
    def setUp(self):
        self.factory = RequestFactory()

    def tearDown(self):
        pass

    def test_get_dates(self):
        request = self.factory.get('/rowers/sessions/create/user/230/?startdate=13.01.2020&enddate=19.01.2020')
        startdate,enddate = get_dates_timeperiod(request)

        teststart = datetime.date(2020,1,1)
        self.assertTrue(teststart<startdate)

        testend = datetime.date(2020,12,12)
        self.assertTrue(testend>enddate)


# tests simple functions from views.py
class SimpleViewTest(TestCase):
    def setUp(self):
        self.u = UserFactory()

        self.r = Rower.objects.create(user=self.u,
                                      birthdate=faker.profile()['birthdate'],
                                      gdproptin=True,surveydone=True,
                                      gdproptindate=timezone.now(),
                                      rowerplan='coach')

        self.c = Client()
        self.user_workouts = WorkoutFactory.create_batch(5, user=self.r)
        #        self.factory = RequestFactory()
        self.password = faker.word()
        self.u.set_password(self.password)
        self.u.save()

    def tearDown(self):
        for workout in self.user_workouts:
            try:
                os.remove(workout.csvfilename)
            except (IOError, FileNotFoundError,OSError):
                pass


    def test_getrequestrower(self):
        user_no_rower = UserFactory(username='norower')
        user_no_rower.set_password(faker.word())


        login = self.c.login(username=self.u.username, password=self.password)
        self.assertTrue(login)

        url = '/rowers/list-workouts/user/{id}/'.format(id=user_no_rower.id)

        response = self.c.get(url)
        self.assertIn(response.status_code,[403,404])

    def test_sendmail(self):
        login = self.c.login(username=self.u.username, password=self.password)
        self.assertTrue(login)

        url = '/rowers/email/send/'

        form_data = {
            'firstname': 'John',
            'lastname':'Doe',
            'email':'roosendaalsander@gmail.com',
            'subject':'testing',
            'botcheck': True,
            'message': faker.text()
            }

        form = EmailForm(form_data)

        self.assertTrue(form.is_valid())

        response = self.c.post(url,form_data,follow=True)
        self.assertEqual(response.status_code,200)

        self.assertRedirects(response,
                             expected_url='/rowers/email/',
                             status_code=302,target_status_code=200)


    def test_getworkout(self):
        with assert_raises(Http404):
            w = get_workout(encoder.encode_hex(123))

    def test_deactivate(self):
        login = self.c.login(username=self.u.username, password=self.password)
        self.assertTrue(login)

        url = '/rowers/me/deactivate/'
        response = self.c.get(url)
        self.assertEqual(response.status_code,200)

        form_data = {
            'is_active': True
            }

        response = self.c.post(url, form_data)
        self.assertEqual(response.status_code, 200)

        form_data = {
            'is_active': False
            }

        response = self.c.post(url, form_data, follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertRedirects(response,
                            expected_url='/login/',
                            status_code=302,target_status_code=200)

        login = self.c.login(username=self.u.username, password=self.password)
        self.assertEqual(login,False)
