# 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
  return render_template("browse_tournaments.html",
                         tournamentlist=tournamentlist,
                         current_region=g.region)


# Displays all subtournaments in a TournamentHeader
@app.route('/tournament/<tournament_name>')
def tournamentHeader(tournament_name):
  print tournament_name
  tournament_name = sanitize_url(tournament_name)
  print "sanitized", tournament_name
  # get Tournament object given string name
  tournament_header = TournamentHeader.query.filter(TournamentHeader.name==tournament_name).first()
  sub_tournaments = tournament_header.sub_tournaments

  # if only 1 sub_tournament (Tournament is a 1-stage bracket), call tournament view function, rendering Tournament
  # elif multiple sub_tournament (Tournament is multi-stage bracket), render tournament_header
  if len(sub_tournaments)<=1:
    return tournament(tournament_header.name, sub_tournaments[0].name)
  elif len(sub_tournaments)>1:
    return render_template("tournament_header.html",
                         tournament_header=tournament_header,
                         sub_tournaments=sub_tournaments
                         )
  else:
    return

@app.route('/tournament/<tournament_header>/<sub_tournament>')
def tournament(tournament_header, sub_tournament):
  print sub_tournament

# 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

  return render_template("browse_tournaments.html",
                         tournamentlist=tournamentlist,
                         current_region=g.region)


# Displays all subtournaments in a TournamentHeader
@app.route('/tournament/<tournament_name>')
def tournamentHeader(tournament_name):
  print tournament_name
  tournament_name = sanitize_url(tournament_name)
  print "sanitized", tournament_name
  # get Tournament object given string name
  tournament_header = TournamentHeader.query.filter(TournamentHeader.name==tournament_name).first()
  sub_tournaments = tournament_header.sub_tournaments

  # if only 1 sub_tournament (Tournament is a 1-stage bracket), call tournament view function, rendering Tournament
  # elif multiple sub_tournament (Tournament is multi-stage bracket), render tournament_header
  if len(sub_tournaments)<=1:
    return tournament(tournament_header.name, sub_tournaments[0].name)
  elif len(sub_tournaments)>1:
    return render_template("tournament_header.html",
                         tournament_header=tournament_header,
                         sub_tournaments=sub_tournaments
                         )
  else:
    return

@app.route('/tournament/<tournament_header>/<sub_tournament>')
def tournament(tournament_header, sub_tournament):
  print sub_tournament

  sub_tournament = sanitize_url(sub_tournament)