import os
import sys

from aiohttp import web

# Ugly hacky hack
# Python must be run from /src
sys.path.append('..')
from src import tkfinder

routes = web.RouteTableDef()


@routes.get('/')
async def hello(request):
    return web.Response(text="Hello, world")


@routes.get('/{name}')
async def variable_handler(request):
    name = request.match_info['name']
    character_name = tkfinder.correct_character_name(name)

    if character_name is not None:
        character = tkfinder.get_character_detail(character_name)
        return web.json_response(character)
    else:
        raise web.HTTPNotFound()
    return web.Response(
        text="Hello, {}".format(character_name))


app = web.Application()
app.add_routes(routes)
web.run_app(app, port=8080)
