"""
Copyright 2014-2016 University of Illinois

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from django.core.mail import send_mail
from django.core.urlresolvers import reverse_lazy, reverse

from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic import CreateView 
from django.views.generic import UpdateView 
from django.views.generic import TemplateView 
from django.contrib.auth.mixins import LoginRequiredMixin

from django.contrib.auth.models import User

from .models import Contact

from fae2.settings import EMAIL_HOST_USER
from fae2.settings import ADMIN_EMAIL

# Create your views here.
class ContactFormView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
    model = Contact
    fields = ['topic', 'message']
    template_name = 'contact/contact_form.html'

    success_url = reverse_lazy('contact_form')
    success_message = "Message on \"%(topic)s\" was sent succesfully sent"

    login_url = reverse_lazy('login')
    redirect_field_name = "Login"
    permission_denied_message = "You must login to contact the administrator"

    def form_valid(self, form):
        user = self.request.user
        form.instance.user = user

        message = "The following message was submitted to the FAE contact system" 
        message += "\n\nUser: " + user.first_name + " " + user.last_name
        message += "\nUsername: " + user.username
        message += "\nE-mail: " + user.email
        message += "\n\nTopic: " + form.instance.topic 
        message += "\n\nMessage:\n" + form.instance.message
        
        contact_topic = "FAE: " + form.instance.topic

        send_mail(contact_topic, message, EMAIL_HOST_USER, [ADMIN_EMAIL], fail_silently=False)

        return super(ContactFormView, self).form_valid(form)


# Create your views here.
class ResponseFormView(LoginRequiredMixin, UpdateView):
    model = Contact
    fields = ['topic', 'message', 'status', 'comments']
    template_name = 'contact/response_form.html'

    login_url = reverse_lazy('run_anonymous_report')
    redirect_field_name = "Anonymous Report"

    def form_valid(self, form):
        form.instance.user = self.request.user

        user = self.request.user
        form.instance.user = user

        message = "The following message was submitted to the FAE contact system" 
        message += "\n\nUser: "      + user.first_name + " " + user.last_name
        message += "\nUsername: "    + user.username
        message += "\nE-mail: "      + user.email
        message += "\n\nTopic: "     + form.instance.topic 
        message += "\n\nStatus:\n"   + form.instance.show_status()
        message += "\n\nMessage:\n"  + form.instance.message
        message += "\n\nResponse:\n" + form.instance.comments
        
        contact_topic = "FAE: " + form.instance.topic

        send_mail(contact_topic, message, EMAIL_HOST_USER, [ADMIN_EMAIL], fail_silently=False)

        return super(ResponseFormView, self).form_valid(form)

    def get_success_url(self):
        return reverse('responses', kwargs={})

    def get_context_data(self, **kwargs):

        context = super(ResponseFormView, self).get_context_data(**kwargs)
        context['action'] = reverse('response_form',
                                    kwargs={'pk': self.get_object().id})

        return context


class ResponsesView(LoginRequiredMixin, TemplateView):
    template_name = 'contact/responses.html'

    def get_context_data(self, **kwargs):
        context = super(ResponsesView, self).get_context_data(**kwargs)

        context['new_contacts']  = Contact.objects.filter(status='NR')
        context['old_contacts']  = Contact.objects.exclude(status='NR')
        
        return context            



@@ -2590,24 +2590,25 @@
  form):%0A
+%0A
         
 form.ins
@@ -2603,47 +2603,35 @@
     
-form.instance.user = self.request.user%0A
+contact = self.get_object()
 %0A   
@@ -2638,35 +2638,30 @@
      user = 
-self.reques
+contac
 t.user%0A     
@@ -3324,35 +3324,42 @@
 HOST_USER, %5B
-ADMIN_EMAIL
+contact.user.email
 %5D, fail_sile

update contact form response view to use correct email addresses