from bottle import Bottle, route, run, template, get, post, request, static_file, response, os
app = Bottle()

# @app.route('/hello')


@app.route('/forum')
def display_forum():
    forum_id = request.query.id
    page = request.query.page or '1'
    return template('Forum ID: {{id}} (page {{page}})', id=forum_id, page=page)


@app.route('/download/<filename:path>')
def download(filename):
    return static_file(filename, root='./', download=filename)


@app.route('/iso')
def get_iso():
    response.charset = 'ISO-8859-15'
    return u'This will be sent with ISO-8859-15 encoding.'


@app.route('/latin9')
def get_latin():
    response.content_type = 'text/html; charset=latin9'
    return u'ISO-8859-15 is also known as latin9.'


@app.route('/')
def hello():
    return "Hello World!"


@app.route('/static/<filename>')
def server_static(filename):
    return static_file(filename, root='./')


@app.route('/static/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='./')


@app.route('/hello/<name>')
def greet(name="Stranger"):
    return template('Hello {{name}}, how are you?', name=name)


@app.route('/wiki/<pagename>')  # matches /wiki/Learning_Python
def show_wiki_page(pagename):
    return "Let's learn Python"


@app.route('/<action>/<user>')  # matches /follow/defnull
def user_api(action, user):
    return "user_api is called"


@app.route('/object/<id:int>')
def callback(id):
    assert isinstance(id, int)


@app.route('/show/<name:re:[a-z]+>')
def callback(name):
    assert name.isalpha()


# @route('/static/<path:path>')
# def callback(path):
#     return static_file(path, ...)


# @get('/login') # or @route('/login')
@app.route('/login')
def login():
    return '''
        <form action="/login" method="post">
            Username: <input name="username" type="text" />
            Password: <input name="password" type="password" />
            <input value="Login" type="submit" />
        </form>
    '''


# @post('/login') # or @route('/login', method='POST')
@app.route('/login', method='POST')
def do_login():
    username = request.forms.get('username')
    password = request.forms.get('password')
    if check_login(username, password):
        return "<p>Your login information was correct.</p>"
    else:
        return "<p>Login failed.</p>"


def check_login(username, password):
    return True


@app.route('/upload')
def upload():
    return '''
        <form action="/upload" method="post" enctype="multipart/form-data">
            Category:      <input type="text" name="category" />
            Select a file: <input type="file" name="upload" />
            <input type="submit" value="Start upload" />
        </form>
    '''


@app.route('/upload', method='POST')
def do_upload():
    # category = request.forms.get('category')
    upload = request.files.get('upload')
    name, ext = os.path.splitext(upload.filename)
    if ext in ('.pdf', '.apk', '.exe'):
        if ext == '.pdf':
            pass  # TODO(Fengjiao Wang <yonah142857@gmail.com>): Please ADD your prediction logic here
        elif ext == '.apk':
            pass  # TODO(Guobin Huang <guobin.bd@gmail.com>): Please ADD your prediction logic here
        elif ext == '.exe':
            pass  # TODO(Deyuan Li <deyuan321123@gmail.com>): Please ADD your prediction logic here
    else:
        return 'File extension NOT allowed.'
    # save_path = get_save_path_for_category(category)
    # upload.save(save_path + '/' + name)  # appends upload.filename automatically

    if ext in ('.pdf', '.apk', '.exe'):
        if ext == '.pdf':
            # TODO(Fengjiao Wang <yonah142857@gmail.com>): ADD your prediction logic here
            return 'Data Analysis from Fengjiao'
        elif ext == '.apk':
            # TODO(Guobin Huang <guobin.bd@gmail.com>): ADD your prediction logic here
            return 'Data Analysis from Guobin'
        elif ext == '.exe':
            # TODO(Deyuan Li <deyuan321123@gmail.com>): ADD your prediction logic here
            return 'Data Analysis from Deyuan'
    else:
        return 'File NOT handled'


def get_save_path_for_category(category):
    return './' + category


run(app, host="localhost", port=8080)  # Single-thread
# run(app, host="localhost", port=8080, server='paste') # Multi-thread
# run(app, host="localhost", port=8080, server='cherrypy') # NOT working currently
'''
@route('/hello/<name>')
def index(name):
    return template('<b>Hello {{name}}</b>!', name=name)
run(host='localhost', port=8080)
'''
