import django.contrib.auth
from django.contrib.auth import authenticate
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.views import View

from main.forms import UserCreationForm


# Create your views here.
def index(request):
    return render(request, 'main/index.html')


def servers(request):
    return render(request, 'main/servers.html')


def mychat(request):
    return render(request, 'main/mychat.html')


def workout(request):
    return render(request, 'main/workout.html')


def login(request):
    return render(request, 'registration/login.html')


def registration(request):
    return render(request, 'registration/signin.html')


def logout(request):
    pass


class Register(View):
    template_name = 'registration/signin.html'

    def get(self, request):
        context = {
            'form': UserCreationForm()
        }
        return render(request, self.template_name, context)

    def post(self, request):
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=password)
            django.contrib.auth.login(request, user)
            return redirect('home')
        context = {
            'form': form
        }
        return render(request, self.template_name, context)