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

#from __future__ import print_function
from .statements import *
nu = datetime.datetime.now()



tested = [
    '/rowers/me/delete/'
]

#@pytest.mark.django_db
@override_settings(TESTING=True)
class URLTests(TestCase):
    def setUp(self):
        redis_connection.publish('tasks','KILL')
        u = User.objects.create_user('john',
                                     'sander@ds.ds',
                                     'koeinsloot',
                                     )
        r = Rower.objects.create(user=u,rowerplan='coach',gdproptin=True,
                                 gdproptindate=timezone.now())
        self.c = Client()

        self.nu = datetime.datetime.now()
        filename = 'rowers/tests/testdata/testdata.csv'
        self.wotw = Workout.objects.create(name='testworkout',
                                        workouttype='water',
                                        user=r,date=self.nu.strftime('%Y-%m-%d'),
                                        starttime=self.nu.strftime('%H:%M:%S'),
                                        duration="0:55:00",distance=8000,
                                        csvfilename=filename)

        self.wote = Workout.objects.create(name='testworkout',
                                        workouttype='Indoor Rower',
                                        user=r,date=self.nu.strftime('%Y-%m-%d'),
                                        starttime=self.nu.strftime('%H:%M:%S'),
                                        duration="0:55:00",distance=8000,
                                        csvfilename=filename)

        powerperc = 100*np.array([r.pw_ut2,
                                  r.pw_ut1,
                                  r.pw_at,
                                  r.pw_tr,r.pw_an])/r.ftp


        self.hrdata = {
            'hrmax':r.max,
            'hrut2':r.ut2,
            'hrut1':r.ut1,
            'hrat':r.at,
            'hrtr':r.tr,
            'hran':r.an,
            'ftp':r.ftp,
            'powerperc':serialize_list(powerperc),
            'powerzones':serialize_list(r.powerzones),
        }

    urlstotest = [
        '/rowers/400/',
        '/rowers/403/',
        '/rowers/404/',
        '/rowers/500/',
        '/rowers/502/',
#        '/rowers/1/list-workouts/',
#        '/rowers/1/list-workouts/2016-01-01/2016-12-31/',
#        '/rowers/1/ote-bests/',
#        '/rowers/1/ote-bests2/2016-01-01/2016-12-31/',
        '/rowers/about/',
        '/rowers/workout/addmanual/',
        '/rowers/agegroupcp/30/',
        '/rowers/agegroupcp/30/1/',
        '/rowers/agegrouprecords/male/hwt/',
        '/rowers/agegrouprecords/male/hwt/2000m/',
        '/rowers/agegrouprecords/male/hwt/2000min/',
        '/rowers/ajax_agegroup/45/hwt/male/1/',
        '/rowers/analysis/',
        '/rowers/analysis/user/1/',
        '/rowers/brochure/',
        '/rowers/courses/upload/',
        '/rowers/createplan/',
        '/rowers/createplan/user/1/',
        '/rowers/cumstats/',
#        '/rowers/cumstats/2016-01-01/2016-12-31/',
        '/rowers/cumstats/user/1/',
#        '/rowers/cumstats/user/1/2016-01-01/2016-12-31/',
        '/rowers/developers/',
        '/rowers/email/',
        '/rowers/email/thankyou/',
        '/rowers/fitness-progress/',
        '/rowers/fitness-progress/user/1/',
        '/rowers/fitness-progress/user/1/rower/',
        '/rowers/flexall/',
        '/rowers/flexall/spm/hr/None/',
#        '/rowers/flexall/spm/hr/None/2016-01-01/2016-12-31/',
#        '/rowers/flexall/spm/hr/None/2016-01-01/2016-12-31/user/1/',
        '/rowers/flexall/user/1/',
        '/rowers/flexalldata/',
        '/rowers/help/',
        '/rowers/histo/',
        '/rowers/histo/user/1/',
#        '/rowers/histo/user/1/2016-01-01/2016-12-31/',
        '/rowers/histodata/',
#        '/rowers/job-kill/1/',
#        '/rowers/jobs-status/',
        '/rowers/laboratory/',
        '/rowers/laboratory/user/1/',
        '/rowers/legal/',
        '/rowers/list-courses/',
        '/rowers/list-graphs/',
        '/rowers/list-jobs/',
        '/rowers/list-workouts/',
#        '/rowers/list-workouts/2016-01-01/2016-12-31/',
#        '/rowers/list-workouts/2016-01-01/2016-12-31/user/1/',
        '/rowers/list-workouts/ranking/',
        '/rowers/list-workouts/user/1/',
        '/rowers/me/calcdps/',
        '/rowers/me/edit/',
        '/rowers/me/edit/user/1/',
        '/rowers/me/exportsettings/',
        '/rowers/me/exportsettings/user/1/',
        '/rowers/me/favoritecharts/',
        '/rowers/me/favoritecharts/user/1/',
        '/rowers/me/invitation/',
        '/rowers/me/preferences/',
        '/rowers/me/preferences/user/1/',
        '/rowers/me/request/',
        '/rowers/me/teams/',
        '/rowers/me/workflowconfig2/',
        '/rowers/me/workflowconfig2/user/1/',
        '/rowers/me/workflowdefault/',
#        '/rowers/multi-compare/',
        '/rowers/ote-bests/',
#        '/rowers/ote-bests/2016-01-01/2016-12-31/',
        '/rowers/ote-bests/user/1/',
#        '/rowers/ote-bests/user/1/2016-01-01/2016-12-31/',
        '/rowers/ote-bests2/',
#        '/rowers/ote-bests2/2016-01-01/2016-12-31/',
        '/rowers/ote-bests2/user/1/',
        '/rowers/ote-ranking/',
#        '/rowers/ote-ranking/2016-01-01/2016-12-31/',
        '/rowers/ote-ranking/user/1/',
#        '/rowers/ote-ranking/user/1/2016-01-01/2016-12-31/',
        '/rowers/otw-bests/',
#        '/rowers/otw-bests/2016-01-01/2016-12-31/',
#        '/rowers/otw-bests/user/1/2016-01-01/2016-12-31/',
        '/rowers/partners/',
        '/rowers/physics/',
        '/rowers/planrequired/',
#        '/rowers/promembership/',
        '/rowers/register/',
        '/rowers/register/thankyou/',
        '/rowers/sessions/',
#        '/rowers/sessions/2016-01-01/2016-12-31/',
#        '/rowers/sessions/2016-01-01/2016-12-31/user/1/',
        '/rowers/sessions/coach/',
#        '/rowers/sessions/coach/user/1/',
        '/rowers/sessions/create/',
#        '/rowers/sessions/create/user/1/',
        '/rowers/sessions/manage/',
#        '/rowers/sessions/manage/user/1/',
        '/rowers/sessions/multiclone/',
#        '/rowers/sessions/multiclone/user/1/',
        '/rowers/sessions/multicreate/',
#        '/rowers/sessions/multicreate/user/1/',
        '/rowers/sessions/print/',
#        '/rowers/sessions/print/user/1/',
        '/rowers/sessions/teamcreate/',
#        '/rowers/sessions/user/1/',
        '/rowers/team-compare-select/',
        '/rowers/team-compare-select/workout/'+encoded1+'/',
#        '/rowers/team-compare-select/2016-01-01/2016-12-31/',
#        '/rowers/test-job/2/',
#        '/rowers/test-job2/2/',
#        '/rowers/test_callback/',
#        '/rowers/updatefitness/',
#        '/rowers/updatefitness/rower/',
#        '/rowers/updatefitness/rower/50/',
#        '/rowers/user-boxplot/',
#        '/rowers/user-boxplot-data/',
#        '/rowers/user-boxplot-select/',
#        '/rowers/user-boxplot-select/user/1/',
#        '/rowers/user-boxplot/user/1/',
#        '/rowers/user-multiflex-data/',
#        '/rowers/user-multiflex-select/',
#        '/rowers/user-multiflex-select/2016-01-01/2016-12-31/',
#        '/rowers/user-multiflex-select/user/1/',
#        '/rowers/user-multiflex-select/user/1/2016-01-01/2016-12-31/',
#        '/rowers/user-multiflex/',
#        '/rowers/user-multiflex/user/1/',
        '/rowers/workout/'+encoded1+'/',
        '/rowers/workout/'+encoded1+'/adddistanceplot/',
        '/rowers/workout/'+encoded1+'/adddistanceplot2/',
        '/rowers/workout/'+encoded1+'/addotwpowerplot/',
        '/rowers/workout/'+encoded1+'/addpiechart/',
        '/rowers/workout/'+encoded1+'/addpowerpiechart/',
        '/rowers/workout/'+encoded1+'/addstatic/',
        '/rowers/workout/'+encoded1+'/addstatic/3/',
        '/rowers/workout/'+encoded1+'/addtimeplot/',
        '/rowers/workout/'+encoded1+'/addtimeplot2/',
        '/rowers/workout/'+encoded1+'/comment/',
        '/rowers/workout/'+encoded1+'/darkskywind/',
        '/rowers/workout/'+encoded1+'/data/',
        '/rowers/workout/'+encoded1+'/edit/',
        '/rowers/workout/'+encoded1+'/editintervals/',
        '/rowers/workout/'+encoded1+'/flexchart/',
        '/rowers/workout/'+encoded1+'/forcecurve/',
        '/rowers/workout/'+encoded1+'/get-thumbnails/',
        '/rowers/workout/'+encoded1+'/histo/',
        '/rowers/workout/'+encoded1+'/image/',
        '/rowers/workout/'+encoded1+'/instroke/',
        '/rowers/workout/'+encoded1+'/interactiveotwplot/',
        '/rowers/workout/'+encoded1+'/map/',
        '/rowers/workout/'+encoded1+'/otwsetpower/',
        '/rowers/workout/'+encoded1+'/recalcsummary/',
        '/rowers/workout/'+encoded1+'/restore/',
        '/rowers/workout/'+encoded1+'/smoothenpace/',
        '/rowers/workout/'+encoded1+'/split/',
        '/rowers/workout/'+encoded1+'/stats/',
        '/rowers/workout/'+encoded1+'/stream/',
#        '/rowers/workout/'+encoded1+'/task/',
#        '/rowers/workout/'+encoded1+'/teststrokedata/',
        '/rowers/workout/'+encoded1+'/toggle-ranking/',
        '/rowers/workout/'+encoded1+'/undosmoothenpace/',
        '/rowers/workout/'+encoded1+'/unsubscribe/',
        '/rowers/workout/'+encoded1+'/updatecp/',
        '/rowers/workout/'+encoded1+'/view/',
        '/rowers/workout/'+encoded1+'/wind/',
        '/rowers/workout/'+encoded1+'/workflow/',
        '/rowers/workout/fusion/'+encoded1+'/',
#        '/rowers/workout/fusion/'+encoded1+'/2016-01-01/2016-12-31/',
        '/rowers/workout/upload/',
        '/rowers/workout/upload/team/',
        '/rowers/workouts-join/',
        '/rowers/workouts-join-select/',
#        '/rowers/workouts-join-select/2016-01-01/2016-12-31/',
    ]

    #    urlstotest = ['/rowers/createplan/user/1/']

    lijst = []
    for url in urlstotest:
        lijst.append(
            (url,200)
        )


    @parameterized.expand(lijst)
    @patch('rowers.dataprep.create_engine')
    @patch('rowers.dataprep.read_df_sql')
    @patch('rowers.dataprep.getsmallrowdata_db')
    @patch('requests.get',side_effect=mocked_requests)
    @patch('requests.post',side_effect=mocked_requests)
    @patch('rowers.dataprep.get_video_data',side_effect=mocked_get_video_data)
    def test_url_generator(self,url,expected,
                           mocked_sqlalchemy,
                           mocked_read_df_sql,
                           mocked_getsmallrowdata_db,
                           mock_get,
                           mock_post,
                           mocked_get_video_data):

        if url not in tested:
            login = self.c.login(username='john',password='koeinsloot')
            self.assertTrue(login)
            response = self.c.get(url,follow=True)
            if response.status_code != expected:
                print(url )
                print(response.status_code)

            self.assertEqual(response.status_code,
                             expected)

            html = BeautifulSoup(response.content,'html.parser')


            urls = [a['href'] for a in html.find_all('a')]


            for u in urls:
                if u not in tested and 'rowers' in u and 'http' not in u and 'authorize' not in u and 'import' not in u and 'logout' not in u:
                    response2 = self.c.get(u)
                    if response2.status_code not in [200,302,301]:
                        print(len(tested))
                        print(response.templates[0].name)
                        print(url)
                        print(u)
                        print(response2.status_code)
                    tested.append(u)
                    self.assertIn(response2.status_code,
                                  [200,302,301])
                else:
                    tested.append(u)
