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

from .statements import *

@override_settings(TESTING=True)
class WorkoutViewTest(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',
                                      showfavoritechartnotes=True)

        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()

        result = get_random_file(filename='rowers/tests/testdata/onwater2.csv')

        self.wwater = WorkoutFactory(user=self.r,
                                     csvfilename=result['filename'],
                                     starttime=result['starttime'],
                                     startdatetime=result['startdatetime'],
                                     duration=result['duration'],
                                     distance=result['totaldist'],
                                     workouttype = 'water',
        )

        result = get_random_file(filename='rowers/tests/testdata/quiske_in_stroke.csv')

        self.winstroke = WorkoutFactory(user=self.r,
                                     csvfilename=result['filename'],
                                     starttime=result['starttime'],
                                     startdatetime=result['startdatetime'],
                                     duration=result['duration'],
                                     distance=result['totaldist'],
                                     workouttype = 'water',
        )

        result = get_random_file(filename='rowers/tests/testdata/erg1.csv')

        self.werg1 = WorkoutFactory(user=self.r,
                                     csvfilename=result['filename'],
                                     starttime=result['starttime'],
                                     startdatetime=result['startdatetime'],
                                     duration=result['duration'],
                                     distance=result['totaldist'],
                                     workouttype = 'rower',
        )

        result = get_random_file(filename='rowers/tests/testdata/erg2.csv')

        self.werg2 = WorkoutFactory(user=self.r,
                                     csvfilename=result['filename'],
                                     starttime=result['starttime'],
                                     startdatetime=result['startdatetime'],
                                     duration=result['duration'],
                                     distance=result['totaldist'],
                                     workouttype = 'rower',
        )

        result = get_random_file(filename='rowers/tests/testdata/erg3.csv')

        self.werg3 = WorkoutFactory(user=self.r,
                                     csvfilename=result['filename'],
                                     starttime=result['starttime'],
                                     startdatetime=result['startdatetime'],
                                     duration=result['duration'],
                                     distance=result['totaldist'],
                                     workouttype = 'rower',
        )

        self.fav1 = FavoriteChart.objects.create(
            yparam1='hr',
            yparam2='spm',
            xparam='distance',
            workouttype='all',
            reststrokes=False,
            notes=faker.word(),
            user=self.r
            )

        self.fav2 = FavoriteChart.objects.create(
            yparam1='power',
            yparam2='strokeenergy',
            xparam='distance',
            workouttype='all',
            reststrokes=False,
            notes=faker.word(),
            user=self.r
            )

    def tearDown(self):
        pass

    @patch('rowers.dataprep.create_engine')
    @patch('rowers.dataprep.getsmallrowdata_db')
    def test_flexchart_water(self, mocked_sqlalechemy, mocked_getsmallrowdata_db):
        login = self.c.login(username=self.u.username, password=self.password)
        self.assertTrue(login)

        url  = reverse('workout_flexchart3_view',
                       kwargs={'id':encoder.encode_hex(self.wwater.id)})

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

        # change chart
        form_data = {
            'xaxis':'time',
            'yaxis1':'hr',
            'yaxis2': 'spm',
            'includereststrokes': True,
            'plottype': 'line'
            }

        form = FlexOptionsForm(form_data)
        if not form.is_valid():
            print(form.errors)
        self.assertTrue(form.is_valid())

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

        url = reverse('workout_flexchart3_view',kwargs={
            'id':encoder.encode_hex(self.wwater.id),
            'xparam':'distance',
            'yparam1':'hr',
            'yparam2':'power'
            })

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

        form_data = {
            'workstrokesonlysave':True,
            'savefavorite':True
            }


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

    @patch('rowers.dataprep.create_engine')
    @patch('rowers.dataprep.getsmallrowdata_db')
    def test_flexchart_erg(self, mocked_sqlalechemy, mocked_getsmallrowdata_db):
        login = self.c.login(username=self.u.username, password=self.password)
        self.assertTrue(login)

        url  = reverse('workout_flexchart3_view',
                       kwargs={'id':encoder.encode_hex(self.werg1.id)})

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

        # change chart
        form_data = {
            'xaxis':'time',
            'yaxis1':'hr',
            'yaxis2': 'spm',
            'includereststrokes': True,
            'plottype': 'line'
            }

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

        url = reverse('workout_flexchart3_view',kwargs={
            'id':encoder.encode_hex(self.werg1.id),
            'xparam':'distance',
            'yparam1':'hr',
            'yparam2':'power'
            })

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

        form_data = {
            'workstrokesonlysave':True,
            'savefavorite':True
            }

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