import grok
from zope.component import getUtility, getMultiAdapter, getAdapter
from zope import interface, component
from zope.app.publisher.browser import menu
from zope.app.publisher.interfaces.browser import IBrowserMenu, IMenuItemType
from zope.publisher.browser import TestRequest, BrowserRequest
from zope.publisher.interfaces.browser import IBrowserRequest

from infos import Infos

from pprint import pformat

class IMenubar(interface.Interface):
	pass

class Menubar(menu.BrowserMenu):
	grok.provides(IBrowserMenu)
	grok.name('menubar')
	id = u'menuitem'

 	def __init__(self, **kw):
		self.kw = kw

	def __call__(self, id, title, description):
		super(menu.BrowserMenu, self).__init__(id, title, description)
		self.id = id

grok.global_utility(Menubar)

class IMenutest(interface.Interface):
	""" Interface
	"""

class Menutest(grok.Application, grok.Container):
	grok.implements(IMenutest)

class IEditMenu(interface.Interface):
	"""
	"""

class EditMenu(grok.Model):
	grok.implements(IEditMenu)

class IAMenuItem(interface.Interface):
	"""
	"""

class AMenuItem(menu.BrowserMenuItem):
	grok.implements(IAMenuItem, IMenuItemType)
	grok.provides(IMenuItemType)
	grok.name('menuitem')

 	def __init__(self, **kw):
		self.kw = kw

	def __call__(self, context, request):
		super(menu.BrowserMenuItem, self).__init__(context, request)

grok.global_utility(AMenuItem)

class AMenuItemAdapter(grok.MultiAdapter):
	grok.context(Menutest)
	grok.adapts(IEditMenu, IBrowserRequest)
	grok.name('amenuitemadapter')
	grok.implements(IMenuItemType)
	title = 'hello'
	description = 'toto'

	def __init__(self, **kw):
		self.kw = kw

	def __call__(self, editmenu, testrequest):
		self.editmenu = editmenu
		self.testrequest = testrequest

class Index(grok.View):
	grok.context(Menutest)
	a_menu =  Menubar()
	a_menu('toto', 'Titre toto', 'Toto est de retour')
	a_menu_item = AMenuItem()
	a_menu_item(EditMenu(), TestRequest())
	a_menu_item_adapter = AMenuItemAdapter()
	a_menu_item_adapter(EditMenu, TestRequest)

	def render(self):
		self.form = getMultiAdapter((EditMenu, TestRequest), IMenuItemType, name='amenuitemadapter')
		return self.form.title

# return pformat(menu.getMenu('menubar', EditMenu(), TestRequest()))
