# -*- coding: utf-8 -*-
"""
Created on May 04, 2014

@author: Tyranic-Moron
"""
from twisted.plugin import IPlugin
from desertbot.moduleinterface import IModule
from desertbot.modules.commandinterface import BotCommand
from zope.interface import implementer

from desertbot.message import IRCMessage
from desertbot.response import IRCResponse, ResponseType

from twisted.words.protocols.irc import assembleFormattedText, attributes as A


@implementer(IPlugin, IModule)
class Rainbow(BotCommand):
    def triggers(self):
        return ['rainbow', 'rrainbow']

    def help(self, query):
        return 'rainbow <text> - outputs the specified text with rainbow colours; rrainbow uses background colours'

    colours = [assembleFormattedText(A.fg.lightRed['']),
               #assembleFormattedText(A.fg.orange['']),
               assembleFormattedText(A.fg.yellow['']),
               assembleFormattedText(A.fg.lightGreen['']),
               assembleFormattedText(A.fg.lightCyan['']),
               assembleFormattedText(A.fg.lightBlue['']),
               assembleFormattedText(A.fg.lightMagenta['']),
               ]
    
    bgcolours = [assembleFormattedText(A.bg.red['']),
                 assembleFormattedText(A.bg.orange['']),
                 assembleFormattedText(A.bg.yellow['']),
                 assembleFormattedText(A.bg.green['']),
                 assembleFormattedText(A.bg.cyan['']),
                 assembleFormattedText(A.bg.blue['']),
                 assembleFormattedText(A.bg.magenta['']),
                 ]

    def execute(self, message: IRCMessage):
        if len(message.parameterList) == 0:
            return IRCResponse(ResponseType.Say, "You didn't give me any text to rainbow!", message.replyTo)

        outputMessage = u''

        if message.command == 'rainbow':
            for i, c in enumerate(message.parameters):
                outputMessage += self.colours[i % len(self.colours)] + c
        else:
            for i, c in enumerate(message.parameters):
                outputMessage += self.bgcolours[i % len(self.bgcolours)] + c

        outputMessage += assembleFormattedText(A.normal[''])

        return IRCResponse(ResponseType.Say, outputMessage, message.replyTo)


rainbow = Rainbow()

@@ -744,39 +744,361 @@
 .fg.
-lightRed%5B''%5D),%0A               #
+white%5B''%5D),%0A               assembleFormattedText(A.fg.black%5B''%5D),%0A               assembleFormattedText(A.fg.blue%5B''%5D),%0A               assembleFormattedText(A.fg.green%5B''%5D),%0A               assembleFormattedText(A.fg.lightRed%5B''%5D),%0A               assembleFormattedText(A.fg.red%5B''%5D),%0A               assembleFormattedText(A.fg.magenta%5B''%5D),%0A               
 asse
@@ -1239,32 +1239,85 @@
 ightGreen%5B''%5D),%0A
+               assembleFormattedText(A.fg.cyan%5B''%5D),%0A
                a
@@ -1496,62 +1496,513 @@
     
-%5D%0A    %0A    bgcolours = %5BassembleFormattedText(A.bg.red
+assembleFormattedText(A.fg.gray%5B''%5D),%0A               assembleFormattedText(A.fg.lightGray%5B''%5D),%0A               %5D%0A    %0A    bgcolours = %5BassembleFormattedText(A.bg.white%5B''%5D),%0A                 assembleFormattedText(A.bg.black%5B''%5D),%0A                 assembleFormattedText(A.bg.blue%5B''%5D),%0A                 assembleFormattedText(A.bg.green%5B''%5D),%0A                 assembleFormattedText(A.bg.lightRed%5B''%5D),%0A                 assembleFormattedText(A.bg.red%5B''%5D),%0A                 assembleFormattedText(A.bg.magenta
 %5B''%5D
@@ -2154,33 +2154,38 @@
 mattedText(A.bg.
-g
+lightG
 reen%5B''%5D),%0A     
@@ -2282,70 +2282,255 @@
 .bg.
-blue%5B''%5D),%0A                 assembleFormattedText(A.bg.magenta
+lightCyan%5B''%5D),%0A                 assembleFormattedText(A.bg.lightBlue%5B''%5D),%0A                 assembleFormattedText(A.bg.lightMagenta%5B''%5D),%0A                 assembleFormattedText(A.bg.gray%5B''%5D),%0A                 assembleFormattedText(A.bg.lightGray
 %5B''%5D
@@ -2749,16 +2749,24 @@
 eplyTo)%0A
+        
 %0A       
@@ -2770,37 +2770,8 @@
     
-outputMessage = u''%0A%0A        
 if m
@@ -2797,16 +2797,360 @@
 rainbow'
+:%0A            fg = True%0A        else:%0A            fg = False%0A%0A        try:%0A            colList = %5Bint(n) for n in message.parameterList%5B0%5D.split(',')%5D%0A        except ValueError:%0A            if fg:%0A                colList = %5B4,8,9,11,12,13%5D%0A            else:%0A                colList = %5B5,7,8,3,10,2,6%5D%0A%0A        outputMessage = u''%0A%0A        if fg
 :%0A      
@@ -3248,29 +3248,33 @@
 urs%5B
-i %25 len(self.colours)
+colList%5Bi %25 len(colList)%5D
 %5D + 
@@ -3396,31 +3396,33 @@
 urs%5B
-i %25 len(self.bgcolours)
+colList%5Bi %25 len(colList)%5D
 %5D + 

add ability to specify colour number list