from src.shared.typings import GrayImage
from src.utils.core import locate
from .config import images


# TODO: add unit tests
# TODO: add perf
def isContainerOpen(screenshot: GrayImage, name: str) -> bool:
    return locate(screenshot, images['containersBars'][name]) is not None


def detectOpenBackpacks(screenshot: GrayImage):
    """Erkennt alle geoeffneten Backpack-Container auf dem Screen.

    Rueckgabe: {name: (x, y, w, h)} der Titelleisten-Position.
    Die Suche laeuft nur in der rechten Bildschirmhaelfte (Container
    oeffnen in der Sidebar) — ~35 Icon-Matches sind so guenstig genug.
    """
    width = screenshot.shape[1]
    sidebar = screenshot[:, width // 2:]
    found = {}
    for name, bar in images['containersBars'].items():
        if name in ('backpack bottom', 'locker', 'depot', 'stash'):
            continue
        position = locate(sidebar, bar, confidence=0.85)
        if position is not None:
            found[name] = (position[0] + width // 2, position[1],
                           position[2], position[3])
    return found
