import wx
import sys
import win32com.client

from types import TupleType, ListType

if not hasattr(sys, "frozen"):
    print "Generating support code ..."
    from win32com.client import gencache
    gencache.EnsureModule('{91493440-5A91-11CF-8700-00AA0060263B}', 0, 2, 6, bForDemand=1)
    print "Done."
    
from win32com.client import constants

class MakePowerPoint:

    LEFTMARGIN = 50
    TOPMARGIN = 50

    def __init__(self, configuration):
        
        self.app = win32com.client.Dispatch("Powerpoint.Application")
        
        self.config = configuration
        
        self.pres = self.app.Presentations.Add(False)

        self.sHeight = self.pres.PageSetup.SlideHeight
        self.sWidth = self.pres.PageSetup.SlideWidth

        # Background colors, default to black        
        rb = self.config.getIntValue("Background/Red", 0)
        gb = self.config.getIntValue("Background/Green", 0)
        bb = self.config.getIntValue("Background/Blue", 0)

        # Foreground colors, default to white
        rf = self.config.getIntValue("Font/Color/Red", 255)
        gf = self.config.getIntValue("Font/Color/Green", 255)
        bf = self.config.getIntValue("Font/Color/Blue", 255)
        

        colorscheme = self.pres.SlideMaster.ColorScheme
        
        colorscheme.Colors(constants.ppBackground).RGB = rgb(rb, gb, bb)
        colorscheme.Colors(constants.ppForeground).RGB = rgb(rf, gf, bf)

    def addSong(self, text, heading=None):

        newSlide = self.pres.Slides.Add(self.pres.Slides.Count + 1, constants.ppLayoutText)
        
        placeholder = newSlide.Shapes.Placeholders(2)#the text placeholder
        
        placeholder.Left = self.LEFTMARGIN
        placeholder.Top = self.TOPMARGIN
        placeholder.Width = self.sWidth - 2*self.LEFTMARGIN
        placeholder.Height = self.sHeight - 2*self.TOPMARGIN
        
         
        #add text to slide
        if isinstance(text, basestring):
            placeholder.TextFrame.TextRange.Text = text
        elif type(text) in (TupleType, ListType):
            #strip empty lines 
            for i in range(len(text)):
                if text[i].strip() != "":
                    text = text[i:]
                    break

            placeholder.TextFrame.TextRange.Text = "\r\n".join(text)

        # XXX Dirty hack for a "title"
        p2  = newSlide.Shapes.Placeholders(1)
        p2.Visible = False  #Don't need to show the title placeholder
        #p2.Width = 0
        #p2.Height = 0

        if heading is not None:
            p2.TextFrame.TextRange.Text = heading
        elif text:
            p2.TextFrame.TextRange.Text = text[0]
        else:
            p2.TextFrame.TextRange.Text = "<blank>"

        
        ## Paragraph properties
        paragraph = placeholder.TextFrame.TextRange.ParagraphFormat
        
        paragraph.Alignment = constants.ppAlignLeft
        paragraph.WordWrap = False
        
        #Line spacing
        paragraph.SpaceWithin = 1.5
        
        #no bullets
        paragraph.Bullet.Type = constants.ppBulletNone
        
        ##Font properties
        font = placeholder.TextFrame.TextRange.Font

        font.Bold = (self.config.getIntValue("Font/Weight", wx.NORMAL) == wx.BOLD)
        font.Name = self.config.getValue("Font/Name", "Arial Black")
        font.Size = self.config.getIntValue("Font/Size", 20)

    def savePresentation(self, name):
        self.pres.SaveAs(name)

    def __del__(self):
        self.app.Quit()
        self.app = None


####################################
# helper functions

def rgb(r, g, b):
    """Fix a Microsoft? RGB value, seems right, but not fully tested"""
    return r | g << 8 | b << 16

