from datetime import date
from smtplib import SMTPException
from django.shortcuts import render
from django.forms import modelformset_factory
from django.http import HttpResponse
from formtools.wizard.views import CookieWizardView
from django.core.mail import send_mail, BadHeaderError, EmailMultiAlternatives
from django.conf import settings
from django.template.loader import get_template
from django.template import Context
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.utils.html import format_html
from django.utils.translation import gettext as _
from .forms import ProjectForm, ExternForm, LibraryForm, IFGForm, LiteratureForm,\
                   HonoraryCertificateForm, InternForm, TravelForm, EmailForm,\
                   ListForm, BusinessCardForm, INTERN_CHOICES
from .models import Project, TYPE_CHOICES, Library, Literature, Travel, IFG, BusinessCard, Email, List
def auth_deny(choice,pk,auth):
    if choice in ('BIB', 'ELIT', 'SOFT'):
        Library.set_granted(pk,auth)
    elif choice == 'LIT':
        Literature.set_granted(pk,auth)
    elif choice == 'IFG':
        IFG.set_granted(pk,auth)
    elif choice == 'TRAV':
        Travel.set_granted(pk,auth)
    elif choice == 'VIS':
        BusinessCard.set_granted(pk,auth)
    elif choice == 'MAIL':
        Email.set_granted(pk,auth)
    elif choice == 'LIST':
        List.set_granted(pk,auth)
    else:
        return HttpResponse(f'ERROR! UNKNOWN CHOICE TYPE! {choice}')
    return False
@login_required
def export(request):
    '''export the project database to a csv'''
    return HttpResponse('WE WANT CSV!')
@login_required
def authorize(request, choice, pk):
    '''If IF grant a support they click a link in a mail which leads here.
    We write the granted field in the database here and set a timestamp.'''
    ret = auth_deny(choice, pk, True)
    if ret:
        return ret
    else:
        return HttpResponse(f"AUTHORIZED! choice: {choice}, pk: {pk}")
@login_required
def deny(request, choice, pk):
    '''If IF denies a support they click a link in a mail which leads here
    We write the granted field in the database here.'''
    ret = auth_deny(choice, pk, False)
    if ret:
        return ret
    else:
        return HttpResponse(f"DENIED! choice: {choice}, pk: {pk}")
def done(request):
    return HttpResponse("Deine Anfrage wurde gesendet. Du erhältst in Kürze eine E-Mail-Benachrichtigung mit deinen Angaben. Für alle Fragen kontaktiere bitte das Team Communitys und Engagement unter community@wikimedia.de.")
def index(request):
    return render(request, 'input/index.html')
