# me2.plugs.wave/gadget.py
#
#

## me2 imports

from me2.lib.commands import cmnds
from me2.lib.examples import examples
from me2.lib.persist import PlugPersist

gadgeturls = PlugPersist('gadgeturls')

gadgeturls.data['gadget'] = 'https://botme2.appspot.com/gadget.xml'
gadgeturls.data['poll'] = 'https://botme2.appspot.com/poll.xml'
gadgeturls.data['iframe'] = 'https://botme2.appspot.com/iframe.xml'
gadgeturls.data['loadiframe'] = 'https://botme2.appspot.com/loadiframe.xml'

def loadroot(event, url):
    if event.rootblip:
        from waveapi import element
        event.rootblip.append(element.Gadget(url))
        return True
    else:
        event.reply("can't find root blip.")
        return False

def load(event, url):
    if event.blip:
        from waveapi import element
        event.blip.append(element.Gadget(url))
        return True
    else:
        event.reply("can't find root blip.")
        return False

def handle_gadgetload(bot, event):
    if event.bottype != "wave":
        event.reply("this command only works in google wave.");
        return

    if not event.rest:
        event.missing('<gadgetname>')
        return

    try:
        url = gadgeturls.data[event.rest]
        if load(event, url):
            event.reply('loaded %s' % url)
    except KeyError:
        event.reply("we don't have a url for %s" % event.rest)

cmnds.add("gadget-load", handle_gadgetload, 'USER')
examples.add("gadget-load", "load a gadget into a blip", "gadget-load")

def handle_gadgetloadroot(bot, event):
    if event.bottype != "wave":
        event.reply("this command only works in google wave.");
        return

    if not event.rest:
        event.missing('<gadgetname>')
        return

    try:
        url = gadgeturls.data[event.rest]
        if loadroot(event, url):
            event.reply('loaded %s' % url)
    except KeyError:
        event.reply("we don't have a url for %s" % event.rest)

cmnds.add("gadget-loadroot", handle_gadgetloadroot, 'USER')
examples.add("gadget-loadroot", "load a gadget into the root blip", "gadget-loadroot")

def handle_gadgetiframe(bot, event):
    if event.bottype != "wave":
        event.reply("this command only works in google wave.");
        return

    if not event.rest:
        event.missing('<url>')
        return

    try:
        url = gadgeturls.data['loadiframe'] + "?&iframeurl=%s" % event.rest
        event.reply('loading %s' % url)
        load(event, url)
    except KeyError:
        event.reply("we don't have a iframe url")

cmnds.add("gadget-iframe", handle_gadgetiframe, 'USER')
examples.add("gadget-iframe", "load a url into a iframe", "gadget-iframe")

def handle_gadgetaddurl(bot, event):

    try:
        (name, url) = event.args
    except ValueError:
        event.missing('<name> <url>')
        return

    if not gadgeturls.data.has_key(name):
        gadgeturls.data[name] = url
        gadgeturls.save()
    else:
        event.reply("we already have a %s gadget" % name)

cmnds.add("gadget-addurl", handle_gadgetaddurl, 'USER')
examples.add("gadget-addurl", "store a gadget url", "gadget-addurl me2 https://botme2.appspot.com/iframe.xml")

def handle_gadgetdelurl(bot, event):

    try:
        (name, url) = event.args
    except ValueError:
        event.missing('<name> <url>')
        return

    gadgeturls.data[name] = url
    gadgeturls.save()

cmnds.add("gadget-delurl", handle_gadgetdelurl, 'OPER')
examples.add("gadget-delurl", "delete a gadget url", "gadget-delurl mygadget")

def handle_gadgetlist(bot, event):
    result = []

    for name, url in gadgeturls.data.iteritems():
        result.append("%s - %s" % (name, url))

    event.reply("available gadgets: ", result)

cmnds.add("gadget-list", handle_gadgetlist, 'USER')
examples.add("gadget-list", "list known gadget urls", "gadget-list")


def handle_gadgetconsole(bot, event):
    if event.bottype != "wave":
        event.reply("this command only works in google wave.");
        return

    wave = event.chan
    if wave.data.feeds and wave.data.dotitle:
        event.set_title("BOTME2 - %s #%s" % (" - ".join(wave.data.feeds), str(wave.data.nrcloned)))

    from waveapi import element

    #url = gadgeturls.data['loadiframe'] + "?&iframeurl=https://botme2.appspot.com"
    #event.reply('loading %s' % url)
    event.append("loading ...\n")
    #load(event, "http://botme2.appspot.com/iframe.xml")

    event.append(
        element.Gadget('http://botme2.appspot.com/console.xml?gadget_cache=0'))

cmnds.add("gadget-console", handle_gadgetconsole, 'OPER')
examples.add("gadget-console", "load the console gadget", "gadget-console")
