# map_render.py
#
# author: deng
# date: 20251127

import folium


class MapRenderer:
    """Render hiking routes on interactive maps."""

    def __init__(self):
        """Initialize map renderer."""
        pass

    def create_route_map(self, points, start_point, end_point):
        """Create an interactive map with the hiking route.

        Args:
            points: List of (latitude, longitude) tuples for the route
            start_point: (latitude, longitude) tuple for start
            end_point: (latitude, longitude) tuple for end

        Returns:
            folium.Map: Interactive map object
        """
        if not points or len(points) < 2:
            return None

        # Calculate center of the route
        center_lat = sum(p[0] for p in points) / len(points)
        center_lon = sum(p[1] for p in points) / len(points)

        # Create map centered on the route
        route_map = folium.Map(
            location=[center_lat, center_lon],
            zoom_start=13,
            tiles='OpenStreetMap',
        )

        # Add the route as a red polyline
        folium.PolyLine(
            locations=points,
            color='red',
            weight=4,
            opacity=0.8,
            tooltip='登山路線',
        ).add_to(route_map)

        # Add start point marker (blue circle)
        if start_point:
            folium.CircleMarker(
                location=start_point,
                radius=8,
                color='blue',
                fill=True,
                fill_color='blue',
                fill_opacity=0.7,
                popup='起點',
                tooltip='起點',
            ).add_to(route_map)

        # Add end point marker (blue circle)
        if end_point:
            folium.CircleMarker(
                location=end_point,
                radius=8,
                color='blue',
                fill=True,
                fill_color='blue',
                fill_opacity=0.7,
                popup='終點',
                tooltip='終點',
            ).add_to(route_map)

        # Fit bounds to show entire route
        route_map.fit_bounds(points)

        return route_map

    def add_hover_marker(self, route_map, position, label='當前位置'):
        """Add a hover position marker to the map.

        Args:
            route_map: Existing folium map
            position: (latitude, longitude) tuple for the marker
            label: Label for the marker

        Returns:
            folium.Map: Map with added marker
        """
        if position and route_map:
            folium.CircleMarker(
                location=position,
                radius=10,
                color='orange',
                fill=True,
                fill_color='orange',
                fill_opacity=0.9,
                popup=label,
                tooltip=label,
                weight=3,
            ).add_to(route_map)

        return route_map
