import numpy as np

from src.repositories.inventory import core as inventoryCore
from src.repositories.inventory.config import images as containerImages


def _screenshot_with_bar(name, x, y):
    screenshot = np.zeros((1080, 1920), dtype=np.uint8)
    bar = containerImages['containersBars'][name]
    screenshot[y:y + bar.shape[0], x:x + bar.shape[1]] = bar
    return screenshot


def test_detects_backpack_in_sidebar():
    name = next(
        name for name in containerImages['containersBars']
        if name != 'backpack bottom')
    screenshot = _screenshot_with_bar(name, 1500, 300)

    found = inventoryCore.detectOpenBackpacks(screenshot)

    assert name in found
    assert found[name][0] == 1500
    assert found[name][1] == 300


def test_ignores_backpack_outside_sidebar():
    name = next(
        name for name in containerImages['containersBars']
        if name != 'backpack bottom')
    screenshot = _screenshot_with_bar(name, 100, 300)

    assert inventoryCore.detectOpenBackpacks(screenshot) == {}


def test_empty_screen_detects_nothing():
    screenshot = np.zeros((1080, 1920), dtype=np.uint8)

    assert inventoryCore.detectOpenBackpacks(screenshot) == {}
