from django.shortcuts import render
from secondApp.forms import UserForm,UserProfileInfoForm

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
# Create your views here.
def index(request):
    #context_dict={'text':'hello','number':'100'}
    return render(request,'secondApp/index.html')
#def other(request):
#    return render(request,'secondApp/other.html')
#def relative(request):
#    return render(request,'secondApp/relative_urlTemps.html')
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 #one to one relationship
            if 'profile_pic' in request.FILES:
                profile.profile_pic=request.FILES['profile_pic']
            profile.save()

            registered=True
        else:
            print(user_form.errors, profile_form.errors)
    else:
        user_form=UserForm()
        profile_form=UserProfileInfoForm()

    return  render(request,'secondApp/register.html',{'user_form':user_form,'profile_form':profile_form,'registered':registered})
 #making login requiered:

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


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 HttpResponseRedirect(reverse('index'))
            else:
                return HttpResponse('ACCOUNT NOT ACTIVE')

        else:
            print('Failed login')
            return  HttpResponse('invalid login details supplied')
    else:
        return render(request,'secondApp/login.html',{})
