import django as django
from django import setup
from django.views.generic import ListView

django.setup()
from main.models import VWorkoutTasksW1, VWorkoutTasksW2

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):
    if request.user.is_authenticated:
        tw1 = VWorkoutTasksW1.objects.filter(user=request.user.username)
        tw2 = VWorkoutTasksW2.objects.filter(user=request.user.username)
        data = {'tw1' : tw1, 'tw2': tw2}
        return render(request, 'main/workout.html', context=data)
    else:
        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('/')
        context = {
            'form': form
        }
        return render(request, self.template_name, context)

class tw1view(ListView):
    model = VWorkoutTasksW1
    template_name = 'main/workout.html'
    context_object_name = 'tw1'

class tw2view(ListView):
    model = VWorkoutTasksW2
    template_name = 'main/workout.html'
    context_object_name = 'tw2'