import json

from flask import Blueprint, request, session, render_template, url_for, flash
from flask_email import EmailMessage
from flask_login import logout_user, login_user
from werkzeug.utils import redirect
from flask_babel import lazy_gettext as _

import APIFactory
from app import secured
from models import ProfileForm, UserEncoder, SigninForm, SignupForm
import providers


__author__ = 'hanl'

router = Blueprint('user', __name__,
                   template_folder='templates')


@router.route('/', methods=['GET'])
@secured
def index():
    return redirect(url_for('.profile'))


@router.route('/profile', methods=['GET', 'POST'])
@secured
def profile():
    user = providers.PROVIDER.get_user(session, True)
    form = ProfileForm(obj=user)
    if request.method == 'POST':
        pass
    elif request.method == 'GET':
        return render_template('profile.html', form=form, submit=_('Update'),
                               complete=user.has_details())


@router.route('/profile/<user>', methods=['GET'])
def user_profile(user=None):
    user = providers.User(username=user)
    form = ProfileForm(obj=user)
    if request.method == 'POST':
        pass
    elif request.method == 'GET':
        return render_template('profile.html', form=form, submit=_('Update'))


# @router.route('/delete', methods=['POST', 'GET'])
def delete():
    response = APIFactory.request('delete')
    pass


@router.route('/login', methods=['GET', 'POST'])
def login():
    form = SigninForm()
    if request.method == 'POST':
        if not form.validate():
            return render_template('login.html', form=form)
        else:
            user = providers.User(username=form.username.data, password=form.password.data)
            success = providers.PROVIDER.login(session, user)
            if success:
                login_user(user)
            else:
                return redirect(url_for('.login'))
            print "the data serialized %s" % json.dumps(user, cls=UserEncoder)
            return redirect(request.args.get('next') or url_for('.profile'))
    elif request.method == 'GET':
        providers.PROVIDER.is_admin("")
        return render_template('login.html', form=form, submit=_('sign in'))


@router.route('/logout', methods=['GET'])
@secured
def logout():
    if not providers.PROVIDER.logout(session):
        redirect(url_for('.login'))
    logout_user()
    return redirect(url_for('.index'))


@router.route('/signup', methods=['GET', 'POST'])
@secured
def signup():
    form = SignupForm()
    if request.method == 'POST':
        if not form.validate():
            return render_template('signup.html', form=form)
        else:
            newuser = providers.User(firstName=form.firstName.data, password=form.password.data,
                                     lastName=form.lastName.data,
                                     email=form.email.data, username=form.username.data,
                                     institution=form.institution.data, phone=form.phone.data,
                                     address=form.address.data)

            response = APIFactory.post("user/register",
                                       data=json.dumps(newuser, cls=UserEncoder))
            if response is None:
                return redirect(url_for('.index'))
            elif providers.MESSAGE_HANDLER.isError(response):
                providers.MESSAGE_HANDLER.notify(response.json(), flash)
            else:
                # send email
                email = EmailMessage(subject='successful registration', to=['micha.hanl@gmail.com'],
                                     body=response.content)
                email.send()
                print "the response %s" % str(response.content)
                providers.MESSAGE_HANDLER.notify("You successfully registered. Congrats", flash, category='success')
                return render_template('signup.html', form=form, submit=_('sign up'))

    elif request.method == 'GET':
        return render_template('signup.html', form=form, submit=_('sign up'))


@router.route('/reset', methods=['POST', 'GET'])
def reset():
    email = EmailMessage(subject='successful registration', to=['micha.hanl@gmail.com'],
                         body="This is a body")
    print "the messages %s " % str(email.send())
    return redirect(url_for('.index'))
