import logging
from examples.voroni_world.models import *
import pprint
from typing import Dict, Tuple, List, Callable
from cooptools import graphs as grph
from cooptools.physics import waypointFollower as wf

logger = logging.getLogger(__name__)

def send_villager_to_region(
        villager: Villager,
        region: Region,
        villager_current_region_provider: Callable[[Villager], Region],
        route_provider: Callable[[Region, Region], grph.AStarResults],
        route_to_waypoints_provider: Callable[[List[grph.Node]], List[wf.Waypoint]],
        stop_at_border: bool = False
):
    current_region = villager_current_region_provider(villager)
    route = route_provider(current_region, region)

    if route.nodes is None:
        logger.error(f"Unable to route from {current_region.name} to selected region {region.name}"
                     f"\n[{pprint.pformat(route)}]")
        return False


    if stop_at_border:
        nds = list(route.nodes[:-1])
    else:
        nds = list(route.nodes)

    wpts = route_to_waypoints_provider(nds)

    villager.waypoint_follower.update(delta_time_ms=0,
                                      new_waypoints=wpts)