from .models import UserProfileInfo
from django.shortcuts import render,redirect
from accounts.forms import UserForm,UserProfileInfoForm,UserUpdateForm, ProfileUpdateForm
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from django.contrib.auth.decorators import login_required
from django.contrib import messages

def index(request):
    if request.user.is_authenticated:
        return redirect('survey:dashboard')
    else:
        return render(request, 'accounts/index.html') 

@login_required
def special(request):
    return HttpResponse("You are logged in !")

@login_required
def user_logout(request):
    logout(request)
    return redirect('index')

@login_required
def profile_view(request):
    if request.method == 'POST':
        user_form=UserUpdateForm(request.POST, instance=request.user)
        profile_form=ProfileUpdateForm(request.POST,request.FILES, instance=request.user.userprofileinfo)

        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile = profile_form.save(commit=False)
            user = user_form.save()
            user.email=profile.email
            user.save()
            profile.save()
            messages.success(request, 'Your profile is successfully updated!')
            return redirect('profile')
        else:
            messages.error(request,user_form.errors)
            messages.error(request,profile_form.errors)
    else:
        user_form=UserUpdateForm(instance=request.user)
        profile_form=ProfileUpdateForm(instance=request.user.userprofileinfo)

    return render(request, 'accounts/profile.html', {'user_form':user_form, 'profile_form':profile_form})

def register(request):
    registered = False
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileInfoForm(data=request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            if 'profile_pic' in request.FILES:
                print('found it')
                profile.profile_pic = request.FILES['profile_pic']
            profile.save()
            user.email=profile.email
            user.save()
            registered = True
            return redirect('survey:privacy')

        else:
            messages.error(request,user_form.errors)
            messages.error(request,profile_form.errors)
    else:
        user_form = UserForm()
        profile_form = UserProfileInfoForm()
    return render(request,'accounts/registration.html',
                          {'user_form':user_form,
                           'profile_form':profile_form,
                           'registered':registered})



def user_login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=username, password=password)

        if user:
            if user.is_active:
                login(request,user)
                return redirect('survey:dashboard')
            else:
                return HttpResponse("Your account was inactive.")
        else:
            print("Someone tried to login and failed.")
            print("They used username: {} and password: {}".format(username,password))
            return HttpResponse("Invalid login details given")
    else:
        return render(request, 'accounts/login.html', {})



