#!/usr/bin/python2

import common
import flask
import pystache
import sqlite3

app = flask.Flask(__name__)

MAIN = '''
<!DOCTYPE html>
<html>
  <head>
    <title>shitbird</title>
    <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8;"/>
    <style type="text/css">
      body {
        font-family: "Helvetica", "Arial", sans-serif;
        font-size: 18pt;
        background-color: #ffffff;
      }
      .contents {
        width: 80%;
        margin-left: auto;
        margin-right: auto;
        padding: 40px;
      }
      .tweet {
        border-top-style: solid;
        border-bottom-style: solid;
        border-width: 1px;
        padding: 20px;
        margin-top: 20px;
        margin-bottom: 20px;
        width: 75%;
        margin-left: auto;
        margin-right: auto;
      }
    </style>
  </head>
  <body>
    <div class="contents">{{{body}}}</div>
  </body>
</html>
'''

TWEET = '''
<div class="tweet">
  <div class="text">{{tweet}}</div>
  <div class="author">
    <a href="http://twitter.com/{{handle}}">{{name}}</a>
  </div>
  <div class="link">
    <a href="http://twitter.com/{{handle}}/status/{{id}}">{{date}}</a>
  </div>
</div>
'''

def render_body():
    return ''.join((pystache.render(TWEET, t)
                    for t in common.get_tweets()))

@app.route("/")
def index():
    for tweet in common.get_tweets():
        return pystache.render(MAIN,
                               {'body': render_body()})

if __name__ == '__main__':
    app.run()
