# Path: sort_utils.py
# def sort_setlist(setlist):
#   sorted_setlist = sorted(setlist, key=attrgetter('tournament.date', 'id'))
#   try:
#   	return sorted_setlist
#   except:
#   	UnicodeEncodeError
# 
# Path: misc_utils.py
# def print_ignore(input):
# 	try:
# 		print input
# 	except UnicodeError:
# 		pass


from app import app, db
from app.models import *
from sort_utils import sort_setlist
from misc_utils import print_ignore
from trueskill import setup, Rating, quality_1vs1, rate_1vs1
import time
	# Iterate through all Sets in order and recalculate Trueskill; currently in order of set.id
	# order Sets by tournament date, then by set id, oldest being at index 0
	setlist = Set.query.all()
	sorted_setlist = sort_setlist(setlist)

	# BUG: if user.tag changed for a User, the Set winner/loser tag will still remain, so when querying for User using the Set's outdated tag, queried User becomes None.
	# Solution: Check and sanitize winner_tag, loser_tag again. But if it wasn't recognized in the first place, still an issue
	# This takes a long time to run. How to optimize?
	for set in sorted_setlist:
		winner_user = User.query.filter(User.tag==set.winner_tag).first()
		loser_user = User.query.filter(User.tag==set.loser_tag).first()
		update_rating(winner_user, loser_user)
		print sorted_setlist.index(set)
	print "All trueskills recalculated"

def trueskills_dict():
	'''
	Returns a dictionary with every user represented as a dictionary key User.tag
	Dictionary values are the user's corresponding Trueskills
	'''
	skills_by_user = {}
	userlist = User.query.all()
	for user in userlist:
		print_ignore(user.tag)

		# Check to see if trueskill attribute is None (new User), and if so sets it to Trueskill object with default values
		populate_trueskills(user)

		# Assign value to user key, the corresponding list of User's Trueskills
		skills_by_user[user.tag] = user.trueskills

# Path: sort_utils.py
# def sort_setlist(setlist):
#   sorted_setlist = sorted(setlist, key=attrgetter('tournament.date', 'id'))
#   try:
#   	return sorted_setlist
#   except:
#   	UnicodeEncodeError
# 
# Path: misc_utils.py
# def print_ignore(input):
# 	try:
# 		print input
# 	except UnicodeError:
# 		pass


# Path: trueskill_functions.py
from app import app, db
from app.models import *
from sort_utils import sort_setlist
from misc_utils import print_ignore
from trueskill import setup, Rating, quality_1vs1, rate_1vs1
import time

	# Iterate through all Sets in order and recalculate Trueskill; currently in order of set.id
	# order Sets by tournament date, then by set id, oldest being at index 0
	setlist = Set.query.all()
	sorted_setlist = sort_setlist(setlist)

	# BUG: if user.tag changed for a User, the Set winner/loser tag will still remain, so when querying for User using the Set's outdated tag, queried User becomes None.
	# Solution: Check and sanitize winner_tag, loser_tag again. But if it wasn't recognized in the first place, still an issue
	# This takes a long time to run. How to optimize?
	for set in sorted_setlist:
		winner_user = User.query.filter(User.tag==set.winner_tag).first()
		loser_user = User.query.filter(User.tag==set.loser_tag).first()
		update_rating(winner_user, loser_user)
		print sorted_setlist.index(set)
	print "All trueskills recalculated"

def trueskills_dict():
	'''
	Returns a dictionary with every user represented as a dictionary key User.tag
	Dictionary values are the user's corresponding Trueskills
	'''
	skills_by_user = {}
	userlist = User.query.all()
	for user in userlist:
		print_ignore(user.tag)

		# Check to see if trueskill attribute is None (new User), and if so sets it to Trueskill object with default values
		populate_trueskills(user)

		# Assign value to user key, the corresponding list of User's Trueskills
		skills_by_user[user.tag] = user.trueskills

		print_ignore(skills_by_user[user.tag])