from os import path

from bottle import response, request, redirect, route, run, view, send_file, TEMPLATE_PATH 
from waskr import log, setlogging, config_options
from waskr.database import Stats

# Fixes a call from a different path
CWD = path.abspath(__file__)
APP_CWD = path.dirname(CWD)
view_dir = APP_CWD+'/views'
TEMPLATE_PATH.append(view_dir)

# Initialize the DB with proper values
CONF = config_options()
db = Stats(CONF)

# Initialize logging
setlogging(CONF)
log.server.debug("initialized logging")


@route('/')
@view('index')
def index():
    logged_in()
    user_email = request.COOKIES.get('user')
    return dict(user_email=user_email,
            last_received=db.last_insert(),
            apps=db.apps_nodes())


@route('/application/:name')
@route('/application/:name/:minutes')
@view('stats')
def interacting(name, minutes=120):
    logged_in()
    return dict(
            app_name=name,
            time_response=db.response_time(minutes),
            requests_second=db.request_time(minutes))


@route('/login', method='POST')
@view('login')
def login_post():
    email = request.forms.get('email')
    if db.authenticate(email):
        try:
            set_cookie(email)
            return dict()
        finally:
            redirect('/')
    else:
        redirect('/login')


@route('/login', method='GET')
@view('login')
def login_get():
    return dict()


@route('/static/:filename')
def static(filename):
    send_file(filename, root=APP_CWD+'/static')


@route('/favicon.ico')
def favicon():
    send_file('favicon.ico', root='static')


@route('/flot/:filename')
def flot(filename):
    send_file(filename, root=APP_CWD+'/static/flot/')


def logged_in():
    cookie = request.COOKIES.get('logged_in')
    if cookie == 'True':
        pass
    else:
        redirect("/login")


def set_cookie(email):
    response.set_cookie('logged_in', 'True', expires=+99500)
    response.set_cookie('user', email, expires=+99500)


def main(config=CONF):
    try:
        # Make sure this is not a first run
        db.first_run()
        log.server.debug("making sure this is not a first run")

        run(host=config['web_host'], port=config['web_port'])
    except Exception, e:
        print "Couldn't start the waskr server:\n%s" % e


if __name__ == '__main__':
    main()

