# This file is part of beets.
# Copyright 2013, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""A Web interface to beets."""
from beets.plugins import BeetsPlugin
from beets import ui
from beets import util
import beets.library
import flask
from flask import g
import os


# Utilities.

def _rep(obj, expand=False):
    """Get a flat -- i.e., JSON-ish -- representation of a beets Item or
    Album object. For Albums, `expand` dictates whether tracks are
    included.
    """
    out = dict(obj)

    if isinstance(obj, beets.library.Item):
        del out['path']

        # Get the size (in bytes) of the backing file. This is useful
        # for the Tomahawk resolver API.
        try:
            out['size'] = os.path.getsize(util.syspath(obj.path))
        except OSError:
            out['size'] = 0

        return out

    elif isinstance(obj, beets.library.Album):
        del out['artpath']
        if expand:
            out['items'] = [_rep(item) for item in obj.items()]
        return out


# Flask setup.

app = flask.Flask(__name__)

@app.before_request
def before_request():
    g.lib = app.config['lib']


# Items.

@app.route('/item/<int:item_id>')
def single_item(item_id):
    item = g.lib.get_item(item_id)
    return flask.jsonify(_rep(item))

@app.route('/item/')
def all_items():
    with g.lib.transaction() as tx:
        rows = tx.query("SELECT id FROM items")
    all_ids = [row[0] for row in rows]
    return flask.jsonify(item_ids=all_ids)

@app.route('/item/<int:item_id>/file')
def item_file(item_id):
    item = g.lib.get_item(item_id)
    response = flask.send_file(item.path, as_attachment=True,
                               attachment_filename=os.path.basename(item.path))
    response.headers['Content-Length'] = os.path.getsize(item.path)
    return response

@app.route('/item/query/<path:query>')
def item_query(query):
    parts = query.split('/')
    items = g.lib.items(parts)
    return flask.jsonify(results=[_rep(item) for item in items])


# Albums.

@app.route('/album/<int:album_id>')
def single_album(album_id):
    album = g.lib.get_album(album_id)
    return flask.jsonify(_rep(album))

@app.route('/album/')
def all_albums():
    with g.lib.transaction() as tx:
        rows = tx.query("SELECT id FROM albums")
    all_ids = [row[0] for row in rows]
    return flask.jsonify(album_ids=all_ids)

@app.route('/album/query/<path:query>')
def album_query(query):
    parts = query.split('/')
    albums = g.lib.albums(parts)
    return flask.jsonify(results=[_rep(album) for album in albums])

@app.route('/album/<int:album_id>/art')
def album_art(album_id):
    album = g.lib.get_album(album_id)
    return flask.send_file(album.artpath)


# Artists.

@app.route('/artist/')
def all_artists():
    with g.lib.transaction() as tx:
        rows = tx.query("SELECT DISTINCT albumartist FROM albums")
    all_artists = [row[0] for row in rows]
    return flask.jsonify(artist_names=all_artists)


# Library information.

@app.route('/stats')
def stats():
    with g.lib.transaction() as tx:
        item_rows = tx.query("SELECT COUNT(*) FROM items")
        album_rows = tx.query("SELECT COUNT(*) FROM albums")
    return flask.jsonify({
        'items': item_rows[0][0],
        'albums': album_rows[0][0],
    })


# UI.

@app.route('/')
def home():
    return flask.render_template('index.html')


# Plugin hook.

class WebPlugin(BeetsPlugin):
    def __init__(self):
        super(WebPlugin, self).__init__()
        self.config.add({
            'host': u'',
            'port': 8337,
        })

    def commands(self):
        cmd = ui.Subcommand('web', help='start a Web interface')
        cmd.parser.add_option('-d', '--debug', action='store_true',
                              default=False, help='debug mode')
        def func(lib, opts, args):
            args = ui.decargs(args)
            if args:
                self.config['host'] = args.pop(0)
            if args:
                self.config['port'] = int(args.pop(0))

            app.config['lib'] = lib
            app.run(host=self.config['host'].get(unicode),
                    port=self.config['port'].get(int),
                    debug=opts.debug, threaded=True)
        cmd.func = func
        return [cmd]

@@ -1852,24 +1852,182 @@
 ll_items():%0A
+    if flask.request.args.has_key('embedded'):%0A        items = %5B_rep(item) for item in g.lib.items()%5D%0A        return flask.jsonify(items=items)%0A    else:%0A    
     with g.l
@@ -2050,32 +2050,36 @@
  as tx:%0A        
+  
+  
 rows = tx.query(
@@ -2098,32 +2098,36 @@
 ROM items%22)%0A    
+   
+ 
 all_ids = %5Brow%5B0
@@ -2137,32 +2137,36 @@
 or row in rows%5D%0A
+    
     return flask
@@ -2899,24 +2899,189 @@
 l_albums():%0A
+    if flask.request.args.has_key('embedded'):%0A        albums = g.lib.albums()%0A        return flask.jsonify(results=%5B_rep(album) for album in albums%5D)%0A    else:%0A    
     with g.l
@@ -3104,32 +3104,36 @@
  as tx:%0A        
+  
+  
 rows = tx.query(
@@ -3149,32 +3149,36 @@
 d FROM albums%22)%0A
+    
     all_ids = %5Br
@@ -3192,32 +3192,36 @@
 or row in rows%5D%0A
+    
     return flask
@@ -3241,32 +3241,33 @@
 m_ids=all_ids)%0A%0A
+%0A
 @app.route('/alb

Embed item data into web request