from django.contrib.auth import logout, login, authenticate
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render
from django.template import RequestContext
from website.models import *
from website.forms import UserForm, ProductForm, CustomerForm



# Create your views here.
def register(request):
    '''Handles the creation of a new user for authentication

    Method arguments:
      request -- The full HTTP request object
    '''

    # A boolean value for telling the template whether the registration was successful.
    # Set to False initially. Code changes value to True when registration succeeds.
    registered = False

    # Create a new user as well as a new customer at the same time
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        customer_form = CustomerForm(data=request.POST)
        confirm_password = request.POST['confirm_password']
        newPassword = request.POST.get('password')
        if user_form.is_valid() and customer_form.is_valid():
            if newPassword == confirm_password:
                user = user_form.save()
                customer = customer_form.save()
                customer.user = user
                user.set_password(user.password)
                user.save()
                customer.save()
                return login_user(request)
            else:

                # print("user form", newPassword)
                # print("confirm", confirm_password)
                print("passwords don't match")
                template_name = 'error.html'
                return render(request, template_name)
        else:
            print("not valid user form")


    elif request.method == 'GET':
        user_form = UserForm()
        customer_form = CustomerForm()
        template_name = 'register.html'
        return render(request, template_name, {'user_form': user_form, 'customer_form': customer_form})


def login_user(request):
    '''Handles the creation of a new user for authentication

    Method arguments:
      request -- The full HTTP request object
    '''

    # Obtain the context for the user's request.
    context = RequestContext(request)

    # If the request is a HTTP POST, try to pull out the relevant information.
    if request.method == 'POST':

        # Use the built-in authenticate method to verify
        username=request.POST['username']
        password=request.POST['password']
        authenticated_user = authenticate(username=username, password=password)

        # If authentication was successful, log the user in
        if authenticated_user is not None:
            login(request=request, user=authenticated_user)
            return HttpResponseRedirect('/')

        else:
            # Bad login details were provided. So we can't log the user in.
            print("Invalid login details: {}, {}".format(username, password))
            return HttpResponse("Invalid login details supplied.")


    return render(request, 'login.html', {}, context)

# Use the login_required() decorator to ensure only those logged in can access the view.
@login_required
def user_logout(request):
    # Since we know the user is logged in, we can now just log them out.
    logout(request)

    # Take the user back to the homepage. Is there a way to not hard code
    # in the URL in redirects?????
    return HttpResponseRedirect('/')
