import json
import sys
from pathlib import Path
from time import sleep

repo = Path.home() / 'TibiaPilotNG'
sys.path.insert(0, str(repo))

from src.utils.mouse import rightClick
from src.repositories.radar.core import getFloorLevel
from src.utils.core import getScreenshot


result = {'ok': False}
try:
    attempts = []
    for coordinate in (
        (720, 352), (736, 352), (720, 368), (704, 368),
        (736, 368), (704, 336), (720, 336), (736, 336),
    ):
        rightClick(coordinate)
        sleep(0.8)
        floor = getFloorLevel(getScreenshot())
        attempts.append({'coordinate': coordinate, 'floor': floor})
        if floor == 7:
            break
    result = {'ok': attempts[-1]['floor'] == 7, 'attempts': attempts}
finally:
    (repo / '.use-ladder-result.json').write_text(
        json.dumps(result), encoding='utf-8',
    )
