#!/usr/bin/python

import mitsfs

d=mitsfs.dexdb(dsn=mitsfs.database_dsn)

((inventory_code, inventory_id, inventory_desc),) = \
    d.cursor.execute('select inventory_code, inventory_id, inventory_desc'
                     ' from inventory order by inventory_stamp desc limit 1')

print '%s (%s)' % (inventory_desc, inventory_code)
print """
Ok, make sure that the box/stack/agglomeration you're putting these
books into is labelled somehow.  If it's one of our new boxes, perhaps
consider labelling it with some sort of hang tag/punch card thingy.

Now, what's written on the label?
"""

label = mitsfs.read('Label: ')

print """
Ok, now I'm going to ask for authors, titles, "formats", and the
preexistence of an orange sticker.  Tab completion works, and is
surprisingly effective.  (If you give it partial authors and title, it
will ask you to pick a 'dex entry.  The format should be one of the
following:
"""

formats = dict(d.cursor.execute('select format, format_id from format'))
for item  in sorted(d.cursor.execute('select format, format_description from format')):
    print '%-4s %s' % item

print """
Even if it has a sticker on it with a shelfcode, I'm still looking for
what it is, not where we keep it, so please make the decision by its
rigidity, long spine dimension, and bookitude.

Once you're done, just leave a full set of prompts blank.  If you exit
prematurely, just restart the program and give the same answer to the
label question.  If you make a mistake, you can drop the last entry
(but only the last) you sucessfully put in by typing oops at the
Format? prompt.  If you notice an error too late even for that, don't
stress about it, just mention it to the entity running the inventory.

(If you find an object that the 'dex doesn't seem to know about, set
it aside)
"""

last_title = None
last_format = None
last_experienced = False
last_id = None

title = None

def out(title, format=None):
    if format:
        fc = [format]
    else:
        fc = []
    return '<'.join([title.authortxt, title.titletxt, title.seriestxt] + fc)
while True:
    print
    title = mitsfs.specify(d, preload=title)
    if title:
        print 'selected',out(title)

    format = mitsfs.read('Format? ', callback=formats.iterkeys, history='format').strip().upper()

    if not title and not format:
        break

    if format == 'OOPS':
        if last_id is None:
            print 'There is nothing to undo...'
            continue
        if not last_experienced:
            print """I'm going to ask you just once to be clear on whether we both agree on
how the "oops" feature works.  I'm about to delete:"""
            print out(last_title, last_format)
            if not mitsfs.readyes('Are you sure?'):
                continue
            last_experienced = True
        print 'Undoing', out(last_title, last_format)
        d.cursor.execute('delete from inventory_found where inventory_found_id=%s', (last_id,))
        d.db.commit()
    elif format not in formats:
        print 'Unknown format',format
        continue
    elif title:
        # this flow is wrong, but...
        orange = mitsfs.readyes('Orange sticker? ', history='orange')

        # do the deed
        d.cursor.execute('insert into inventory_found(inventory_id, title_id, format_id, found_tag, orange)'
                         ' values (%s, %s, %s, %s, %s)',
                         (inventory_id, title.title_id, formats[format], label, orange))
        (last_id,) = d.cursor.execute('select last_value from id_seq')
        d.db.commit()
        print 'entered',out(title, format)
        last_title = title
        last_format = format
        title = None
