# 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
                        tag1=tag1,
                        tag2=tag2,
                        user1=user1,
                        user2=user2,
                        user1_set_win_count=user1_set_win_count,
                        user2_set_win_count=user2_set_win_count,
                        user1_match_win_count=user1_match_win_count,
                        user2_match_win_count=user2_match_win_count,
                        user1_score_matches_won=user1_score_matches_won,
                        user2_score_matches_won=user2_score_matches_won,
                        h2h_sets_played=h2h_sets_played,
                        h2h_matches_played=h2h_matches_played,
                        h2h_stages_played=h2h_stages_played,
                        mutual_tournaments=mutual_tournaments,
                        form=form) 


# browse all Users, 25 per page
@app.route('/browse_users')
@app.route('/browse_users/<region>')
@app.route('/browse_users/<region>/<int:page>')
def browse_users(region, page=1):
  g.region = region
  print g.region
  # if viewing global information, don't filter query by g.region
  # if character=="Main", or the default SelectField "title", don't filter for any character
  if g.region=="Global" or g.region=="National":
    userlist = User.query.join(User.trueskills).filter(TrueSkill.region=="Global").order_by(TrueSkill.cons_mu.desc()).paginate(page, USERS_PER_PAGE, False)
  else:
    # filter by g.region by joining Region and User.region, and order by Trueskill by joining Trueskill and User.trueskill

# 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

                        tag1=tag1,
                        tag2=tag2,
                        user1=user1,
                        user2=user2,
                        user1_set_win_count=user1_set_win_count,
                        user2_set_win_count=user2_set_win_count,
                        user1_match_win_count=user1_match_win_count,
                        user2_match_win_count=user2_match_win_count,
                        user1_score_matches_won=user1_score_matches_won,
                        user2_score_matches_won=user2_score_matches_won,
                        h2h_sets_played=h2h_sets_played,
                        h2h_matches_played=h2h_matches_played,
                        h2h_stages_played=h2h_stages_played,
                        mutual_tournaments=mutual_tournaments,
                        form=form) 


# browse all Users, 25 per page
@app.route('/browse_users')
@app.route('/browse_users/<region>')
@app.route('/browse_users/<region>/<int:page>')
def browse_users(region, page=1):
  g.region = region
  print g.region
  # if viewing global information, don't filter query by g.region
  # if character=="Main", or the default SelectField "title", don't filter for any character
  if g.region=="Global" or g.region=="National":
    userlist = User.query.join(User.trueskills).filter(TrueSkill.region=="Global").order_by(TrueSkill.cons_mu.desc()).paginate(page, USERS_PER_PAGE, False)
  else:
    # filter by g.region by joining Region and User.region, and order by Trueskill by joining Trueskill and User.trueskill

    userlist = User.query.join(User.trueskills).filter(TrueSkill.region==g.region).order_by(TrueSkill.cons_mu.desc()).paginate(page, USERS_PER_PAGE, False)