class InternView(LoginRequiredMixin, CookieWizardView):
    '''This View is for WMDE-employees only'''
    template_name = 'input/extern.html'
    form_list = [InternForm, ProjectForm]
    def get_form(self, step=None, data=None, files=None):
        '''this function determines which part of the multipart form is
        displayed next'''
        if step is None:
            step = self.steps.current
        print ("get_form() step " + step)
        if step == '1':
            prev_data = self.get_cleaned_data_for_step('0')
            choice = prev_data.get('choice')
            print(f'choice detection: {INTERN_CHOICES[choice]}')
            if choice == 'HON':
                form = HonoraryCertificateForm(data)
            elif choice == 'PRO':
                form = ProjectForm(data)
            elif choice == 'TRAV':
                form = TravelForm(data)
            else:
                raise RuntimeError(f'ERROR! UNKNOWN FORMTYPE {choice} in InternView')
            self.choice = choice
        else:
            form = super().get_form(step, data, files)
            form.fields['realname'].help_text = format_html("Vor- und Zuname (Realname), Wer hat das Projekt beantragt?
\
                                                            Wer ist Hauptansprechperson? Bei WMDE-MAs immer „(WMDE)“,
\
                                                            bei externen Partnern „(PART)“ hinzufügen.")
        return form
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        if hasattr(self, 'choice'):
            context["choice"] = INTERN_CHOICES[self.choice]
        return context
    def done(self, form_list, **kwargs):
        print('InternView.done() reached')
        # gather data from all forms
        data = {}
        for form in form_list:
            data = {**data, **form.cleaned_data}
        if data['choice'] == 'LIT':
            if data['selfbuy'] == 'TRUE':
                data['selfbuy_give_data'] = 'False'
        # write data to database
        form = form.save(commit=False)
        # we have to copy the data from the first form here
        # this is ugly code. how can we copy this without explicit writing?
        # i found no way to access the ModelForm.Meta.exclude-tupel
        form.realname = data['realname']
        # form.username = data['username']
        form.email = data['email']
        form.granted = True
        form.granted_date = date.today()
        if data['choice'] == 'LIT':
            form.selfbuy_give_data = data['selfbuy_give_data']
        form.save()
        return done(self.request)
# these where used as labels in the second form TYPE_CHOICES is used for the first form and the
# text above the second form. only used for BIB, SOFT, ELIT in the moment
LABEL_CHOICES = {'BIB': format_html('Bibliothek'),
           'ELIT': format_html('Datenbank/Online-Ressource'),
           'MAIL': format_html('E-Mail-Adresse'),
           'IFG': format_html('Kostenübernahme IFG-Anfrage'),
           'LIT': format_html('Literaturstipendium'),
           'LIST': format_html('Mailingliste'),
           'TRAV': format_html('Reisekosten'),
           'SOFT': format_html('Software'),
           'VIS': format_html('Visitenkarten'),
           }
HELP_CHOICES = {'BIB': format_html("In welchem Zeitraum möchtest du recherchieren oder
wie lange ist der Bibliotheksausweis gültig?"),
                'ELIT': "Wie lange gilt der Zugang?",
                'SOFT': "Wie lange gilt die Lizenz?",
                }
class ExternView(CookieWizardView):
    '''This View is for Volunteers'''
    template_name = "input/extern.html"
    form_list = [ExternForm, LibraryForm]
    def get_form(self, step=None, data=None, files=None):
        '''this function determines which part of the multipart form is
        displayed next'''
        if step is None:
            step = self.steps.current
        print ("get_form() step " + step)
        if step == '1':
            prev_data = self.get_cleaned_data_for_step('0')
            choice = prev_data.get('choice')
            print(f'choice detection in ExternView: {TYPE_CHOICES[choice]}')
            if choice == 'IFG':
                form = IFGForm(data)
                form.fields['notes'].help_text = format_html("Bitte gib an, wie die gewonnenen Informationen den
Wikimedia-Projekten zugute kommen sollen.")
            elif choice in ('BIB', 'SOFT', 'ELIT'):
                form = LibraryForm(data)
                form.fields['library'].label = LABEL_CHOICES[choice]
                form.fields['library'].help_text = f"Für welche {LABEL_CHOICES[choice]} gilt das Stipendium?"
                form.fields['duration'].help_text = HELP_CHOICES[choice]
            elif choice == 'MAIL':
                form = EmailForm(data)
                form.fields['domain'].help_text = format_html("Mit welcher Domain, bzw. für welches Wikimedia-Projekt,
möchtest du eine Mailadresse beantragen?")
            elif choice == 'LIT':
                form = LiteratureForm(data)
                form.fields['notes'].help_text = "Bitte gib an, wofür du die Literatur verwenden möchtest."
            elif choice == 'VIS':
                form = BusinessCardForm(data)
            elif choice == 'LIST':
                form = ListForm(data)
                form.fields['domain'].help_text = format_html("Mit welcher Domain, bzw. für welches Wikimedia-Projekt,
möchtest du eine Mailingliste beantragen?")
            elif choice == 'TRAV':
                form = TravelForm(data)
            else:
                raise RuntimeError(f'ERROR! UNKNOWN FORMTYPE {choice} in ExternView')
            self.choice = choice
        else:
            form = super().get_form(step, data, files)
        return form
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        if hasattr(self, 'choice'):
            context["choice"] = TYPE_CHOICES[self.choice]
        return context
    def done(self, form_list, **kwargs):
        print('ExternView.done() reached')
        # gather data from all forms
        data = {}
        for form in form_list:
            data = {**data, **form.cleaned_data}
        if data['choice'] == 'LIT':
            if data['selfbuy'] == 'TRUE':
                data['selfbuy_give_data'] = 'False'
        # write data to database
        modell = form.save(commit=False)
        # we have to copy the data from the first form here
        # this is a bit ugly code. can we copy this without explicit writing?
        if data['choice'] == 'LIT':
            modell.selfbuy_give_data = data['selfbuy_give_data']
        if user := self.request.session.get('user'):
            modell.username = user['username']
        modell.realname = data['realname']
        modell.email = data['email']
        # write type of form in some cases
        if data['choice'] in ('BIB', 'ELIT', 'SOFT'):
            modell.type = data['choice']
        form.save()
        # add some data to context for mail templates
        data['pk'] = modell.pk
        data['url_prefix'] = settings.EMAIL_URL_PREFIX
        data['grant'] = ('LIT', 'SOFT', 'ELIT', 'BIB', 'IFG')
        data['DOMAIN'] = ('MAIL', 'LIST')
        data['typestring'] = TYPE_CHOICES[data['choice']]
        # we need to send the following mails here:
        context = { 'data': data }
        try:
            #  - mail with entered data to the Volunteer
            txt_mail_template1 = get_template('input/ifg_volunteer_mail.txt')
            html_mail_template1 = get_template('input/ifg_volunteer_mail.html')
            subject1, from_email1, to1 = 'Formular ausgefüllt', settings.IF_EMAIL, data['email']
            text_content1 = txt_mail_template1.render(context)
            html_content1 = html_mail_template1.render(context)
            msg1 = EmailMultiAlternatives(subject1, text_content1, from_email1, [to1])
            msg1.attach_alternative(html_content1, "text/html")
            msg1.send()
            #print('ifg volunteer mail would have been sent')
            #send_mail(
            #    'Formular ausgefüllt',
            #    txt_mail_template1.render(context),
            #    IF_EMAIL,
            #    [data['email']],
            #    fail_silently=False)
            ##  - mail to IF with link to accept/decline
            txt_mail_template = get_template('input/if_mail.txt')
            html_mail_template = get_template('input/if_mail.html')
            subject, from_email, to = 'Formular ausgefüllt', settings.IF_EMAIL, settings.IF_EMAIL
            text_content = txt_mail_template.render(context)
            html_content = html_mail_template.render(context)
            msg2 = EmailMultiAlternatives(subject, text_content, from_email, [to])
            msg2.attach_alternative(html_content, "text/html")
            msg2.send()
            #print('if mail would have been sent')
            #send_mail(
            #    'Formular ausgefüllt',
            #    txt_mail_template.render(context),
            #    IF_EMAIL,
            #    [IF_EMAIL],
            #    fail_silently=False)
            ## raise SMTPException("testing pupose only")
        except BadHeaderError:
            modell.delete()
            return HttpResponse('Invalid header found. Data not saved!')
        except SMTPException:
            modell.delete()
            return HttpResponse('Error in sending mails (probably wrong adress?). Data not saved!')
        return done(self.request)