# Copyright (c) 2010 gocept gmbh & co. kg
# See also LICENSE.txt

from zeit.cms.i18n import MessageFactory as _
import gocept.lxml.interfaces
import grokcore.component
import lxml.objectify
import zeit.cms.content.interfaces
import zeit.cms.interfaces
import zeit.content.article.edit.block
import zeit.content.article.edit.interfaces
import zeit.content.image.interfaces
import zeit.edit.block
import zeit.edit.interfaces
import zope.component


class Image(zeit.edit.block.Element,
            grokcore.component.MultiAdapter):

    grokcore.component.adapts(
      zeit.content.article.edit.interfaces.IEditableBody,
      gocept.lxml.interfaces.IObjectified)
    grokcore.component.implements(
        zeit.content.article.edit.interfaces.IImage)
    grokcore.component.provides(
        zeit.content.article.edit.interfaces.IImage)

    type = 'image'
    grokcore.component.name(type)

    layout = zeit.cms.content.property.ObjectPathAttributeProperty(
        '.', 'layout', zeit.content.article.edit.interfaces.IImage['layout'])

    @property
    def image(self):
        unique_id = self.xml.get('src')
        return zeit.cms.interfaces.ICMSContent(unique_id, None)

    @image.setter
    def image(self, value):
        node = zope.component.getAdapter(
            value, zeit.cms.content.interfaces.IXMLReference,
            name='image')
        # We have to save a few attributes before we replace the whole node
        name = self.__name__
        layout = self.layout
        self.xml.getparent().replace(self.xml, node)
        self.xml = node
        # Restore saved attributes
        self.__name__ = name
        self.layout =  layout
        self._p_changed = True


class Factory(zeit.content.article.edit.block.BlockFactory):

    element_type = Image.type
    title = _('Image')
    grokcore.component.name(element_type)

    def get_xml(self):
        return lxml.objectify.E.image()


@grokcore.component.adapter(zeit.content.article.edit.interfaces.IEditableBody,
                            zeit.content.image.interfaces.IImage)
@grokcore.component.implementer(zeit.edit.interfaces.IElement)
def factor_image_block_from_image(body, image):
    block = Factory(body)()
    block.image = image
    return block

@@ -472,100 +472,35 @@
 ock.
-Element,%0A            grokcore.component.MultiAdapter):%0A%0A    grokcore.component.adapts(%0A     
+SimpleElement):%0A%0A    area =
  zei
@@ -550,52 +550,8 @@
 Body
-,%0A      gocept.lxml.interfaces.IObjectified)
 %0A   
@@ -643,143 +643,22 @@
     
-grokcore.component.provides(%0A        zeit.content.article.edit.interfaces.IImage)%0A%0A    type = 'image'%0A    grokcore.component.name(type)
+type = 'image'
 %0A%0A  

use the elment grokker for image