#!/usr/bin/env python3

from com.sun.star.awt import Size, Point
from .easymain import BaseObject, set_properties


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(BaseObject):
    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
    @x.setter
    def x(self, value):
        self.obj.Position = Point(value, self.y)

    @property
    def y(self):
        return self.position.Y
    @y.setter
    def y(self, value):
        self.obj.Position = Point(self.x, value)

    @property
    def possize(self):
        data = {
            'Width': self.size.Width,
            'Height': self.size.Height,
            'X': self.position.X,
            'Y': self.position.Y,
        }
        return data
    @possize.setter
    def possize(self, value):
        self.obj.Size = Size(value['Width'], value['Height'])
        self.obj.Position = Point(value['X'], value['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

    @property
    def in_background(self):
        return self.obj.LayerID
    @in_background.setter
    def in_background(self, value):
        self.obj.LayerID = value

    @property
    def layerid(self):
        return self.obj.LayerID
    @layerid.setter
    def layerid(self, value):
        self.obj.LayerID = value

    @property
    def is_range(self):
        return False

    @property
    def is_cell(self):
        return False


