###
# Bleeep
###

"""
Configure plugin; takes the user through the process of setting all the
options for a given plugin.
"""

import supybot

__revision__ = "0.1"
__author__ = supybot.authors.unknown
__contributors__ = {}


import supybot.conf as conf
import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
from supybot.ircutils import bold as bold
import supybot.privmsgs as privmsgs
import supybot.registry as registry
import supybot.callbacks as callbacks


def configure(advanced):
    # This will be called by setup.py to configure this module.  Advanced is
    # a bool that specifies whether the user identified himself as an advanced
    # user or not.  You should effect your configuration by manipulating the
    # registry as appropriate.
    from supybot.questions import expect, anything, something, yn
    conf.registerPlugin('Wizard', True)


conf.registerPlugin('Wizard')
# This is where your configuration variables (if any) should go.

class Wizard(callbacks.Privmsg):
    # FIXME: Make "thread-safe" (for multiple users)
    
    def __init__(self):
        self._reset()
        callbacks.Privmsg.__init__(self)

    def _currentValue(self):
        values = self.group.getValues(fullNames=False)
        return values[self.index]

    def _askQuestion(self, irc):
        try:
            (name, value) = self._currentValue()
            try:
                irc.reply('%d. %s (%s) Current value: %s' % (self.index + 1, bold(name), value.help(), value()))
            except ValueError:
                irc.reply('%d. %s: a group. Support for this has not been implemented yet.' % (self.index + 1, bold(name)))
            return True
        except IndexError:
            return False

    def _reset(self):
        self.plugin = None
        self.group = None
        self.index = 0
        
    def reconfigure(self, irc, msg, args, plugin):
        """<plugin>

        Allows the user to reconfigure all options of a certain plugin in a
        wizard-like style.
        """
        self.plugin = plugin
        self.group = conf.supybot.plugins.get(self.plugin.name())
        self.index = 0

        irc.reply('Reconfiguring plugin %s -- use command %s to skip a question, %s to end configuring and %s to set a value' % (bold(plugin.name()), bold('next'), bold('finish'), bold('set <value>')))
        if not self._askQuestion(irc):
            irc.error('That plugin has no properties to configure')
            self._reset()
        
    reconfigure = wrap(reconfigure, ['plugin'])

    def next(self, irc, msg, args):
        """takes no arguments

        Skips to the next question in the wizard without changing the current value.
        """
        if self.plugin:
            self.index += 1
            if not self._askQuestion(irc):
                self._reset()
                irc.reply('That was the last question')
        else:
            irc.error('Not configuring a plugin right now -- use %s' % bold('reconfigure <plugin>'))

    def finish(self, irc, msg, args):
        """takes no arguments

        Ends the wizard.
        """
        if self.plugin:
            self._reset()
            irc.reply('The wizard has been cancelled')
        else:
            irc.error('Not configuring a plugin')

    def set(self, irc, msg, args, value):
        """<value>

        Changes the value for the current question and advances to the next one.
        """
        if self.plugin:
            name, property = self._currentValue()
            property.set(value)
            irc.reply('Value of %s has been set to %s' % (bold(name), value))
            self.index += 1
            if not self._askQuestion(irc):
                irc.reply('That was the last question')
                self._reset()
        else:
            irc.error('Not configuring a plugin right now -- use %s' % bold('reconfigure <plugin>'))
    set = wrap(set, ['anything'])

    def back(self, irc, msg, args):
        """takes no arguments

        Retreats one question into the wizard.
        """
        if self.plugin:
            if self.index > 0:
                self.index -= 1
            else:
                irc.reply('You are already at the first question')
            self._askQuestion(irc)
        else:
            irc.error('Not configuring a plugin right now -- use %s' % bold('reconfigure <plugin>'))
    

Class = Wizard

# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: