from django.contrib.auth import login, logout
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render, redirect
from django.views.generic import TemplateView

from users.forms import UserRegisterForm, UserLoginForm, PlayerUpdateForm, UserUpdateForm, BloonsaConfigUpdateForm
from users.models import Player, InviteCode
from users.util import bloonsa_util, actions


class LoginView(TemplateView):

    def get(self, request, *args, **kwargs):
        form = UserLoginForm()
        return render(request=request, template_name="users/login.html", context={"form": form})

    def post(self, request, *args, **kwargs):
        form = UserLoginForm(data=request.POST)
        if not form.is_valid():
            return render(request=request, template_name="users/login.html", context={"form": form})
        user = form.get_user()
        login(request=request, user=user)
        bloonsa_util.init_player(request=request)
        bloonsa_util.log(player=user.player,
                         action=actions.login)
        return redirect("bloonsa_game:game")


class RegisterView(TemplateView):

    def get(self, request, *args, **kwargs):
        form = UserRegisterForm()
        return render(request=request, template_name="users/register.html", context={"form": form})

    def post(self, request, *args, **kwargs):
        form = UserRegisterForm(request.POST)
        if not form.is_valid():
            return render(request=request, template_name="users/register.html", context={"form": form})

        invite_code = InviteCode.objects.get(code=form.cleaned_data["invite_code"])
        user = form.save()
        login(request=request, user=user)
        player = bloonsa_util.init_player(request=request)
        player.invite_code = invite_code
        player.save()

        bloonsa_util.log(player=player,
                         action=actions.login)
        return redirect("bloonsa_game:game")



class LogoutView(TemplateView):
    def get(self, request, *args, **kwargs):
        if request.user.is_authenticated:
            bloonsa_util.init_player(request=request)
            bloonsa_util.log(request=request,
                             action=actions.logout)
            logout(request)
        return redirect("bloonsa_game:game")


class ConfigView(LoginRequiredMixin, TemplateView):

    def get(self, request, *args, **kwargs):
        form_config = BloonsaConfigUpdateForm(instance=request.user.player.bloonsa_config)
        form_player = PlayerUpdateForm(instance=request.user.player)
        form_user = UserUpdateForm(instance=request.user)

        return render(request=request,
                      template_name="users/config.html",
                      context={"form_config": form_config,
                                "form_player": form_player,
                               "form_user": form_user})

    def post(self, request, *args, **kwargs):
        form_config = BloonsaConfigUpdateForm(request.POST,
                                              instance=request.user.player.bloonsa_config)
        form_player = PlayerUpdateForm(request.POST,
                                       request.FILES,
                                       instance=request.user.player)
        form_user = UserUpdateForm(request.POST, instance=request.user)
        """if not form_player.is_valid() or not form_user.is_valid() or not form_config.is_valid():
            return render(request=request,
                          template_name="users/config.html",
                          context={
                              "form_config": form_config,
                                "form_player": form_player,
                               "form_user": form_user})"""
        if form_user.is_valid():
            form_user.save()
        if form_player.is_valid():
            form_player.save()
        # TODO this form doesn't save for some reason
        if form_config.is_valid():
            form_config.save()

        bloonsa_util.log(action=actions.config,
                         player=request.user.player)
        # messages.success(request, "Profile updated successfully")
        return redirect("users:config")