# This program 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.
#
# This program 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 Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


#python modules
import os, time, stat, re, copy, time

# rdf modules -- I'll use these later
#from xml.dom.ext.reader import Sax2
#import urllib

#freevo modules
import config, menu, rc, plugin, skin, osd, util
from gui.PopupBox import PopupBox
from item import Item


#get the singletons so we get skin info and access the osd
skin = skin.get_singleton()
osd  = osd.get_singleton()

skin.register('sample', ('screen', 'title', 'info', 'plugin'))

class PluginInterface(plugin.MainMenuPlugin):
    """
    A quick plugin that does absolutely nothing but display a menu item.

    To activate, put the following in local_conf.py

    plugin.activate('sample', level=45)
	 PLUGIN_SAMPLE = [
          ('Sample Item 1', 'Sample value 1'), 
          ('Sample Item 2', 'Sample value 2') ] 
    """
    def __init__(self):
        if not hasattr(config, 'PLUGIN_SAMPLE'):
            self.reason = 'PLUGIN_SAMPLE not defined'
            return
        plugin.MainMenuPlugin.__init__(self)
        
    def config(self):
        return [('PLUGIN_SAMPLE',
                 [ ('Sample Item 1', 'Sample value 1'), 
                   ('Sample Item 2', 'Sample value 2') ],
                   'sample data')]

    def items(self, parent):
        return [ SampleMainMenuItem(parent) ]

class SampleMainMenuItem(Item):
    """
    this is the item for the main menu and creates the submenu.
    """
    def __init__(self, parent):
        Item.__init__(self, parent, skin_type='sample')
        self.name = _('Sample Main Menu')

    def actions(self):
        """
        return a list of actions for this item
        """
        items = [ ( self.create_sub_menu , _('Sample Sub Menu' )) ]
        return items
 
    def create_sub_menu(self, arg=None, menuw=None):
        sample_menu_items = []

        for item in config.PLUGIN_SAMPLE:
            sample_menu_item = SampleItem(self)
            sample_menu_item.name = item[0]
            sample_menu_item.value = item[1]
            sample_menu_items += [ sample_menu_item ]

        sample_menu_items += [menu.MenuItem(_('Static Sample Menu Item'),
                 menuw.goto_prev_page, 0)]

        sample_menu = menu.Menu(_('Sample Sub Menu'), sample_menu_items)
        menuw.pushmenu(sample_menu)
        menuw.refresh()

class SampleItem(Item):
    """
    Item for the sub menu
    """
    def __init__(self, parent):
        Item.__init__(self, parent)
        self.value        = ''

    def actions(self):
        """
        return a list of actions for this item
        """
        items = [ ( self.show , _('Show Menu Entries') ) ]
        return items

    def show(self, arg=None, menuw=None):
        popup = PopupBox(text=_('Menu value:\n %s' % self.value))
        popup.show()
        time.sleep(5)
        popup.destroy()
