import colorsys


def aida_rectangle_items(rectangles):
    """
    Create list of rectangle items for AIDA.

    :param rectangles: List [rectangle_0, rectangle_1...], where rectangle_i is a tuple (x0, y0, width, height).
    :return: List of dictionaries, each one with the structure of a rectangle object.
    """


    def aida_rectangle_item(rectangle):
        """
        Create an object that describes a rectangle in AIDA.

        :param rectangle: (x0, y0, width, height).
        :return: item: dictionary with the structure of the rectangle object.
        """

        # extract rectangle parameters
        (x0, y0, width, height) = rectangle

        # convert RGB to HSL
        hls_colour_black = colorsys.rgb_to_hls(0, 0, 0)
        hls_colour_white = colorsys.rgb_to_hls(1, 1, 1)

        item = {
            'type': 'rectangle',
            'class': '',
            'color': {
                'fill': {
                    'hue': hls_colour_black[0] * 360,
                    'saturation': hls_colour_black[2],
                    'lightness': hls_colour_black[1],
                    'alpha': 0.5},
                'stroke': {
                    'hue': hls_colour_white[0] * 360,
                    'saturation': hls_colour_white[2],
                    'lightness': hls_colour_white[1],
                    'alpha': 1}},
            'x': x0,
            'y': y0,
            'width': width,
            'height': height,
            'locked': False
        }

        return item

    ## main function

    if type(rectangles) != list:
        raise TypeError('rectangles must be a list, even if it only has one item')

    # create the list of rectangle objects (each item is a rectangle object)
    items = [aida_rectangle_item(rectangle=rectangles[i]) for i in range(len(rectangles))]

    return items