"""
Polygon class

Medical Images 2022-2023 MUEI/MUNR ETSEIB
D. Tost
"""

from point import Point


class Polygon:
    @classmethod
    def from_dict(cls, dic):
        color = tuple(dic["color"])
        return cls(color, list(map(lambda x: Point.from_dict(x), dic["vertices"])))

    def __init__(self, color, vertices):
        self.color = color
        self.__vertices = vertices

    def add(self, v):
        self.__vertices.append(v)

    def __len__(self):
        return len(self.__vertices)

    def __getitem__(self, i):
        return self.__vertices[i]

    def __str__(self):
        return "Polygon of {} vertices".format(len(self))

    def same_vertices(self, other):
        """
        Returns True if 2 polygons have the same vertices
        """
        for v1, v2 in zip(self, other):
            if v1 != v2:
                return False
        return True
        
    def __eq__(self, other):
        """
        Returns True if 2 polygons have the same color and vertices
        """
        return self.color == other.color and self.same_vertices(other)
    
    def to_dict(self):
        l = []
        for vert in self.__vertices:
            l.append(vert.to_dict())
        return {"color": list(self.color), "vertices": l}

    def bounding_box(self):
        """
        To be implemented: it returns the bounding box of the polygon
        """
        return None

    def rasterization(self, matx):
        """
        To be implemented: it returns the list of pixels corresponding to the
        rasterization of the polygon having applied the window-to -viewport transformation matrixmatx
        """
        return []
