# plugs/lists.py
#
#

""" maintain lists """

from gozerbot.pdol import Pdol
from gozerbot.datadir import datadir
from gozerbot.commands import cmnds
from gozerbot.plughelp import plughelp
from gozerbot.examples import examples
import os

plughelp.add('lists', 'maintain lists')

lists = Pdol(datadir + os.sep + 'lists')

def handle_oldlists(bot, ievent):
    """ oldlists <listname> [',' <item>] """
    if not ievent.rest:
        ievent.missing("<listname> [',' <item>]")
        return
    try:
        listname, item = ievent.rest.split(',', 1)
    except ValueError:
        teller = 0
        result = []
        listname = ievent.rest.strip()
        l = lists[listname]
        if not l:
            ievent.reply('no %s list available' % listname)
            return
        for i in lists[listname]:
            result.append("%s) %s" % (teller, i))
            teller += 1
        ievent.reply(result)
        return
    listname = listname.strip().lower()
    item = item.strip()
    if not listname or not item:
        ievent.missing("<listname> [',' <item>]")
        return
    lists[listname] = item
    lists.save()
    ievent.reply('%s added to %s list' % (item, listname))

cmnds.add('oldlists', handle_oldlists, 'USER')
examples.add('oldlists', "oldlists <listname> [',' <item>] .. show content \
of list or add item to list", '1) oldlists bla 2) oldlists bla, mekker')

def handle_oldlistsdel(bot, ievent):
    """ oldlist-del <listname> ',' <listofnrs> .. remove items with indexnr 
        from list """
    if not ievent.rest:
        ievent.missing('<listname>, <listofnrs>')
        return
    try:
        listname, listofnrs = ievent.rest.split(',', 1)
    except ValueError:
        ievent.missing('<listname>, <listofnrs>')
        return
    listname = listname.strip().lower()
    l = lists[listname]
    if not l:
        ievent.reply('no %s list avaiable' % listname)
        return
    try:
        nrs = []
        for i in listofnrs.split():
            nrs.append(int(i))
    except ValueError:
        ievent.reply('%s is not an integer' % i)
        return
    nrs.sort()
    nrs.reverse()
    itemsdeleted = 0
    for i in range(len(lists.data[listname])-1, -1 , -1):
        if i in nrs:
            try:
                del lists.data[listname][i]
                itemsdeleted += 1
            except IndexError:
                pass
    if not lists[listname]:
        del lists[listname]
        ievent.reply('%s list removed' % listname)
        lists.save()
        return
    lists.save()
    ievent.reply('%s item(s) removed from %s list' % (itemsdeleted, listname))

cmnds.add('oldlists-del', handle_oldlistsdel, 'USER')
examples.add('oldlists-del', "oldlists-del <listname> ',' <listofnrs> .. \
remove items with indexnr from list", '1) oldlist-del mekker , 1 2) \
oldlist-del mekker , 0 3 6')

def handle_oldlistsshow(bot, ievent):
    """ show avaiable lists """
    l = lists.data.keys()
    if not l:
        ievent.reply('no lists available')
        return
    else:
        ievent.reply(' .. '.join(l))

cmnds.add('oldlists-show', handle_oldlistsshow, 'USER')
examples.add('oldlists-show', 'show available global lists' , 'oldlists-show')
