from zope.interface import implementsOnly, implements, Interface
from zope.component import adapts, getGlobalSiteManager

gsm = getGlobalSiteManager()

from graph_ZCA.base import BaseAdapter, BaseComponent, BaseFactory
from graph_ZCA.interfaces import ITickLabelAdapter, IAxis, IViewBox, IImage


class TickLabelAdapter(BaseAdapter):
    """
    Generates Ticklabels for an axis
    """
    implementsOnly(ITickLabelAdapter)
    adapts(IAxis)

    def __init__(self, axis):
        """
        Gets an Object to be adapted
        """
        self.axis = axis

    def render(self):
        return map(str, range(self.axis.start, self.axis.end))

gsm.registerAdapter(TickLabelAdapter)


class AxisComponent(BaseComponent):
    """
    Represents an axis
    """

    implements(IAxis)
    adapts(Interface)

    def __init__(self, parent, orientation=None, start=None, end=None):
        super(AxisComponent, self).__init__(parent)
        self.orientation = orientation
        self.start = start
        self.end = end

    def get_tick_labels(self):
        return ITickLabelAdapter(self).render()

    def render(self):
        return '%s: ' % self.__class__.__name__ + self.orientation + ':' + ' '.join(self.get_tick_labels())

gsm.registerAdapter(AxisComponent)


class Axis(BaseFactory):

    target_interface = IAxis


class ViewBoxComponent(BaseComponent):
    """
    Drawing area of a plot. THe drawing ares may have an unlimited number of axis
    """
    implements(IViewBox)
    adapts(Interface)


    def __init__(self, parent):
        super(ViewBoxComponent, self).__init__(parent)



    def render(self):
        result = []
        for item in self.items:
            result.append(item.render())

        return '\n'.join(result)


gsm.registerAdapter(ViewBoxComponent)


class ViewBox(BaseFactory):

    target_interface = IViewBox


class ImageComponent(BaseComponent):
    """
    A image representing f(x,y) values
    """
    implements(IImage)
    adapts(Interface)


    def __init__(self, parent):
        super(ImageComponent, self).__init__(parent)

    def render(self):
        return '%s:' % self.__class__.__name__ + str(self.data)

    def setup(self, data, **kwargs):
        self.data = data

gsm.registerAdapter(ImageComponent)


class Image(BaseFactory):

    target_interface = IImage
