# -*- coding: utf-8 -*-
import flask

from flask import Flask,jsonify,send_from_directory, render_template, request,redirect,url_for
from config import Config
import yaml
import os
from sqlalchemy import desc
from storage import MailThread,db_session

app = Flask(__name__, template_folder="templates", static_folder="static")
package_directory = os.path.dirname(os.path.abspath(__file__))

cfg = Config(file(os.path.join(package_directory, 'config.cfg')))

maintopic_values=["studium", "information","ausleihen","jobausschreibung", "umfragen"]

def render_index(mths,opened=None,code=200):
    return render_template("index.html",mths=mths,
                           title=cfg.title.decode("utf8"),opened=opened,maintopics=maintopic_values
                           ), code    
from classifier import get_pipe
#mail_threads=db_session.query(MailThread).all()
#pipe1,le=get_pipe("pipe1",b"answered")
#pipe2,le2=get_pipe("pipe2b", b"maintopic")
#pipe3,le3=get_pipe("pipe2b", b"lang")

#answered=le.inverse_transform(pipe1.predict(mail_threads))
#maintopic=le2.inverse_transform(pipe2.predict(mail_threads))
#lang=le3.inverse_transform(pipe3.predict(mail_threads))

#for i, t in enumerate(mail_threads):
#    t.answered=answered[i]
#    t.maintopic=maintopic[i]
#    t.lang=lang[i]


@app.route("/")
def hello():
    mth=db_session.query(MailThread).order_by(desc(MailThread.date)).all()
    return render_index(mth) 

def store_value(id,key,value):
    mth=db_session.query(MailThread).filter(MailThread.firstmail==int(id)).first()
    
    if key =="answered":
        value = value in ["true", "True", "1", "t"]
        mth.answered=bool(value)
        mth.opened=bool(value)
    if key=="maintopic" and value in maintopic_values:
        mth.maintopic=str(value)
    if key=="lang" and value in ["de", "en"]:
        mth.lang=str(value)
    if key =="trained":
        value = value in ["true", "True", "1", "t"]
        mth.istrained=bool(value)
    db_session.add(mth)
    db_session.commit()



@app.route("/<int:id>")
def store_answered(id):

    key = request.args.get('key')
    value = request.args.get('value')
    if not key is None and not value is None:
        store_value(id,key,value)
    mth=db_session.query(MailThread).filter(MailThread.firstmail==int(id)).first()
    
    return render_index([mth], opened=id) 



@app.route("/studium/")
def studium():
    mth=db_session.query(MailThread).filter(MailThread.maintopic=="studium").order_by(desc(MailThread.date)).all()
    return render_index(mth) 


@app.route("/<maintopic>/")
def maintopic(maintopic):
    if maintopic == "trained":
        mth=db_session.query(MailThread).filter(MailThread.istrained==True).order_by(desc(MailThread.date)).all()
    else:
        mth=db_session.query(MailThread).filter(MailThread.maintopic=="%s" % maintopic).order_by(desc(MailThread.date)).all()
    return render_index(mth) 

@app.route("/<maintopic>/<int:id>")
def maintopic_store(maintopic,id):
    if maintopic == "trained":
        mth=db_session.query(MailThread).filter(MailThread.istrained==True).order_by(desc(MailThread.date)).all()
    else:
        mth=db_session.query(MailThread).filter(MailThread.maintopic=="%s" % maintopic).order_by(desc(MailThread.date)).all()

    key = request.args.get('key')
    value = request.args.get('value')
    

    if not key is None and not value is None:
        store_value(id,key,value)
        return redirect(url_for('maintopic_store', id=id, maintopic=maintopic), 302)
    else:
        return render_index(mth,opened=id) 
