from smtplib import SMTPException
import collections

from django.views.generic.edit import CreateView
from django.urls import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.core.mail import send_mail, BadHeaderError
from django.template.loader import get_template
from formtools.wizard.views import CookieWizardView
from django.shortcuts import render
from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin

from .models import Employee, DEPARTMENT_CHOICES, LAPTOP_CHOICES, OS_CHOICES, VENDOR_CHOICES, \
                    MOBILE_CHOICES, LANG_CHOICES, ACCOUNT_CHOICES, TRANSPONDER_CHOICES
from .forms import PersonalForm, WorkingForm, ITForm, OfficeForm, DummyForm,\
                   ChangeForm, TYPE_CHOICES
from .settings import MAILS, EVA_MAIL, BASIC_DATA, ONLY_ONBOARDING

def success(request):
    return HttpResponse("Vielen Dank! Du hast E.V.A. erfolgreich ausgefüllt. Die Mails an die Abteilungen wurden versendet.")

def long_process(wizard):
    '''this method is called via urls.py to determine if a form is part of the IN-Process'''

    if ONLY_ONBOARDING:
        wizard.set_choice('IN')
        return True
    else:
        data = wizard.get_cleaned_data_for_step('0') or {}
        print(data)
        if data.get('choice') != 'CHANGE':
            wizard.set_choice('IN')
            print('PROZESS IN')
            return True
        else:
            wizard.set_choice('CHANGE')
            print('PROZESS NOT IN')
            return False

def change_process(wizard):
    ''' this method is called via urls.py to determine if the form is part of the change process'''
    print('CHANGE PROZESS')
    return not long_process(wizard)


class EvaFormView(LoginRequiredMixin, CookieWizardView):
    template_name = 'evapp/employee_form.html'
    form_list = [PersonalForm, WorkingForm, ITForm, OfficeForm, ChangeForm, DummyForm]
    instance = None
    choice = 'IN'

    # maybe we dont need this, if *_process() would be class methods,
    # but unsure if this would work fine with the entries in urls.py
    def set_choice(self, c):
        self.choice = c


    def get_all_cleaned_data(self):
        '''this method deletes data which is only used temporary and is not in the modell,
        it also changes the mail adress of the employee in some circumstances'''

        data = super().get_all_cleaned_data()
        if not data['intern']:
                print("intern employee detected")
                if not data['email'].endswith('_ext'):
                    print('added "_ext" to mail adress')
                    data['email'] += '_ext'

        print("delete CHOICE FROM DATA")
        if 'choice' in data:
            del data['choice']
        return data


    def get_context_data(self, form, **kwargs):
        '''this method is called to give context data to the template'''

        #print('GETCONTEXT')
        context = super().get_context_data(form=form, **kwargs)
        context.update({'choice': self.choice,
                        'choice_string': TYPE_CHOICES[self.choice]})

        # deliver context for forms if we are in the last step
        if (self.steps.step1 == 5 or (self.choice != 'IN' and self.steps.step1 == 3)):
            context.update({'data': self.beautify_data(self.get_all_cleaned_data()),
                            'datatable': True,})
        return context

    def get_form_instance(self,step):
        ''' this method makes shure, that we use the same model instance for all steps'''

        if self.instance == None:
            self.instance = Employee()
        return self.instance


    def done(self, form_list, **kwargs):
        '''this method is called from CookieWizardView after all forms are filled'''

        print ('INSTANCE_DICT')
        print(self.instance_dict)

        # save data to database
        for form in form_list:
            form.save()

        # send data to departments
        for dep in MAILS:
            self.send_mail_to_department(dep)

        if not settings.DEBUG:
            self.instance.delete()

        return HttpResponseRedirect('success')


    def send_mail_to_department(self, department):
        'send a mail to the given department with the nececcary notifications'

        print(f'send mail to department {department}...')

        data = self.get_all_cleaned_data()
        # some data should be in every mail
        newdata = {k: v for k, v in data.items() if (k in BASIC_DATA)}
        # only the relevant data should be in the context
        newdata.update({k: v for k, v in data.items() if (k in MAILS[department]['DATA'])})

        context = {'data': self.beautify_data(newdata)}

        try:
             mail_template = get_template(f'evapp/department_mail.txt')
             if settings.MAILTEST:
                 send_mail(
                 'EVA: Neuzugang',
                 mail_template.render(context),
                 EVA_MAIL,
                 [EVA_MAIL, self.instance.usermail],
                 fail_silently=False)
             else:
                 send_mail(
                 'EVA: Neuzugang',
                 mail_template.render(context),
                 EVA_MAIL,
                 [MAILS[department]['MAIL'], self.instance.usermail],
                 fail_silently=False)
        except BadHeaderError:
            self.instance.delete()
            return HttpResponse('Invalid header found. Data not saved!')
        except SMTPException:
            self.instance.delete()
            return HttpResponse('Error in sending mails (propably wrong adress?). Data not saved!')

    def beautify_data(self, data):
        '''  # use long form for contextdata instead of short form if available
            #
            # ATTENTION!
            # This implementation works only for unique keys over all of these dicts from model.py
            #
        '''

        # print("BEAUTIFY")

        # update values in data dictionary with keys from *_CHOICES if present there
        choices = {**DEPARTMENT_CHOICES, **LAPTOP_CHOICES, **TRANSPONDER_CHOICES,
                   **OS_CHOICES, **MOBILE_CHOICES, **LANG_CHOICES, **VENDOR_CHOICES}
        data.update({k:choices[v] for k,v in data.items() \
            if isinstance(v,collections.abc.Hashable) \
            and v in choices})

        # replace values in accounts array from *_CHOICES
        if 'accounts' in data:
            data['accounts'] = [ACCOUNT_CHOICES[c] for c in data['accounts']]

        # replace keys in data dictionary with verbose_name
        newdata = {self.instance._meta.get_field(k).verbose_name.title() : v for k,v in data.items()}

        return newdata