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

from versus.config import log
from versus.config.settings import AUTHORS, LICENSE, HDFS_BUFFER_FILE, \
    __version__
from versus.src.web import app
from versus.tools.dataIO import DataIOHDFS

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

__author__ = AUTHORS
__date__ = "2013-08-20"
__license__ = LICENSE


# Flask Login views

from versus.src.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=__version__)


def ingest():
    """
    Handles form data ingestion.
    """
    # redirect to home with a message
    # return render_template('about.html')

    # TODO - Get form data
    label = None
    text = None

    with open(HDFS_BUFFER_FILE, 'a') as f:
        # TODO - use control char as separator
        f.write(str(label) + ':' + str(text) + '\n')

        # TODO -
        # if size of file >= max bytes:
        #    hdfs_path = get_path()
        #    DataIOHDFS().write(HDFS_BUFFER_FILE, hdfs_path)

    return redirect(url_for('home'))


def train():
    """
    Invoke training from control.
    """

    # TODO - Get form data
    dataset = None
    model_type = None

    # TODO - given model type and data train a new model and store

    return render_template('train.html')


# Add View Decorators
# ##

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

# 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'),
    ingest.__name__: app.route('/ingest', methods=['GET', 'POST']),
    train.__name__: app.route('/train', methods=['GET', 'POST'])
}

# Dict stores flag for login required on view
views_with_anonymous_access = [
    home.__name__,
    about.__name__,
    contact.__name__,
    ingest.__name__,
    train.__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)))
@@ -2405,63 +2405,122 @@
     
-# TODO - Get form data%0A    label = None%0A    text = None
+label = request.form%5B'phraseInput'%5D%0A    text = request.form%5B'labelInput'%5D%0A    # model = request.form%5B'modelInput'%5D
 %0A%0A  

add - handle post input to 'ingest' view