"""
Module implementing the view portion of the MVC pattern.
"""

import json
import hashlib

from flickipedia.parse import parse
from flickipedia.redisio import DataIORedis, _decode_list, _decode_dict

from flickipedia.config import log, settings
from flickipedia.web import app
from flickipedia.sources import flickr
import wikipedia

from flask import render_template, redirect, url_for, \
    request, escape, flash

__author__ = 'Ryan Faulkner'
__date__ = "2014-03-30"


# Flask Login views

from flickipedia.web.session import APIUser

from flask.ext.login import login_required, logout_user, \
    confirm_login, login_user, fresh_login_required, current_user

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST' and 'username' in request.form:

        username = escape(unicode(str(request.form['username'])))
        passwd = escape(unicode(str(request.form['password'])))
        remember = request.form.get('remember', 'no') == 'yes'

        # Initialize user
        user_ref = APIUser(username)
        user_ref.authenticate(passwd)

        log.debug(__name__ + ' :: Authenticating "{0}"/"{1}" ...'.
            format(username, passwd))

        if user_ref.is_authenticated():
            login_user(user_ref, remember=remember)
            flash('Logged in.')
            return redirect(request.args.get('next')
                            or url_for('api_root'))
        else:
            flash('Login failed.')
    return render_template('login.html')

@app.route('/reauth', methods=['GET', 'POST'])
@login_required
def reauth():
    if request.method == 'POST':
        confirm_login()
        flash(u'Reauthenticated.')
        return redirect(request.args.get('next') or url_for('api_root'))
    return render_template('reauth.html')

@app.route('/logout')
def logout():
    logout_user()
    flash('Logged out.')
    return redirect(url_for('api_root'))


def home():
    """ View for root url - API instructions """
    if current_user.is_anonymous():
        return render_template('index_anon.html')
    else:
        return render_template('index.html')


def about():
    return render_template('about.html')


def contact():
    return render_template('contact.html')


def version():
    return render_template('version.html', version=settings.__version__)


def mashup():

    DataIORedis().connect()

    key = hashlib.md5(request.form['article']).hexdigest()
    body = DataIORedis().read(key)

    if not body:

        wiki = wikipedia.page(request.form['article'])
        html = parse(wiki.content.split('\n'))

        res = flickr.call('photos_search', {'text': request.form['article'],
                                            'format': 'json',
                                            'sort': 'relevance',
                                            })
        res_json = json.loads(res[14:-1])

        # Extract data for the first photo returned
        owner = res_json['photos']['photo'][0]['owner']
        photo_id = res_json['photos']['photo'][0]['id']
        farm = res_json['photos']['photo'][0]['farm']
        server = res_json['photos']['photo'][0]['server']
        title = res_json['photos']['photo'][0]['title']
        secret = res_json['photos']['photo'][0]['secret']

        page_content = {
            'content': html,
            'owner': owner,
            'photo_id': photo_id,
            'farm': farm,
            'server': server,
            'title': title,
            'secret': secret
        }
        DataIORedis().write(key, json.dumps(page_content))

    else:
         page_content = json.loads(body, object_hook=_decode_dict)

    return render_template('mashup.html', **page_content)


# Add View Decorators
# ##

# Stores view references in structure
view_list = {
    home.__name__: home,
    about.__name__: about,
    contact.__name__: contact,
    version.__name__: version,
    mashup.__name__: mashup,
}

# Dict stores routing paths for each view

from werkzeug.routing import BaseConverter
class RegexConverter(BaseConverter):
    def __init__(self, url_map, *items):
        super(RegexConverter, self).__init__(url_map)
        self.regex = items[0]

app.url_map.converters['regex'] = RegexConverter

route_deco = {
    home.__name__: app.route('/'),
    about.__name__: app.route('/about/'),
    contact.__name__: app.route('/contact/'),
    version.__name__: app.route('/version'),
    mashup.__name__: app.route('/mashup',  methods=['POST']),
}

# Dict stores flag for login required on view
views_with_anonymous_access = [
    home.__name__,
    about.__name__,
    contact.__name__,
    mashup.__name__,
]

# Apply decorators to views
def init_views():
    for key in view_list:
        if key not in views_with_anonymous_access:
            view_list[key] = login_required(view_list[key])

    for key in route_deco:
        route = route_deco[key]
        view_method = view_list[key]
        view_list[key] = route(view_method)

    log.info(__name__ + ' :: Registered views - {0}'.format(str(view_list)))
@@ -59,16 +59,126 @@
 n.%0A%22%22%22%0A%0A
+# Fixes issue with jinja template encoding in unicode%0Aimport sys%0Areload(sys)%0Asys.setdefaultencoding('utf-8')%0A%0A
 import j
@@ -3692,25 +3692,24 @@
 se:%0A        
- 
 page_content
@@ -3753,16 +3753,85 @@
 de_dict)
+%0A        # page_content%5B'content'%5D = unicode(page_content%5B'content'%5D)
 %0A%0A    re

fix - for jinja template accepting utf-8 chars