"""Example widgets for showing Flash content."""

from pkg_resources import resource_filename

from turbogears import url, util, widgets
from turbogears.widgets import Widget, JSLink, JSSource

this_package = util.get_package_name()
static = resource_filename(this_package, 'static')
widgets.register_static_directory(this_package, static)


class FlashWidget(Widget):
    """Embeds Adobe Flash Player content using SWFObject 2.

    We assume that you're only using one of these widgets per page.

    See http://code.google.com/p/swfobject for info on SWFObject.

    """

    params = 'name base title width height flash_vars'.split()

    object_id = 'flashwidget'
    base = '/static/flash'
    width, height = 550, 400
    flash_version = '9.0.0'
    get_flash_url = 'http://www.adobe.com/go/getflashplayer'
    get_flash_button = 'http://www.adobe.com/images/shared/' \
        'download_buttons/get_flash_player.gif'

    javascript=[JSLink(this_package, 'javascript/swfobject.js'),
        JSSource("swfobject.registerObject('flashwidget', '%s', '%s');"
            % (flash_version, url(base + '/expressInstall.swf')))]

    template = """
        <div xmlns:py="http://purl.org/kid/ns#" py:strip="True">
        <h2 py:if="title" py:content="title">Title</h2>
        <object id="%s"
            classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
        width="$width" height="$height">
        <param name="base" value="$base"/>
        <param name="movie" value="$name"/>
        <param name="play" value="true"/>
        <param name="loop" value="false"/>
        <param name="menu" value="true"/>
        <param name="quality" value="high"/>
        <param name="scale" value="showall"/>
        <param name="FlashVars" value="$flash_vars"/>
        <!--[if !IE]>-->
        <object type="application/x-shockwave-flash"
            codebase="$base" data="$name"
            width="$width" height="$height">
        <param name="base" value="$base"/>
        <param name="play" value="true"/>
        <param name="loop" value="false"/>
        <param name="menu" value="true"/>
        <param name="quality" value="high"/>
        <param name="scale" value="showall"/>
        <param name="FlashVars" value="$flash_vars"/>
        <!--<![endif]-->
        <p>&#160;</p>
        <div style="width:${width}px;">
        <a href="%s"><img src="%s"
            align="right" border="0" hspace="16" vspace="2"
            alt="Get Adobe Flash player"/></a>
        <p style="text-align:left">You need an up to date version
            of the Adobe Flash Player to view this content.</p>
        </div>
        <p>&#160;</p>
        <!--[if !IE]>-->
        </object>
        <!--<![endif]-->
        </object>
        </div>
    """ % (object_id, get_flash_url, get_flash_button)

    def __init__(self, name, title=None, flash_vars=None,
            width=None, height=None, *args, **kwargs):
        Widget.__init__(self, *args, **kwargs)
        if not '.' in name:
            name += '.swf'
        self.base = url(self.base)
        self.name = url([self.base, name])
        self.title = title
        if isinstance(flash_vars, dict):
            flash_vars = ['%s=%s' % item for item in flash_vars.items()]
        if isinstance(flash_vars, list):
            flash_vars = '&'.join(flash_vars)
        self.flash_vars = flash_vars or None
        if width:
            self.width = width
        if height:
            self.height = height


class MP3Widget(FlashWidget):
    """Embeds MP3 content using MP3 Player and SWFObject 2.

    See http://flash-mp3-player.net for info on MP3 Player.

    """

    object_id = 'mp3widget'
    name = 'player_mp3_multi.swf'
    width, height = 200, 100

    def __init__(self, title=None, flash_vars=None,
            width=None, height=None, *args, **kwargs):
        if not flash_vars:
            flash_vars = {}
        if 'width' in flash_vars:
            if not width:
                width = flash_vars['width']
        elif width:
            flash_vars['width'] = width
        if 'height' in flash_vars:
            if not height:
                height = flash_vars['height']
        elif height:
            flash_vars['height'] = width
        FlashWidget.__init__(self, self.name, title, flash_vars,
            width, height, *args, **kwargs)

