# 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
    tournament_headers = TournamentHeader.query.filter(TournamentHeader.region==None).order_by(TournamentHeader.date.desc()).paginate(page, TOURNAMENTS_PER_PAGE, False)
  else:
    # filter for Tournaments by g.region, by joining Region and TournamentHeader.region
    tournament_headers = TournamentHeader.query.join(Region, TournamentHeader.region).filter(Region.region==g.region).order_by(TournamentHeader.date.desc()).paginate(page, TOURNAMENTS_PER_PAGE, False)
  return render_template("browse_tournament_headers.html",
                         tournament_headers=tournament_headers,
                         current_region=g.region)

# Lists tournaments in database, 15 per page
def browse_tournaments(region, page=1):
  pass
  # if viewing Global information, don't filter query by g.region
  g.region = region
  if g.region=="Global":
    tournamentlist = Tournament.query.order_by(Tournament.date.desc()).paginate(page, TOURNAMENTS_PER_PAGE, False)
  # if viewing national information, filter query to take Tournaments with region==None
  elif g.region=="National":
    tournamentlist = Tournament.query.filter(Tournament.region==None).order_by(Tournament.date.desc()).paginate(page, TOURNAMENTS_PER_PAGE, False)
  else:
    # filter for Tournaments by g.region, by joining Region and Tournament.region
    tournamentlist = Tournament.query.join(Region, Tournament.region).filter(Region.region==g.region).order_by(Tournament.date.desc()).paginate(page, TOURNAMENTS_PER_PAGE, False)
  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

# 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

    tournament_headers = TournamentHeader.query.filter(TournamentHeader.region==None).order_by(TournamentHeader.date.desc()).paginate(page, TOURNAMENTS_PER_PAGE, False)
  else:
    # filter for Tournaments by g.region, by joining Region and TournamentHeader.region
    tournament_headers = TournamentHeader.query.join(Region, TournamentHeader.region).filter(Region.region==g.region).order_by(TournamentHeader.date.desc()).paginate(page, TOURNAMENTS_PER_PAGE, False)
  return render_template("browse_tournament_headers.html",
                         tournament_headers=tournament_headers,
                         current_region=g.region)

# Lists tournaments in database, 15 per page
def browse_tournaments(region, page=1):
  pass
  # if viewing Global information, don't filter query by g.region
  g.region = region
  if g.region=="Global":
    tournamentlist = Tournament.query.order_by(Tournament.date.desc()).paginate(page, TOURNAMENTS_PER_PAGE, False)
  # if viewing national information, filter query to take Tournaments with region==None
  elif g.region=="National":
    tournamentlist = Tournament.query.filter(Tournament.region==None).order_by(Tournament.date.desc()).paginate(page, TOURNAMENTS_PER_PAGE, False)
  else:
    # filter for Tournaments by g.region, by joining Region and Tournament.region
    tournamentlist = Tournament.query.join(Region, Tournament.region).filter(Region.region==g.region).order_by(Tournament.date.desc()).paginate(page, TOURNAMENTS_PER_PAGE, False)
  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)