# 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
  '''
  Returns list of UserPlacements object, each storing info for each tournament, placement, and seed
  '''
  user_placements_sorted = sort_placementlist(user.tournament_assocs)
  
  placings = []
  for tournament_placement in user_placements_sorted:
    user_placement = UserPlacements(tournament_placement.tournament.name)
    user_placement.tournament = tournament_placement.tournament
    user_placement.placement = convert_placement(tournament_placement.placement)
    placings.append(user_placement)
  return placings


def h2h_get_sets_played(user1, user2):
  """Given two users, returns all sets played between them, sorted by set id.

  Args:
      user1 : User(models.py) : First user object to query for sets.
      user2 : User(models.py) : Second user object to query for sets.

  Returns:
      A list of Set(models.py) objects sorted by set id of sets played between
      the two users.
  """
  user1_won_sets = h2h_get_sets_won(user1, user2)
  user2_won_sets = h2h_get_sets_won(user2, user1)
  # Any set user2 has won, user1 has lost, so user2_won == number of sets user1 has lost.

  h2h_sets_played = user1_won_sets + user2_won_sets

# 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

  '''
  Returns list of UserPlacements object, each storing info for each tournament, placement, and seed
  '''
  user_placements_sorted = sort_placementlist(user.tournament_assocs)
  
  placings = []
  for tournament_placement in user_placements_sorted:
    user_placement = UserPlacements(tournament_placement.tournament.name)
    user_placement.tournament = tournament_placement.tournament
    user_placement.placement = convert_placement(tournament_placement.placement)
    placings.append(user_placement)
  return placings


def h2h_get_sets_played(user1, user2):
  """Given two users, returns all sets played between them, sorted by set id.

  Args:
      user1 : User(models.py) : First user object to query for sets.
      user2 : User(models.py) : Second user object to query for sets.

  Returns:
      A list of Set(models.py) objects sorted by set id of sets played between
      the two users.
  """
  user1_won_sets = h2h_get_sets_won(user1, user2)
  user2_won_sets = h2h_get_sets_won(user2, user1)
  # Any set user2 has won, user1 has lost, so user2_won == number of sets user1 has lost.

  h2h_sets_played = user1_won_sets + user2_won_sets

  h2h_sets_played = sort_setlist(h2h_sets_played)