import datetime
import hashlib

from flask import (
    abort, render_template, flash, g, request, redirect, make_response, url_for)

from . import app
from .lib import current_page, authenticate, validate_signup, create_user
from .models import Quote, User, AREA_ORDER_MAP, DEFAULT_ORDER, QSTATUS


MEMBER_AREAS = ['favourites', 'disapproved']
ADMIN_AREAS = ['unapproved', 'reported', 'deleted']

@app.before_request
def before_request():
    g.current_page = current_page()
    g.user = None
    auth = request.cookies.get('auth')
    username = request.cookies.get('username')
    level = request.cookies.get('level')
    if auth:
        value = '{}:{}:{}'.format(app.config['COOKIE_SECRET'], username, level)
        if auth == hashlib.md5(value).hexdigest():
            user = User.query.filter(User.username == username).first()
            if user:
                g.user = user


@app.route('/')
def landing_page():
    return render_template('/index.html')


@app.route('/browse')
@app.route('/browse/<int:quote_id>')
@app.route('/browse/<area>')
def browse(area=None, quote_id=None):
    if area in MEMBER_AREAS and not g.user:
        flash('You must be logged in to view that page.', 'info')
        return redirect(url_for('login', redirect_url=request.path))
    if area in ADMIN_AREAS and not g.user.is_admin():
        abort(404)
    g.page = area or 'browse'
    quotes = Quote.query
    if quote_id is not None:
        quotes = quotes.filter(Quote.id == quote_id)
        if not quotes or quotes[0].status != QSTATUS['approved']:
            abort(404)
    else:
        try:
            quotes = quotes.filter(Quote.status == QSTATUS[area])
        except KeyError:
            # This is the default case, for areas like "best" and "worst", that
            # don't have a specific filter.
            quotes = quotes.filter(Quote.status == QSTATUS['approved'])
        quotes = quotes.order_by(AREA_ORDER_MAP.get(area, DEFAULT_ORDER))
    pagination = quotes.paginate(
        g.current_page, app.config['QUOTES_PER_PAGE'], error_out=True)
    if quote_id or area == 'random':
        pagination.items = pagination.items[:1]
    return render_template('/browse.html', pagination=pagination)


@app.route('/browse/tags')
@app.route('/browse/tags/<tag>')
def browse_by_tags(tag=None, page=None):
    raise NotImplementedError()


@app.route('/search')
@app.route('/search/<term>')
def search(term=None, page=None):
    raise NotImplementedError()

@app.route('/create')
def new_quote():
    raise NotImplementedError()


@app.route('/signup', methods=['GET', 'POST'])
def signup():
    g.page = 'sign up'
    if request.method == 'GET':
        return render_template('/signup.html')
    username = request.form['username']
    password = request.form['password']
    password_confirm = request.form['password_confirm']
    email = request.form['email']

    validity = validate_signup(username, password, password_confirm, email)
    if not validity['status']:
        flash(validity['msg'], 'error')
        return render_template('/signup.html')
    try:
        create_user(username, password, email)
        authenticate(username, password)
        g.user = User.query.filter(User.username == username).first()
        return render_template('/signup_success.mako')
    except NameError, e:
        flash(e.__str__(), 'error')
        return render_template('/signup.html')


@app.route('/login', methods=['GET', 'POST'])
def login():
    g.page = 'log in'
    if request.method == 'GET':
        return render_template('/login.html', redirect_url=request.args.get('redirect_url', ''))
    user = authenticate(request.form['username'], request.form['password'])
    if not user:
        flash('Incorrect username / password', 'error')
        return render_template('/login.html')
    cleartext_value = '{}:{}:{}'.format(
        app.config['COOKIE_SECRET'], user.username, user.level)
    auth = hashlib.md5(cleartext_value).hexdigest()
    if request.args.get('redirect_url') not in [None, '/signup', '/logout', '/reset_password']:
        response = make_response(redirect(request.args.get('redirect_url')))
    else:
        response = make_response(redirect(url_for('browse')))
    expiry = datetime.datetime.now() + datetime.timedelta(
        days=app.config['COOKIE_LIFETIME'])
    response.set_cookie('auth', auth, expires=expiry)
    response.set_cookie('username', user.username, expires=expiry)
    response.set_cookie('level', str(user.level), expires=expiry)
    return response


@app.route('/logout')
def logout():
    g.page = 'logout'
    response = make_response(redirect(url_for('landing_page')))
    response.set_cookie('auth', '', expires=0)
    response.set_cookie('username', '', expires=0)
    response.set_cookie('level', '', expires=0)
    g.user = None
    flash('Logged out successfully!', 'info')
    return response


@app.route('/reset_password')
def reset_password():
    raise NotImplementedError()

@@ -243,20 +243,13 @@
 ort 
-Quote, User,
+(%0A   
  ARE
@@ -260,16 +260,20 @@
 DER_MAP,
+%0A   
  DEFAULT
@@ -279,24 +279,28 @@
 T_ORDER,
+%0A   
  QSTATUS
 %0A%0A%0AMEMBE
@@ -291,17 +291,49 @@
  QSTATUS
-%0A
+,%0A    Quote,%0A    Tag,%0A    User,%0A)
 %0A%0AMEMBER
@@ -2335,32 +2335,52 @@
 ne, page=None):%0A
+    if not tag:%0A    
     raise NotImp
@@ -2388,32 +2388,430 @@
 ementedError()%0A%0A
+    tag = Tag.query.filter(Tag.tag == tag).one()%0A    q = Quote.query%0A    q = q.filter(Quote.tags.contains(tag))%0A    q = q.filter(Quote.status == QSTATUS%5B'approved'%5D)%0A    q = q.order_by(Quote.submitted.desc())%0A%0A    pagination = q.paginate(%0A        g.current_page,%0A        app.config%5B'QUOTES_PER_PAGE'%5D,%0A        error_out=True%0A    )%0A%0A    return render_template('/browse.html', pagination=pagination)%0A
 %0A@app.route('/se

Implement browsing by tag