# Path: LL1_Academy/views/stats.py
# def log_start_grammar(gid):
# def log_complete_grammar(request):
# def log_skip_grammar(request):


import random
import ast
import json
from django.shortcuts import render
from django.http import JsonResponse, HttpRequest, HttpResponseRedirect, Http404, HttpResponseBadRequest
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.forms.models import model_to_dict
from LL1_Academy.views import stats 
from LL1_Academy.models import *
		uncompleted_grammars = Grammar.objects.all()
	else:
		completed_gids = UserHistory.objects.all().filter(user=user,complete=True).values_list('grammar',flat=True)
		uncompleted_grammars = Grammar.objects.exclude(gid__in=completed_gids)
	size = uncompleted_grammars.count()
	if size == 0:
		return None
	randid = random.randint(0,size-1)
	g=uncompleted_grammars[randid]
	return g

def practice(request):
	# on page load we start the session over

	#if 'gid' not in request.session or request.session['gid']==None
	#print(request.GET.get('gid') )

	if request.GET.get('gid') == None:
		random_grammar = get_random_grammar(request.user)
		if random_grammar == None:
			context = {
				"title": "You finished all our grammars!",
				"text": "Click on your username to visit your profile page. You will be able to continue practicing on grammars you have already completed or skipped."
			}
			return render(request, 'LL1_Academy/error.html', context)

		request.session['gid'] = random_grammar.gid
	else:
		request.session['gid'] = request.GET.get('gid')


# Path: LL1_Academy/views/stats.py
# def log_start_grammar(gid):
# def log_complete_grammar(request):
# def log_skip_grammar(request):


# Path: LL1_Academy/views/practice.py
import random
import ast
import json
from django.shortcuts import render
from django.http import JsonResponse, HttpRequest, HttpResponseRedirect, Http404, HttpResponseBadRequest
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.forms.models import model_to_dict
from LL1_Academy.views import stats 
from LL1_Academy.models import *

		uncompleted_grammars = Grammar.objects.all()
	else:
		completed_gids = UserHistory.objects.all().filter(user=user,complete=True).values_list('grammar',flat=True)
		uncompleted_grammars = Grammar.objects.exclude(gid__in=completed_gids)
	size = uncompleted_grammars.count()
	if size == 0:
		return None
	randid = random.randint(0,size-1)
	g=uncompleted_grammars[randid]
	return g

def practice(request):
	# on page load we start the session over

	#if 'gid' not in request.session or request.session['gid']==None
	#print(request.GET.get('gid') )

	if request.GET.get('gid') == None:
		random_grammar = get_random_grammar(request.user)
		if random_grammar == None:
			context = {
				"title": "You finished all our grammars!",
				"text": "Click on your username to visit your profile page. You will be able to continue practicing on grammars you have already completed or skipped."
			}
			return render(request, 'LL1_Academy/error.html', context)

		request.session['gid'] = random_grammar.gid
	else:
		request.session['gid'] = request.GET.get('gid')


	stats.log_start_grammar(request.session['gid'])