# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Emesene is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    emesene is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

VERSION = '0.2'
import random
from Plugin import Plugin

class MainClass(Plugin):

    def __init__(self, controller, msn):
        Plugin.__init__(self, controller, msn)

        self.description = _('Show random numbers in the conversation. "/help dice" for usage.')
        self.authors = {'LE COZ Florent (aka louiz)': 'louizatakk AT gmail DOT com'}
        self.website = 'None'
        self.displayName = _('Dices')
        self.name = 'Dices'
        self.Slash = controller.Slash
        self.enabled = False

    def start(self):
        self.Slash.register('dice', self.rolls_dice, _('Dice:\nformat : xdy\nRolls x dices, each dice has y faces'))
        self.enabled = True

    def rolls_dice(self, slash_action):
        '''rolls dices : it sends random numbers, defined by the xdy format'''
        data = slash_action.getParams()
        params = data.split('d')
        if len(params) != 2:
            return
        try:
            x = int(params[0])
            nb = int(params[1])
        except ValueError:
            return
        data += " :\n"
        if (nb <= 0):
            return
        for i in range(x):
            data += str(random.randint(1, nb)) + '  '
        slash_action.outputText(data, True)

    def stop(self):
        self.Slash.unregister('dice')
        self.enabled = False

    def check(self):
        return (True, 'Ok')

