# Path: config.py
# USERS_PER_PAGE = 25
# 
# TOURNAMENTS_PER_PAGE = 15 
# 
# CHAR_USERS_PER_PAGE = 15
# 
# 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_userlist(userlist):
#   sorted_userlist = sorted(userlist, key=attrgetter('trueskill.mu'), reverse=True)
# 
# Path: misc_utils.py
# def sanitize_url(tournament_name):
# 	tournament_name = tournament_name.replace("%20", ' ')
# 	tournament_name = tournament_name.replace("%2F", '/')
# 	tournament_name = tournament_name.replace("%2D", '-')
# 	tournament_name = tournament_name.replace("%2E", '.')
# 	return tournament_name


from flask import render_template, flash, redirect, request, url_for, g, session
from app import app, db
from models import *
from forms import UserCreate, UserEdit, SetCreate, SetEdit, MatchSubmit, HeadToHead, SearchForm, CharacterFilter, character_choices, character_list, RegionSelect
from sqlalchemy import and_, or_
from config import USERS_PER_PAGE, TOURNAMENTS_PER_PAGE, CHAR_USERS_PER_PAGE 
from sanitize_utils import check_and_sanitize_tag, check_and_sanitize_tag_multiple
from sort_utils import sort_placementlist, sort_userlist
from misc_utils import sanitize_url
from h2h_stats_functions import *
import collections
import sys

  def __init__(self, tournament):
    self.tournament = tournament

  def __str__(self):
    return "UserTournamentSets(tournament_name=%s, tournament=%s, sets=%s)" % (self.tournament_name, self.tournament, self.sets)


# User profile page
@app.route('/user/<tag>')
@app.route('/user/<tag>/<int:page>')
def user(tag, page=1):
  # Pagination variables
  tournaments_per_page = 10 
  start_index = (page - 1) * tournaments_per_page
  end_index = start_index + tournaments_per_page

  user = User.query.filter(User.tag==tag).first()
  # If routed to user profile page (user/<tag>), check to make sure user exists
  if user is None:
    flash('User %s not found.' % tag)
    return redirect(url_for('browse_users', region=g.region))

  # get User secondaries
  user_characters = user.get_characters()
  
  # create ordered dictionary with Tournament name and respective placement; important to keep order so placements can be displayed in order of tournament.date 
  user_sets_by_tournament = collections.OrderedDict()

  # sort by Placement.tournament.date, in reverse order (most recent first). Sort by tournament.name as secondary sort

# Path: config.py
# USERS_PER_PAGE = 25
# 
# TOURNAMENTS_PER_PAGE = 15 
# 
# CHAR_USERS_PER_PAGE = 15
# 
# 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_userlist(userlist):
#   sorted_userlist = sorted(userlist, key=attrgetter('trueskill.mu'), reverse=True)
# 
# Path: misc_utils.py
# def sanitize_url(tournament_name):
# 	tournament_name = tournament_name.replace("%20", ' ')
# 	tournament_name = tournament_name.replace("%2F", '/')
# 	tournament_name = tournament_name.replace("%2D", '-')
# 	tournament_name = tournament_name.replace("%2E", '.')
# 	return tournament_name


# Path: app/views.py
from flask import render_template, flash, redirect, request, url_for, g, session
from app import app, db
from models import *
from forms import UserCreate, UserEdit, SetCreate, SetEdit, MatchSubmit, HeadToHead, SearchForm, CharacterFilter, character_choices, character_list, RegionSelect
from sqlalchemy import and_, or_
from config import USERS_PER_PAGE, TOURNAMENTS_PER_PAGE, CHAR_USERS_PER_PAGE 
from sanitize_utils import check_and_sanitize_tag, check_and_sanitize_tag_multiple
from sort_utils import sort_placementlist, sort_userlist
from misc_utils import sanitize_url
from h2h_stats_functions import *
import collections
import sys


  def __init__(self, tournament):
    self.tournament = tournament

  def __str__(self):
    return "UserTournamentSets(tournament_name=%s, tournament=%s, sets=%s)" % (self.tournament_name, self.tournament, self.sets)


# User profile page
@app.route('/user/<tag>')
@app.route('/user/<tag>/<int:page>')
def user(tag, page=1):
  # Pagination variables
  tournaments_per_page = 10 
  start_index = (page - 1) * tournaments_per_page
  end_index = start_index + tournaments_per_page

  user = User.query.filter(User.tag==tag).first()
  # If routed to user profile page (user/<tag>), check to make sure user exists
  if user is None:
    flash('User %s not found.' % tag)
    return redirect(url_for('browse_users', region=g.region))

  # get User secondaries
  user_characters = user.get_characters()
  
  # create ordered dictionary with Tournament name and respective placement; important to keep order so placements can be displayed in order of tournament.date 
  user_sets_by_tournament = collections.OrderedDict()

  # sort by Placement.tournament.date, in reverse order (most recent first). Sort by tournament.name as secondary sort

  user_tournaments_sorted = sort_placementlist(user.tournament_assocs)