from django.template import RequestContext
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse

from shoppy.contact.forms import AddContactForm
from shoppy.contact.models import Contact

@login_required
def showAllContacts(request):
    """
    shows all active contacts (delivery/billing contacts) for
    the specified (current) user.
    """
    user=request.user
    try:
        contact_list = Contact.objects.all().filter(user=user,isActive=True)
    except:
        contact_list = None

    return render_to_response('contact/contact_list.html', {
                'contact_list': contact_list,
            },context_instance=RequestContext(request))

@login_required
def contact(request, contact_id=0):
    """
    Shows a form to add a new or edit an existing contact.
    """
    template = None

    if request.method == 'POST':
        template = request.POST['next']
        form = AddContactForm(request.POST)
        if form.is_valid():
            if int(contact_id) > 0:
                c = Contact.objects.get(id=contact_id,user=request.user,isActive=True)
                c.isActive = False
                c.save()

            contact = form.save(request)
            
            return HttpResponseRedirect(template)
    else:
        form = AddContactForm()
        if int(contact_id) > 0:
            try:
                c = Contact.objects.get(id=contact_id,user=request.user,isActive=True)
                init_data = {}
                init_data['title'] = c.form.id
                init_data['name'] = c.name
                init_data['streetandnr'] = c.streetandnr
                init_data['zip'] = c.zip
                init_data['city'] = c.city
                init_data['country'] = c.country.id
                init_data['phone'] = c.phone
                form = AddContactForm(initial=init_data)
            except:
                pass

    try:
        template = request.GET['next']
    except:
        pass
    if not template:
        template = reverse('shoppy_contact_list')

    return render_to_response('contact/contact_form.html', {
                'form': form,
                'next': template,
            },context_instance=RequestContext(request))

@login_required
def contactDelete(request, contact_id):
    """
    Gives an user the option to remove a contact from his addressbook.
    Because of history we can't allow a user to delete the contacts from
    database. The contact is deactivated/removed by setting the value
    of isActive to false.
    """
    template = None
    try:
        template = request.GET['next']
    except:
        pass
    if not template:
        template = reverse('shoppy_contact_list')
    try:
        c = Contact.objects.get(id=contact_id,user=request.user)
        c.isActive = False
        c.save()
    except:
        pass
    return HttpResponseRedirect(template)
