from functools import partial

import cv2
import numpy as np

from cv_analysis.figure_detection.figures import detect_large_coherent_structures
from cv_analysis.figure_detection.text import remove_primary_text_regions
from cv_analysis.utils.filters import (
    is_large_enough,
    has_acceptable_format,
    is_not_too_large,
)
from cv_analysis.utils.postprocessing import remove_included
from cv_analysis.utils.structures import Rectangle


def detect_figures(image: np.array):
    max_area = image.shape[0] * image.shape[1] * 0.99
    min_area = 5000
    max_width_to_height_ratio = 6
    figure_filter = partial(is_likely_figure, min_area, max_area, max_width_to_height_ratio)

    image = remove_primary_text_regions(image)
    cnts = detect_large_coherent_structures(image)
    cnts = filter(figure_filter, cnts)

    rects = map(cv2.boundingRect, cnts)
    rects = map(Rectangle.from_xywh, rects)
    rects = remove_included(rects)

    return rects


def is_likely_figure(min_area, max_area, max_width_to_height_ratio, cnts):
    return (
        is_not_too_large(cnts, max_area)
        and is_large_enough(cnts, min_area)
        and has_acceptable_format(cnts, max_width_to_height_ratio)
    )
