import numpy as np

from src.repositories.actionBar import core as actionBarCore


def _screenshot_with_slot(fill):
    shot = np.zeros((60, 120), dtype=np.uint8)
    shot[10:44, 20:54] = fill
    return shot


def _mockArrows(mocker):
    # Position so, dass Slot 1 bei (20, 10) beginnt: x0 = 3+15+2
    mocker.patch.object(
        actionBarCore.actionBarLocators, 'getLeftArrowsPosition',
        return_value=(3, 10, 15, 34))


def test_ocr_is_cached_per_digit_image(mocker):
    _mockArrows(mocker)
    ocrSpy = mocker.patch.object(
        actionBarCore.pytesseract, 'image_to_string', return_value='25')
    actionBarCore._slotCountCache.clear()
    shot = _screenshot_with_slot(100)

    assert actionBarCore.getSlotCount(shot, 1) == 25
    assert actionBarCore.getSlotCount(shot, 1) == 25
    assert ocrSpy.call_count == 1


def test_ocr_reruns_when_digits_change(mocker):
    _mockArrows(mocker)
    ocrSpy = mocker.patch.object(
        actionBarCore.pytesseract, 'image_to_string', return_value='24')
    actionBarCore._slotCountCache.clear()

    assert actionBarCore.getSlotCount(_screenshot_with_slot(100), 1) == 24
    assert actionBarCore.getSlotCount(_screenshot_with_slot(200), 1) == 24
    assert ocrSpy.call_count == 2
