from django.test import TestCase
from django.test import Client
from django.contrib.auth.models import User
from django.conf import settings
from django.http import HttpResponse
from django.core import mail

from .forms import ITForm, WorkingForm, OfficeForm, DummyForm, OffboardingForm

class LoginTestCase(TestCase):
    def setUp(self):
        self.client = Client()
        self.user = User.objects.create_user('vladimir', 'vladimir@reiherzehe.com', 'reiherzehe')
        self.client.login(username='vladimir', password='reiherzehe')
        self.response = self.client.get('/')

    def testLogin(self):
        self.assertContains(self.response, 'Du bist eingeloggt als vladimir@reiherzehe.com', status_code=200)

    def testDebugWarning(self):
        with self.settings(DEBUG=True):
            self.response = self.client.get('/') # we need to do it again with DEBUG = True
            self.assertContains(self.response, "WARNUNG! Test-MODUS aktiviert. Es werden keine Mails verschickt!", status_code=200)
        with self.settings(DEBUG=False) and self.settings(MAILTEST=False):
            self.response = self.client.get('/') # we need to do it again with DEBUG = False
            self.assertNotContains(self.response, "WARNUNG! Test-MODUS aktiviert. Es werden keine Mails verschickt!", status_code=200)

    def _postform(self, data, expected_form):
        '''helper function to manage the Wizzard'''
        response = self.client.post('/', data, follow=True)
        # print(type(response))
        self.assertEqual(200, self.response.status_code)
        if not type(response) == HttpResponse:
            if 'form' in response.context:
                self.assertFalse(response.context['form'].errors)
            else:
                raise "NO FORM FOUND"
            self.assertEqual(
                 type(response.context['wizard']['form']),
                 expected_form
            )
        return response

    def test_department(self):
        self.assertContains(self.response, 'Programme', status_code=200)
        self.assertContains(self.response, 'Kommunikation und Events', status_code=200)

    def test_wizzard_in(self):
        ''' this test goes through the whole onboarding process of the EvaFormView from start to end '''

        self.assertEqual(200, self.response.status_code)

        response = self._postform({
                    'eva_form_view-current_step': '0',
                    '0-firstname': 'Ara',
                    '0-lastname': 'Seva',
                    '0-department': 'CENT',
                    '0-choice': 'IN'
                }, WorkingForm)

        response = self._postform({
                    'eva_form_view-current_step': '1',
                    '1-firstdate_employment': '2021-01-01',
                    '1-firstdate_presence': '2021-01-01',
                    '1-jobdescription_german': 'hau drauf',
                    '1-jobdescription_english': 'und schluss',
                    '1-works_in_gs': False
                }, ITForm)

        response = self._postform({
                    'eva_form_view-current_step': '2',
                    '2-vendor': 'STANDARD',
                    '2-os': 'UBU',
                    '2-keyboard': 'DE',
                    '2-language': 'GER'
                }, OfficeForm)

        response = self._postform({
                    'eva_form_view-current_step': '3',
                    '3-transponder': 'NORM'
                    }, DummyForm)

        response = self._postform({
                    'eva_form_view-current_step': '5',
        }, DummyForm)

    def test_wizzard_out(self):
        ''' this test goes through the whole offboarding process of the EvaFormView from start to end '''

        self.assertEqual(200, self.response.status_code)

        response = self._postform({
                    'eva_form_view-current_step': '0',
                    '0-firstname': 'Ara',
                    '0-lastname': 'Seva',
                    '0-department': 'CENT',
                    '0-choice': 'OUT'
                }, OffboardingForm)

        print()
        print('ACHTUNG ACHTUNG')
        print()

        response = self._postform({
                    'eva_form_view-current_step': '1',
                    '1-firstdate_employment': '2021-01-01',
                    '1-firstdate_presence': '2021-01-01',
                    '1-jobdescription_german': 'hau drauf',
                    '1-jobdescription_english': 'und schluss',
                    '1-works_in_gs': False
                }, DummyForm)

        response = self._postform({
                    'eva_form_view-current_step': '5',
        }, DummyForm)


    def test_mail(self):
        self.test_wizzard_in()
        # print(mail.outbox)
        self.assertGreater(len(mail.outbox), 2)
        self.assertIn("Ara Seva", mail.outbox[0].subject)

class NoLoginTestCase(TestCase):
    def setUp(self):
        self.client = Client()

    def test_details(self):
        response = self.client.get('/')
        self.assertEqual(response.status_code, 302)
        response2 = self.client.get(response.url)
        self.assertContains( response2, 'Bitte via Wolke einloggen:', status_code=200)

class ITFORMTestCase(TestCase):
    def test_mac(self):
        form = ITForm(data={"vendor": 'MAC', 'os': 'UBU'})
        #print (form.errors)
        self.assertEqual(form.non_field_errors(), ['Ein MAC sollte Mac OS installiert haben'])

    def test_ubu(self):
        form = ITForm(data={"vendor": 'STANDARD', 'os': 'UBU'})
        #print (form.errors)
        self.assertNotEqual(form.non_field_errors(), ['Ein MAC sollte Mac OS installiert haben'])