import os
from importlib import import_module

from flask import Flask, url_for

BLUEPRINTS = ['announce']


def get_config():
  env_config = os.getenv('TRACKPY_SETTINGS_MODULE',
                         'trackpy.settings.production')
  return import_module(env_config)


def create_app(config=None):
  app = Flask(__name__)

  config = get_config()
  app.config.from_object(config)

  app = register_endpoints(app)

  @app.errorhandler(404)
  def page_not_found(e):
    import urllib
    output = ""
    for rule in app.url_map.iter_rules():

        options = {}
        for arg in rule.arguments:
            options[arg] = "[{0}]".format(arg)

        methods = ','.join(rule.methods)
        url = url_for(rule.endpoint, **options)
        line = urllib.unquote("{:50s} {:20s} {}".format(rule.endpoint, methods, url))
        line = "<strong>%s</strong> %s %s" % (rule.endpoint, methods, urllib.unquote(url))
        output += "<li>" + line + "</li>"

    return """
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>

<h3>Current routes:</h3>
<ul>
%s
</ul>
    """ % output, 404

  return app


def register_endpoints(app):
  for module in BLUEPRINTS:
    module = import_module('%s.api' % module)

    app.register_blueprint(module.endpoint)

  return app
