import logging
import uuid
from typing import Optional
from cooptools.geometry_utils import vector_utils as vec
from dataclasses import dataclass, field
from cooptools.physics.kinematic import Transform
from cooptools.physics import waypointFollower as wf
from examples.voroni_world.constants import *
from examples.voroni_world.behaviorTypes import BehaviorType

logger = logging.getLogger(__name__)

@dataclass(kw_only=True)
class Villager:
    name: int
    waypoint_follower: wf.WaypointFollower = field(init=False)
    id: uuid.UUID = field(init=False, default_factory=uuid.uuid4)
    __behavior: Optional[BehaviorType] = None

    def __post_init__(self):
        self.waypoint_follower = wf.WaypointFollower(
            name=str(self.name),
            initial_transform=Transform(translation=(0, 0)),
            max_acceleration=VILLAGER_MAX_ACCEL,
            velocity_magnitude_bounds=(0, VILLAGER_MAX_VELO)
        )

    def set_pos(self, pos: vec.FloatVec):
        self.waypoint_follower.update(delta_time_ms=0, set_pos=pos)
        return self

    def set_behavior(self, behavior: BehaviorType):
        logger.info(f"Setting villager \'{self.name}\'s behavior to \'{behavior}\'")
        self.__behavior = behavior

    @property
    def Position(self) -> vec.FloatVec:
        return self.waypoint_follower.Position

    @property
    def Behavior(self) -> BehaviorType:
        return self.__behavior
