#!/usr/bin/env python3


class LOShapes(object):
    _type = 'ShapeCollection'

    def __init__(self, obj):
        self._obj = obj

    def __len__(self):
        return self.obj.Count

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        pass

    def __iter__(self):
        self._index = 0
        return self

    def __next__(self):
        try:
            s = self.obj[self._index]
            shape = LOShape(s)
        except IndexError:
            raise StopIteration

        self._index += 1
        return shape

    def __str__(self):
        return 'Shapes'

    @property
    def obj(self):
        return self._obj


class LOShape():
    IMAGE = 'com.sun.star.drawing.GraphicObjectShape'

    def __init__(self, obj):
        self._obj = obj

    def __str__(self):
        return f'Shape: {self.name}'

    @property
    def obj(self):
        return self._obj

    @property
    def properties(self):
        # ~ properties = self.obj.PropertySetInfo.Properties
        # ~ data = {p.Name: getattr(self.obj, p.Name) for p in properties}
        data = self.obj.PropertySetInfo.Properties
        keys = [p.Name for p in data]
        values = self.obj.getPropertyValues(keys)
        data = dict(zip(keys, values))
        return data
    @properties.setter
    def properties(self, values):
        set_properties(self.obj, values)

    @property
    def shape_type(self):
        return self.obj.ShapeType

    @property
    def name(self):
        return self.obj.Name
    @name.setter
    def name(self, value):
        self.obj.Name = value

    @property
    def is_image(self):
        return self.shape_type == self.IMAGE

    @property
    def is_shape(self):
        return self.shape_type != self.IMAGE

    @property
    def size(self):
        s = self.obj.Size
        return s
    @size.setter
    def size(self, value):
        self.obj.Size = value

    @property
    def width(self):
        s = self.obj.Size
        return s.Width
    @width.setter
    def width(self, value):
        s = self.size
        s.Width = value
        self.size = s

    @property
    def height(self):
        s = self.obj.Size
        return s.Height
    @height.setter
    def height(self, value):
        s = self.size
        s.Height = value
        self.size = s

    @property
    def position(self):
        return self.obj.Position
    @property
    def x(self):
        return self.position.X
    @property
    def y(self):
        return self.position.Y

    @property
    def string(self):
        return self.obj.String
    @string.setter
    def string(self, value):
        self.obj.String = value

    @property
    def title(self):
        return self.obj.Title
    @title.setter
    def title(self, value):
        self.obj.Title = value

    @property
    def description(self):
        return self.obj.Description
    @description.setter
    def description(self, value):
        self.obj.Description = value

