#!/usr/bin/env python3
import json
import random
import string
import httplib2
import requests
from flask import session as login_session
from sqlalchemy import create_engine, asc, desc, and_
from sqlalchemy.orm import sessionmaker
from oauth2client.client import flow_from_clientsecrets, FlowExchangeError
from models import Base, Category, CatalogItem, User
from flask import Flask, url_for, render_template, redirect, request
from flask import jsonify, make_response

app = Flask(__name__)

CLIENT_ID = json.loads(
    open('client_secrets.json', 'r').read())['web']['client_id']
APPLICATION_NAME = "Item Catalog Application"

# Connect to database and create database session
engine = create_engine('sqlite:///itemcatalog.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()


# Source for Google Sign-In from Udacity's Authentication and
# Authorization course
@app.route('/gconnect', methods=['POST'])
def gconnect():
    """Login with Google Sign-In"""
    # Validate state token
    if request.args.get('state') != login_session['state']:
        response = make_response(json.dumps('Invalid state parameter.'), 401)
        response.headers['Content-Type'] = 'application/json'
        return response
    # Obtain authorization code, now compatible with Python3
    request.get_data()
    code = request.data.decode('utf-8')

    try:
        # Upgrade the authorization code into a credentials object
        oauth_flow = flow_from_clientsecrets('client_secrets.json', scope='')
        oauth_flow.redirect_uri = 'postmessage'
        credentials = oauth_flow.step2_exchange(code)
    except FlowExchangeError:
        response = make_response(
            json.dumps('Failed to upgrade the authorization code.'), 401)
        response.headers['Content-Type'] = 'application/json'
        return response

    # Check that the access token is valid.
    access_token = credentials.access_token
    url = ('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=%s'
           % access_token)
    # Submit request, parse response - Python3 compatible
    h = httplib2.Http()
    response = h.request(url, 'GET')[1]
    str_response = response.decode('utf-8')
    result = json.loads(str_response)

    # If there was an error in the access token info, abort.
    if result.get('error') is not None:
        response = make_response(json.dumps(result.get('error')), 500)
        response.headers['Content-Type'] = 'application/json'
        return response

    # Verify that the access token is used for the intended user.
    gplus_id = credentials.id_token['sub']
    if result['user_id'] != gplus_id:
        response = make_response(
            json.dumps("Token's user ID doesn't match given user ID."), 401)
        response.headers['Content-Type'] = 'application/json'
        return response

    # Verify that the access token is valid for this app.
    if result['issued_to'] != CLIENT_ID:
        response = make_response(
            json.dumps("Token's client ID does not match app's."), 401)
        response.headers['Content-Type'] = 'application/json'
        return response

    stored_access_token = login_session.get('access_token')
    stored_gplus_id = login_session.get('gplus_id')
    if stored_access_token is not None and gplus_id == stored_gplus_id:
        response = make_response(json.dumps('Current user is already '
                                            'connected.'), 200)
        response.headers['Content-Type'] = 'application/json'
        return response

    # Store the access token in the session for later use.
    login_session['access_token'] = access_token
    login_session['gplus_id'] = gplus_id

    # Get user info
    userinfo_url = "https://www.googleapis.com/oauth2/v1/userinfo"
    params = {'access_token': access_token, 'alt': 'json'}
    answer = requests.get(userinfo_url, params=params)

    data = answer.json()

    login_session['username'] = data['name']
    login_session['email'] = data['email']

    # see if user exists, if it doesn't make a new one
    user_id = getUserID(login_session['email'])
    if not user_id:
        user_id = createUser(login_session)
    login_session['user_id'] = user_id

    output = ''
    output += '<h1>Welcome, '
    output += login_session['username']
    output += '!</h1>'
    return output


# DISCONNECT - Revoke a current user's token and reset their login_session
@app.route('/gdisconnect')
def gdisconnect():
    """Disconnect from Google Sign-In"""
    # Only disconnect a connected user.
    access_token = login_session.get('access_token')
    if access_token is None:
        response = make_response(
            json.dumps('Current user not connected.'), 401)
        response.headers['Content-Type'] = 'application/json'
        return response
    url = 'https://accounts.google.com/o/oauth2/revoke?token=%s' % access_token
    h = httplib2.Http()
    result = h.request(url, 'GET')[0]
    if result['status'] == '200':
        # Reset the user's sesson.
        del login_session['access_token']
        del login_session['gplus_id']
        del login_session['username']
        del login_session['email']

        response = make_response(json.dumps('Successfully disconnected.'), 200)
        response.headers['Content-Type'] = 'application/json'
        return response
    else:
        # For whatever reason, the given token was invalid.
        response = make_response(
            json.dumps('Failed to revoke token for given user.', 400))
        response.headers['Content-Type'] = 'application/json'
        return response


# User Helper Functions
def createUser(login_session):
    """Create a new user in database"""
    newUser = User(name=login_session['username'],
                   email=login_session['email'])
    session.add(newUser)
    session.commit()
    user = session.query(User).filter_by(email=login_session['email']).one()
    return user.id


def getUserInfo(user_id):
    """Return User information"""
    user = session.query(User).filter_by(id=user_id).one()
    return user


def getUserID(email):
    """Return User ID"""
    try:
        user = session.query(User).filter_by(email=email).one()
        return user.id
    except:
        return None


# Create anti-forgery state token
def getStateToken():
    """Return anti-forgery state token for login"""
    state = ''.join(random.choice(string.ascii_uppercase +
                    string.digits) for x in range(32))
    login_session['state'] = state
    return state


# JSON APIs to view the catalog information
@app.route('/catalog/api/catalog/categories/JSON')
def categoriesJSON():
    """Return all categories in JSON format"""
    categories = session.query(Category).all()
    return jsonify(categories=[c.serialize for c in categories])


@app.route('/catalog/api/catalog/items/JSON')
def itemsJSON():
    """Return all items in JSON format"""
    items = session.query(CatalogItem).all()
    return jsonify(items=[i.serialize for i in items])


@app.route('/catalog/api/catalog/<string:category_name>/items/JSON')
def catItemsJSON(category_name):
    """Return all items for specific category in JSON format"""
    try:
        items = session.query(CatalogItem).filter_by(
            category_name=category_name).all()
        return jsonify(items=[i.serialize for i in items])
    except:
        return render_template('page_not_found.html')


# Show main catalog page
@app.route('/')
@app.route('/catalog/')
@app.route('/catalog/latest')
def showCatalog():
    """Route to main page of catalog"""
    categories = session.query(Category).order_by(asc(Category.name))
    items = session.query(CatalogItem).order_by(desc(
        CatalogItem.created_date)).limit(10)
    list_title = "Latest"
    if 'username' not in login_session:
        return render_template('index.html', categories=categories,
                               items=items, list_title=list_title,
                               public=True, STATE=getStateToken())
    else:
        return render_template('index.html', categories=categories,
                               items=items, list_title=list_title,
                               public=False, STATE=getStateToken())


# Show category page
@app.route('/catalog/<string:category_name>')
def showCategory(category_name):
    """Route to specific category page"""
    try:
        categories = session.query(Category).order_by(asc(Category.name))
        items = session.query(CatalogItem).filter_by(
            category_name=category_name).order_by(asc(CatalogItem.name)).all()
        list_title = category_name
        if 'username' not in login_session:
            return render_template('index.html', categories=categories,
                                   items=items, list_title=list_title,
                                   public=True, STATE=getStateToken())
        else:
            return render_template('index.html', categories=categories,
                                   items=items, list_title=list_title,
                                   public=False, STATE=getStateToken())
    except:
        return render_template('page_not_found.html')


@app.route('/catalog/<string:category_name>/<string:item_name>')
def showItem(category_name, item_name):
    """Route to specific item page"""
    try:
        item = session.query(CatalogItem).filter(and_(
            CatalogItem.name == item_name,
            CatalogItem.category_name == category_name)).one()
        creator = getUserInfo(item.user_id)
        if 'username' not in login_session:
            return render_template('item.html', item=item, public=True,
                                   STATE=getStateToken())
        elif creator.id != login_session['user_id']:
            return render_template('item.html', item=item, public=False,
                                   creator=False, STATE=getStateToken())
        else:
            return render_template('item.html', item=item, public=False,
                                   creator=True, STATE=getStateToken())
    except:
        return render_template('page_not_found.html')


# Create a new catalog item
@app.route('/catalog/new', methods=['GET', 'POST'])
def newItem():
    """Route to new item page"""
    if 'username' not in login_session:
        return redirect('/login')
    if request.method == 'POST':
        newItem = CatalogItem(user_id=login_session['user_id'],
                              name=request.form['name'],
                              description=request.form['description'],
                              category_name=request.form['category'])
        session.add(newItem)
        session.commit()
        return redirect(url_for('showCatalog'))
    else:
        categories = session.query(Category).order_by(asc(Category.name))
        return render_template('new_item.html', categories=categories)


# Edit a catalog item
@app.route('/catalog/<string:category_name>/<string:item_name>/edit',
           methods=['GET', 'POST'])
def editItem(category_name, item_name):
    """Route to edit item page"""
    if 'username' not in login_session:
        return redirect('/login')
    try:
        itemToEdit = session.query(CatalogItem).filter(and_(
            CatalogItem.name == item_name,
            CatalogItem.category_name == category_name)).one()
        if request.method == 'POST':
            if request.form['name']:
                itemToEdit.name = request.form['name']
                itemToEdit.description = request.form['description']
                itemToEdit.category_name = request.form['category']
                session.add(itemToEdit)
                session.commit()
                return redirect(url_for('showCategory',
                                        category_name=category_name))
        else:
            categories = session.query(Category).order_by(asc(Category.name))
            return render_template('edit_item.html', item=itemToEdit,
                                   categories=categories)
    except:
        return render_template('page_not_found.html')


# Delete a catalog item
@app.route('/catalog/<string:category_name>/<string:item_name>/delete',
           methods=['GET', 'POST'])
def deleteItem(category_name, item_name):
    """Route to delete item page"""
    if 'username' not in login_session:
        return redirect('/login')
    try:
        itemToDelete = session.query(CatalogItem).filter(and_(
            CatalogItem.name == item_name,
            CatalogItem.category_name == category_name)).one()
        if request.method == 'POST':
            session.delete(itemToDelete)
            session.commit()
            return redirect(url_for('showCategory',
                                    category_name=category_name))
        else:
            categories = session.query(Category).order_by(asc(Category.name))
            return render_template('delete_item.html', item=itemToDelete,
                                   categories=categories)
    except:
        return render_template('page_not_found.html')


@app.errorhandler(404)
def pageNotFound(error):
    """Return error page"""
    return render_template('page_not_found.html'), 404


if __name__ == '__main__':
    app.secret_key = 'super_secret_key'
    app.debug = True
    app.run(host='0.0.0.0', port=8000)
