import numpy as np

from src.repositories.gameWindow import fields


def make_image(bright_tiles=(), size=(220, 300)):
    """15x11 Tiles bei 300x220 px: Zentrum-Tile (7,5) = Spieler."""
    image = np.full(size, 80, dtype=np.uint8)
    tile_w, tile_h = size[1] // 15, size[0] // 11
    for (tx, ty) in bright_tiles:
        image[ty * tile_h:(ty + 1) * tile_h,
              tx * tile_w:(tx + 1) * tile_w] = 250
    return image


def test_bright_tile_maps_to_radar_coordinate(monkeypatch):
    monkeypatch.delenv(fields.FIELDS_ENV, raising=False)
    image = make_image(bright_tiles=[(8, 5)])
    result = fields.getFieldCoordinates(image, (32000, 32000, 7))
    assert result == [(32001, 32000, 7)]


def test_multiple_fields_and_offsets(monkeypatch):
    monkeypatch.delenv(fields.FIELDS_ENV, raising=False)
    image = make_image(bright_tiles=[(7, 5), (0, 0), (14, 10)])
    result = fields.getFieldCoordinates(image, (32000, 32000, 7))
    assert (32000, 32000, 7) in result
    assert (31993, 31995, 7) in result
    assert (32007, 32005, 7) in result
    assert len(result) == 3


def test_dim_tiles_are_ignored(monkeypatch):
    monkeypatch.delenv(fields.FIELDS_ENV, raising=False)
    image = np.full((220, 300), 180, dtype=np.uint8)
    assert fields.getFieldCoordinates(image, (32000, 32000, 7)) == []


def test_none_inputs(monkeypatch):
    monkeypatch.delenv(fields.FIELDS_ENV, raising=False)
    assert fields.getFieldCoordinates(None, (32000, 32000, 7)) == []
    image = make_image(bright_tiles=[(8, 5)])
    assert fields.getFieldCoordinates(image, None) == []


def test_disabled_via_env(monkeypatch):
    monkeypatch.setenv(fields.FIELDS_ENV, '0')
    image = make_image(bright_tiles=[(8, 5)])
    assert fields.getFieldCoordinates(image, (32000, 32000, 7)) == []
