# Path: sort_utils.py
# def sort_placementlist(placementlist):
#   sorted_placementlist = sorted(placementlist, key=attrgetter('tournament.date', 'tournament.name'), reverse=True)
#   return sorted_placementlist
# 
# def sort_setlist(setlist):
#   sorted_setlist = sorted(setlist, key=attrgetter('tournament.date', 'id'))
#   try:
#   	return sorted_setlist
#   except:
#   	UnicodeEncodeError


from app import *
from app.models import *
from operator import attrgetter
from sort_utils import sort_placementlist, sort_setlist
import collections
    return 'UserPlacements(tournament_name=%s, placement=%s, seed=%s, tournament=%s)' % \
    (self.tournament_name, self.placement, self.seed, self.tournament)


def h2h_get_mutual_tournaments(user1, user2):
  '''
  given two User objects, returns list of lists containing two UserPlacements objects (which contain tournament_name,
  placement, and seed), corresponding to user1 and user2 for each mutual tournament attended

  call len() on the result of this method to get total number of tournaments
  attended together.
  '''

  # Debug this
  user1_placements = get_placement_info(user1)
  user2_placements = get_placement_info(user2)
 
  # if identical names for tournament (both attended same tournament), add to list of mutual tournaments
  mutual_tournaments = []
  for user1_placement in user1_placements:
    mutual_tournament = next((user2_placement for user2_placement in user2_placements if user1_placement.tournament_name==user2_placement.tournament_name), None)
    if mutual_tournament is not None:
      mutual_tournaments.append([user1_placement, mutual_tournament])
  return mutual_tournaments

# Make this a class function in models?
def get_placement_info(user):
  '''
  Returns list of UserPlacements object, each storing info for each tournament, placement, and seed
  '''

# Path: sort_utils.py
# def sort_placementlist(placementlist):
#   sorted_placementlist = sorted(placementlist, key=attrgetter('tournament.date', 'tournament.name'), reverse=True)
#   return sorted_placementlist
# 
# def sort_setlist(setlist):
#   sorted_setlist = sorted(setlist, key=attrgetter('tournament.date', 'id'))
#   try:
#   	return sorted_setlist
#   except:
#   	UnicodeEncodeError


# Path: h2h_stats_functions.py
from app import *
from app.models import *
from operator import attrgetter
from sort_utils import sort_placementlist, sort_setlist
import collections

    return 'UserPlacements(tournament_name=%s, placement=%s, seed=%s, tournament=%s)' % \
    (self.tournament_name, self.placement, self.seed, self.tournament)


def h2h_get_mutual_tournaments(user1, user2):
  '''
  given two User objects, returns list of lists containing two UserPlacements objects (which contain tournament_name,
  placement, and seed), corresponding to user1 and user2 for each mutual tournament attended

  call len() on the result of this method to get total number of tournaments
  attended together.
  '''

  # Debug this
  user1_placements = get_placement_info(user1)
  user2_placements = get_placement_info(user2)
 
  # if identical names for tournament (both attended same tournament), add to list of mutual tournaments
  mutual_tournaments = []
  for user1_placement in user1_placements:
    mutual_tournament = next((user2_placement for user2_placement in user2_placements if user1_placement.tournament_name==user2_placement.tournament_name), None)
    if mutual_tournament is not None:
      mutual_tournaments.append([user1_placement, mutual_tournament])
  return mutual_tournaments

# Make this a class function in models?
def get_placement_info(user):
  '''
  Returns list of UserPlacements object, each storing info for each tournament, placement, and seed
  '''

  user_placements_sorted = sort_placementlist(user.tournament_assocs)