import re

from django.test import TestCase, Client
from django.urls import reverse
from django.contrib.auth.models import User
from django.utils import timezone


from lostplaces.models import Place

from lostplaces.tests.views import (
    ViewTestCase,
    GlobalTemplateTestCaseMixin
)

class TestHomeView(GlobalTemplateTestCaseMixin, ViewTestCase):
    @classmethod
    def setUpTestData(cls):
        user = User.objects.create_user(
            username='testpeter',
            password='Develop123'
        )

        place = Place.objects.create(
            name='Im a place',
            submitted_when=timezone.now(),
            submitted_by=user.explorer,
            location='Testtown',
            latitude=50.5,
            longitude=7.0,
            description='This is just a test, do not worry'
        )
        place.tags.add('I a tag', 'testlocation')
        place.save()
        
        # Creating a place with level one to test against
        # unauth's users and users with level 1
        Place.objects.create(
            name='Im a place level 1',
            submitted_when=timezone.now(),
            submitted_by=user.explorer,
            location='Testtown',
            latitude=50.5,
            longitude=7.0,
            description='This is just a test, do not worry',
            level=1
        )

        # Creating a place with level two to test against
        # unauth's users and users above level 1
        Place.objects.create(
            name='Im a place level 2',
            submitted_when=timezone.now(),
            submitted_by=user.explorer,
            location='Testtown',
            latitude=50.5,
            longitude=7.0,
            description='This is just a test, do not worry',
            level=2
        )

    def setUp(self):
        self.client = Client()
        
    def test_global(self):
        response = self.client.get(reverse('lostplaces_home'))
        self.assertGlobal(response)
        
    def test_place_list_authenticated(self):
        """
        Testing there is the place list containing the name,
        location and description of the latest place for 
        authenticated users.
        """
        self.client.login(username='testpeter', password='Develop123')
        response = self.client.get(reverse('lostplaces_home'))
        self.assertNotEqual(
            None,
            re.search(
                """Im a place.*Testtown.*This is just a test, do not worry""",
                response.content.decode().replace('\n', '')
            ),
            msg='Expecting the test place to show up on the homepage'
        )
        
    def test_place_list_unauthenticated(self):
        """
        Testing there is a place list of the lates places
        containing the place names only for unauthenticated users.
        """
        response = self.client.get(reverse('lostplaces_home'))
        self.assertNotEqual(
            None,
            re.search(
                """Im a place""",
                response.content.decode().replace('\n', '')
            ),
            msg='Expecting the test place to show up on the homepage'
        )
        self.assertEqual(
            None,
            re.search(
               """Testtown.*This is just a test, do not worry""",
                response.content.decode().replace('\n', '')
            ),
            msg='Expecting the test place to show up on the homepage'
        )

        print(response.content.decode().replace('\n', ''))
        self.assertNotEqual(
            None,
            re.search(
                """Im a place level 1""",
                response.content.decode().replace('\n', '')
            ),
            msg="Expecting the level 1 places to show up on the homepage publicly"
        )

        self.assertEqual(
            None,
            re.search(
                """Im a place level 2""",
                response.content.decode().replace('\n', '')
            ),
            msg="Expecting the level 2 places to *not* show up on the homepage publicly"
        )

    def test_map_authenticated(self):
        """
        Testing there is a map showing all the lates places
        on a map for authenticated users.
        """
        self.client.login(username='testpeter', password='Develop123')
        response = self.client.get(reverse('lostplaces_home'))
        self.assertNotEqual(
            None,
            re.search(
                """map.*7.0,.*50.5.*Im a place""",
                response.content.decode().replace('\n', '')
            ),
            msg='Expecting the test place to show up on the map'
        )
        
    def test_map_unauthenticated(self):
        """
        Testing there is no map for unauthenticated users.
        """
        response = self.client.get(reverse('lostplaces_home'))
        self.assertEqual(
            None,
            re.search(
                """7.0,.*50.5.*Im a place""",
                response.content.decode().replace('\n', '')
            ),
            msg='Expecting the test place to show up on the map'
        )