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 rowers.views import isplanmember,iscoachmember,ispromember
from rowers.rower_rules import *

class TrialsTest(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='basic')

        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_basictrial(self):
        self.assertEqual(isplanmember(self.u),False)
        self.assertEqual(iscoachmember(self.u),False)
        self.assertEqual(ispromember(self.u),False)

        self.r.protrialexpires = (nu+datetime.timedelta(days=10)).date()
        self.r.save()

        self.assertEqual(isplanmember(self.u),False)
        self.assertEqual(iscoachmember(self.u),False)
        self.assertEqual(ispromember(self.u),True)

        self.r.plantrialexpires = (nu+datetime.timedelta(days=10)).date()
        self.r.save()

        self.assertEqual(isplanmember(self.u),True)
        self.assertEqual(iscoachmember(self.u),False)
        self.assertEqual(ispromember(self.u),True)

    def test_protrial(self):
        self.r.rowerplan = 'pro'
        self.r.save()
        self.assertEqual(isplanmember(self.u),False)
        self.assertEqual(iscoachmember(self.u),False)
        self.assertEqual(ispromember(self.u),True)

        self.r.plantrialexpires = (nu+datetime.timedelta(days=10)).date()
        self.r.save()

        self.assertEqual(is_plantrial(self.u),True)
        self.assertEqual(isplanmember(self.u),True)
        self.assertEqual(iscoachmember(self.u),False)
        self.assertEqual(ispromember(self.u),True)
