from .forms import ContactForm
from django.contrib import messages
from django.core.mail import send_mail
import datetime
from django.conf import settings


# Create your views here.
def current_year(request):
    current_datetime = datetime.datetime.now()
    return {
        'current_year': current_datetime.year
    }


def contact(request):
    # if this is a POST request we need to process the form data
    if request.method == "POST":
        # create a form instance and populate it with data from the request:
        form = ContactForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            name = form.cleaned_data["name"]
            mail = form.cleaned_data["mail"]
            subject = form.cleaned_data["subject"]
            body = form.cleaned_data["body"]

            recipients = [settings.EMAIL_HOST_USER]
            body = ('Nom de l\'expéditeur :' + '\n' + name + '\n\n' + 'Objet :' + '\n' + subject + '\n\n' + 'Message :'
                    + '\n' + body)
            send_mail(subject, body, mail, recipients)
            messages.success(request, 'Votre message a bien été envoyé !')
            form = ContactForm()
            return {"form": form}

    # if a GET (or any other method) we'll create a blank form
    else:
        form = ContactForm()
    return {"form": form}
