#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Ett skript som genererar input om mina samlingar för mina bordspels
sidor"""

from blamlib import web
import sys

print "Generating boardgame list..."
output_file = "input/boardgames/collection/index.xhtml"
output = "<h2>My Boardgame Collection</h2>\n"
output += '<p>This page show what I have currently in my game collection (at least registered at <veb link "bgg"/>). Its generated with a little python <a href="bggexport.py">hack</a> using <veb link "blamlib"/>. And I guess it is currently the most ugly part of my homepage :)</p><p><b>Current collection</b></p>'

bgg = web.BGG( "blambi" )
bgg.get_xml()

rated = lambda x: x == None and "Not rated yet" or x

games = bgg.get_owned()
games += bgg.get_owned( False )

is_oddrow = False

# This need some preprocessing since we need to find the expansions.
def contains( title, part ):
    """Returns true if it just contains the name, not being equal etc"""
    if title == part: return False
    elif title.find( part ) != -1: return True
    return False

expansions = dict()
expansions['Arkham Horror'] = filter(
    lambda x: x['name'].startswith( 'Arkham Horror:' ), games )
expansions['Carcassonne'] = filter(
    lambda x: x['name'].startswith( 'Carcassonne:' ), games )
expansions['The Haunting House'] = filter(
    lambda x: x['name'].startswith( 'The Haunting House ' ), games )
expansions['Zombies!!! (Second Edition)'] = filter(
    lambda x: ( x['name'].startswith( 'Zombies!!! ' ) and
                x['name'][11].isdigit() ), games )
expansions['Munchkin'] = filter(
    lambda x: contains( x['name'], 'Munchkin' ), games )
    #lambda x: x['name'].startswith( 'Munchkin ' ), games )
expansions['Fluxx'] = filter( lambda x: contains( x['name'], 'Fluxx' ), games )

expansions['Last Night on Earth: The Zombie Game'] = filter(
    lambda x: x['name'] != "Last Night on Earth: The Zombie Game" and contains( x['name'], 'Last Night on Earth: ' ), games )

# remove expansions
games = filter( lambda x: not x['name'].startswith( 'Arkham Horror:' ) and
                not x['name'].startswith( 'Carcassonne:' ) and
                not x['name'].startswith( 'The Haunting House ' ) and
                #not x['name'].startswith( 'Munchkin ' ) and
                not contains( x['name'], 'Munchkin' ) and
                not contains( x['name'], 'Fluxx' ) and
                not ( x['name'] != "Last Night on Earth: The Zombie Game" and contains( x['name'], 'Last Night on Earth: ' ) )
                , games )

# zombies!!! are special..
games = filter( lambda x: not ( x['name'].startswith( 'Zombies!!! ' ) and
                                x['name'][11].isdigit() ), games )


for game in games:
    if is_oddrow:
        output += '<div class="oddrow">'
        
    output += '<a href="%s">%s</a>\n<br />Rating: %s\n<br />' %(
        game['url'], game['name'], rated( game['rating'] ) )

    # Any game with expansions?
    if expansions.has_key( game['name'] ):
        output += '<div class="list-expansions">'
        
        for expansion in expansions[ game['name'] ]:
            print expansion
            output += '<a href="%s">%s</a>: Rating: %s\n<br />' %(
                expansion['url'], expansion['name'],
                rated( expansion['rating'] ) )

        output += '</div>'

    if is_oddrow:
        output += '</div>'

    is_oddrow = not is_oddrow
   
output += "<p><b>Wishlist?</b><br />This is a list of games I'm sort of intrested in buying. They are ordered after how intresting I think they are.</p>\n"

wanted = bgg.get_wanted()
wanted.sort( lambda x, y: x['wishlist_level'] - y['wishlist_level'] )

for game in wanted:
    if game['wishlist_level'] != 5:
        if is_oddrow:
            output += '<div class="oddrow">'

        if game['ordered']:
            output += '<a href="%s">%s</a>\n<br /><b>Ordered</b>\n' %(
                game['url'], game['name'] )
        else:
            output += '<a href="%s">%s</a>\n<br /><i>%s</i>\n' %(
                game['url'], game['name'],
                web.wishlist_helper[ game['wishlist_level'] ] )

        
        if is_oddrow:
            output += '</div>'

        is_oddrow = not is_oddrow
            
outfile = open( output_file, 'w' )
outfile.write( output.encode( 'utf-8' ) )
outfile.close()
print "Wrote %s" % output_file

