/*
 * Copyright (C) 2025  Tim Zimmermann <tim@aglais.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.aglais.zms.ui.map;

import org.aglais.zms.ui.util.GeoUtil;
import org.jxmapviewer.JXMapViewer;
import org.jxmapviewer.viewer.DefaultWaypointRenderer;
import org.jxmapviewer.viewer.WaypointRenderer;

import java.awt.*;
import java.awt.geom.Point2D;

/**
 * A custom WaypointRenderer implementation that renders RadiusWaypoint objects on a JXMapViewer.
 * This renderer displays a waypoint with a surrounding circle representing a specified radius.
 * The circle is drawn with a semi-transparent blue fill and a solid blue outline.
 */
public class RadiusWaypointRenderer implements WaypointRenderer<RadiusWaypoint> {

    private final DefaultWaypointRenderer defaultRenderer = new DefaultWaypointRenderer();

    @Override
    public void paintWaypoint(Graphics2D g, JXMapViewer map, RadiusWaypoint waypoint) {
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        defaultRenderer.paintWaypoint(g, map, waypoint);

        Point2D center = map.getTileFactory().geoToPixel(waypoint.getPosition(), map.getZoom());
        Point2D edge = map.getTileFactory()
                .geoToPixel(GeoUtil.offsetByMeters(waypoint.getPosition(), 0, waypoint.getRadius()),
                        map.getZoom());
        int radiusPixels = (int) (edge.getX() - center.getX());

        // Draw a semi-transparent blue filled circle
        g.setColor(new Color(0, 0, 255, 50));
        g.fillOval((int) center.getX() - radiusPixels, (int) center.getY() - radiusPixels,
                radiusPixels * 2, radiusPixels * 2);

        // Draw the solid blue outline of the circle
        g.setColor(Color.BLUE);
        g.drawOval((int) center.getX() - radiusPixels, (int) center.getY() - radiusPixels,
                radiusPixels * 2, radiusPixels * 2);
    }
}