from django.shortcuts import render, redirect
from django.contrib.auth.views import LoginView, LogoutView
from django.contrib.messages.views import SuccessMessageMixin
from django.contrib import messages 
from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login

def register(request):
    if request.user.is_authenticated:
        messages.info(request, "You are already logged in")
        return redirect('/')
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save() 
            username = form.cleaned_data.get('username')
            messages.success(request, f'Your account is created! You are now logged in!') 
            new_user = authenticate(username= form.cleaned_data['username'],
                                    password= form.cleaned_data['password1'])    
            login(request, new_user)  
            return redirect('blog-home')
    else:
        form = UserRegisterForm()
    return render(request, 'users/register.html', {'form': form})


class MyLoginView(SuccessMessageMixin, LoginView):
    template_name = 'users/login.html'
    success_url = 'blog-home'
    success_message = 'You are successfully logged in!'


class MyLogoutView(SuccessMessageMixin, LogoutView):
    template_name = 'users/logout.html'
    success_url = 'blog-home'
    
    def dispatch(self, request, *args, **kwargs): # mora ovako jer nije form view
        response = super().dispatch(request, *args, **kwargs)
        messages.add_message(request, messages.INFO, 'Successfully logged out.')
        return response


@login_required # samo za metode
def profile(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, f'Your account has been updated!')
            return redirect('profile') # mora redirekt da se ne okine post opet
    else:   
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)


    context = {
        'u_form': u_form,
        'p_form': p_form
    }
    
    return render(request, 'users/profile.html', context)

