from django.shortcuts import render, get_object_or_404
from django.views import generic
from django.urls import reverse

from django.contrib.auth import logout, authenticate, login
from django.contrib.auth.models import User

from django.http import HttpResponse, HttpResponseRedirect

from rpgs.models import Rpg
from exec.models import ExecRole
from forum.models import Thread, Response

from .models import Member
from .forms import MemberForm, UserForm, LoginForm, SignupForm

#temp
import json

def viewmember(request, pk):
	if(pk == 'me'):
		# if we're logged in, get the user id and use that
		if(request.user.is_authenticated):
			pk = request.user.member.id
		else:
			# if we're not logged in then it's an inappropriate request
			return HttpResponseRedirect('/', status=400)

	member = get_object_or_404(Member, id=pk)

	execroles = ExecRole.objects.filter(incumbent__id=pk)

	# = ','.join(str()

	context = {
		# whether the viewed user is the logged in one
		'me': member.id == pk,
		'result': request.GET.get('result', None),
		'member': member,

		# activity info
		'recent_threads': Thread.objects.filter(author__id=pk).order_by('-pub_date')[:3],
		'recent_responses': Response.objects.filter(author__id=pk).order_by('-pub_date')[:3],
		'rpgs': Rpg.objects.filter(game_masters__id=pk),
		'execroles': execroles,
	}
	return render(request, 'users/view.html', context)

def edit(request):
	context = {
		'member': request.user.member,
		'result': request.GET.get('result', None),
		'userform': UserForm(instance=request.user),
		'memberform': MemberForm(instance=request.user.member),
	}
	return render(request, 'users/edit.html', context)

def update(request):
	if(request.method == 'POST'):
		# generate a filled form from the post request
		memberform = MemberForm(request.POST, instance=request.user.member)
		userform = UserForm(request.POST, instance=request.user)
		if(memberform.is_valid() and userform.is_valid()):
			memberform.save()
			userform.save()
			res = HttpResponseRedirect(reverse('me') + '?result=success')
			return res
		else:
			return HttpResponseRedirect(reverse('edit') + '?result=invalid')

	else:
		return HttpResponse('GET !')

# view for the login form
def login_view(request):
	form = LoginForm()
	context = {'form': form, 'result': request.GET.get('result'), 'next': request.GET.get('next')}
	return render(request, 'users/login.html', context)

# actually logs the user in
def login_process(request):
	username = request.POST.get('username')
	password = request.POST.get('password')
	user = authenticate(username=username, password=password)
	if user is not None:
		login(request, user)
		return HttpResponseRedirect(request.GET.get('next') or '/')
	else:
		return HttpResponseRedirect(reverse('login') + '?result=invalid')

def signup_view(request):
	form = SignupForm()
	context = {'form': form, 'result': request.GET.get('result')}
	return render(request, 'users/signup.html', context)

def signup_process(request):
	form = SignupForm(request.POST)
	if(form.is_valid()):

		u = User.objects.create_user(form.cleaned_data['username'], form.cleaned_data['email'], form.cleaned_data['password'])
		m = Member.objects.create(equiv_user=u)
		u.save()
		auth = authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password'])
		if auth is not None:
			login(request, auth)
			return HttpResponseRedirect(reverse('me'))
		else:
			return HttpResponse('auth after signup failed to login')
	else:
		# TODO: Proper error lol
		return HttpResponse('form not valid')

def logout_view(request):
	logout(request)
	return HttpResponseRedirect('/')

def spawn_user(username, email, password):
	u = User.objects.create_user(username, email, password)
	Member.objects.create(equiv_user=u)
	u.save()
	return u


@@ -3019,16 +3019,17 @@
 ()):%0A%0A%09%09
+#
 u = User
@@ -3141,16 +3141,17 @@
 rd'%5D)%0A%09%09
+#
 m = Memb
@@ -3184,16 +3184,17 @@
 er=u)%0A%09%09
+#
 u.save()
@@ -3194,16 +3194,125 @@
 .save()%0A
+%0A%09%09u = spawn_user(form.cleaned_data%5B'username'%5D, form.cleaned_data%5B'email'%5D, form.cleaned_data%5B'password'%5D)%0A%0A
 %09%09auth =
@@ -3876,9 +3876,8 @@
 eturn u%0A
-%0A

remove a newline I think git diff on my system sucks