import cv2

from cv_analysis.utils import copy_and_normalize_channels


def draw_contours(image, contours, color=None, annotate=False):

    image = copy_and_normalize_channels(image)

    for cont in contours:
        cv2.drawContours(image, cont, -1, (0, 255, 0), 4)

    return image


def draw_rectangles(image, rectangles, color=None, annotate=False):
    def annotate_rect(x, y, w, h):
        cv2.putText(
            image,
            "+",
            (x + (w // 2) - 12, y + (h // 2) + 9),
            cv2.FONT_HERSHEY_SIMPLEX,
            1,
            (0, 255, 0),
            2,
        )

    image = copy_and_normalize_channels(image)

    if not color:
        color = (0, 255, 0)

    for rect in rectangles:
        x, y, w, h = rect
        cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
        if annotate:
            annotate_rect(x, y, w, h)

    return image
