from django.shortcuts import render
from custom_robo_app.forms import LoginForm, SignupForm
from django.contrib.auth import login, logout, authenticate
from django.shortcuts import render,HttpResponseRedirect, reverse, HttpResponse
from custom_robo_app.models import CustomRobo
# Create your views here.
def index_view(request):
    return render(request, 'index.html')
    

def login_view(request):
    if request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            user= authenticate(request,username=data.get("username"),password=data.get("password"))
            if user:
                login(request, user)
                return HttpResponseRedirect(request.GET.get('next',reverse("homepage")))

    form = LoginForm()
    return render(request, "form.html", {"form": form})
    


def signup_view(request):
    if request.method == "POST":
        form = SignupForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            new_user = CustomRobo.objects.create_user(username=data.get("username"),password=data.get("password"))
            
            login(request,new_user)
            return HttpResponseRedirect(reverse("homepage"))
    form = SignupForm()
    return render(request,"form.html",{"form":form})    
        
def logout_view(request):
    logout(request)
    return HttpResponseRedirect(reverse("homepage"))