# 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
  character_object = Character.query.filter(Character.name==character).first()
  if character_object is None:
    return render_template('404.html'), 404

  # if viewing Global information, don't filter query by g.region
  if g.region=="Global" or g.region=="National":
    matching_users = User.query.join(TrueSkill.user, User).filter(and_(TrueSkill.region=="Global", User.character.contains(character_object))).order_by(TrueSkill.cons_mu.desc()).paginate(page, CHAR_USERS_PER_PAGE, False)
# if viewing national information, filter query to take Users with User.region==None
  else:
    matching_users = User.query.join(TrueSkill.user, User).join(Region, User.region).filter(and_(TrueSkill.region==g.region, User.characters.contains(character_object), Region.region==g.region)).order_by(TrueSkill.cons_mu.desc()).paginate(page, CHAR_USERS_PER_PAGE, False)
  if matching_users.total <= 0:
    flash('No players found that play this character')

  return render_template("character.html",
                         matching_users=matching_users,
                         character=character,
                         current_region=g.region)
 

# Lists tournament_headers in database, 15 per page
@app.route('/browse_tournaments')
@app.route('/browse_tournaments/<region>')
@app.route('/browse_tournaments/<region>/<int:page>')
def browse_tournaments(region, page=1):
  # if viewing Global information, don't filter query by g.region
  g.region = region
  if g.region=="Global":
    tournament_headers = TournamentHeader.query.order_by(TournamentHeader.date.desc()).paginate(page, TOURNAMENTS_PER_PAGE, False)
  # if viewing national information, filter query to take Tournaments with region==None
  elif g.region=="National":

# 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

  character_object = Character.query.filter(Character.name==character).first()
  if character_object is None:
    return render_template('404.html'), 404

  # if viewing Global information, don't filter query by g.region
  if g.region=="Global" or g.region=="National":
    matching_users = User.query.join(TrueSkill.user, User).filter(and_(TrueSkill.region=="Global", User.character.contains(character_object))).order_by(TrueSkill.cons_mu.desc()).paginate(page, CHAR_USERS_PER_PAGE, False)
# if viewing national information, filter query to take Users with User.region==None
  else:
    matching_users = User.query.join(TrueSkill.user, User).join(Region, User.region).filter(and_(TrueSkill.region==g.region, User.characters.contains(character_object), Region.region==g.region)).order_by(TrueSkill.cons_mu.desc()).paginate(page, CHAR_USERS_PER_PAGE, False)
  if matching_users.total <= 0:
    flash('No players found that play this character')

  return render_template("character.html",
                         matching_users=matching_users,
                         character=character,
                         current_region=g.region)
 

# Lists tournament_headers in database, 15 per page
@app.route('/browse_tournaments')
@app.route('/browse_tournaments/<region>')
@app.route('/browse_tournaments/<region>/<int:page>')
def browse_tournaments(region, page=1):
  # if viewing Global information, don't filter query by g.region
  g.region = region
  if g.region=="Global":
    tournament_headers = TournamentHeader.query.order_by(TournamentHeader.date.desc()).paginate(page, TOURNAMENTS_PER_PAGE, False)
  # if viewing national information, filter query to take Tournaments with region==None
  elif g.region=="National":

    tournament_headers = TournamentHeader.query.filter(TournamentHeader.region==None).order_by(TournamentHeader.date.desc()).paginate(page, TOURNAMENTS_PER_PAGE, False)