from django.contrib.auth import login, authenticate, get_user_model
from django.shortcuts import render, redirect
from .form import ContactForm, LoginForm, RegisterForm
from django.contrib.auth import logout

User = get_user_model()


def home_page(request):
    context = {
        "title": "Don't wait just buy!",
        "content": "Welcome at home page"
    }

    return render(request, "home_page.html", context)


def contact_page(request):
    contact_form = ContactForm(request.POST or None)

    context = {
        "title": "test czy context dziala",
        "content": "Here you can contact with us",
        "contact_form": contact_form
    }

    if contact_form.is_valid():
        print(contact_form.cleaned_data)

    return render(request, "contact/contact_page.html", context)


def logout_page(request):
    logout(request)
    return render(request, "auth/logout.html")


def login_page(request):
    form = LoginForm(request.POST or None)
    context = {
        "form": form
    }
    print("User logged in:\n", request.user.is_authenticated())
    if form.is_valid():
        print(form.cleaned_data)
        username = form.cleaned_data.get("username")
        password = form.cleaned_data.get("password")
        # Handle authentication
        user = authenticate(request, username=username, password=password)
        print(request.user.is_authenticated)
        if user is not None:
            login(request, user)
            print(request.user.is_authenticated)
            # Redirect to a success page.
            # context["form"] = LoginForm
            return redirect("home")
        else:
            # Return an 'invalid login' error message.
            print("Error")

    return render(request, "auth/login.html", context)


def register_page(request):
    form = RegisterForm(request.POST or None)
    context = {
        "form": form
    }
    if form.is_valid():
        print(form.cleaned_data)
        username = form.cleaned_data.get("username")
        email = form.cleaned_data.get("email")
        password = form.cleaned_data.get("password")
        new_user = User.objects.create_user(username, email, password)
        print(new_user)
    return render(request, "auth/register.html", context)
