import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.Map;
import org.openstreetmap.gui.jmapviewer.*;

public class TLTVehicleDisplayGUI extends JFrame {
    private DefaultTableModel model;
    private JTable table;
    private JMapViewer map;
    private List<Map<String, String>> vehicles;

    public TLTVehicleDisplayGUI(List<Map<String, String>> initialVehicles) {
        this.vehicles = initialVehicles;

        setTitle("ThoriumBusParser v1.0 - (c) 2025 VELENDEU, eetnaviation");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(900, 600);
        setLayout(new BorderLayout());

        String[] columns = {"Type", "Line", "Latitude", "Longitude",
                "Heading", "TAK", "IsLowGround", "Destination"};
        model = new DefaultTableModel(columns, 0);
        table = new JTable(model);
        JScrollPane tableScroll = new JScrollPane(table);

        map = new JMapViewer();

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tableScroll, map);
        splitPane.setDividerLocation(400);
        add(splitPane, BorderLayout.CENTER);

        table.getSelectionModel().addListSelectionListener(e -> {
            if (!e.getValueIsAdjusting()) {
                int index = table.getSelectedRow();
                if (index >= 0 && index < map.getMapMarkerList().size()) {
                    MapMarkerDot marker = (MapMarkerDot) map.getMapMarkerList().get(index);
                    map.setDisplayPosition(marker.getCoordinate(), 15);
                }
            }
        });

        map.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent e) {
                Point clickPoint = e.getPoint();
                for (int i = 0; i < map.getMapMarkerList().size(); i++) {
                    MapMarkerDot marker = (MapMarkerDot) map.getMapMarkerList().get(i);
                    Point markerPos = map.getMapPosition(marker.getCoordinate(), false);
                    if (markerPos != null && clickPoint.distance(markerPos) <= 10) {
                        table.setRowSelectionInterval(i, i);
                        table.scrollRectToVisible(table.getCellRect(i, 0, true));
                        break;
                    }
                }
            }
        });

        refreshData(vehicles);
    }

    public void refreshData(List<Map<String, String>> newVehicles) {
        this.vehicles = newVehicles;
        model.setRowCount(0);
        map.removeAllMapMarkers();

        for (Map<String, String> vehicle : vehicles) {
            String typeStr = vehicle.get("VehicleType");
            String typeText = switch (typeStr) {
                case "1" -> "Trolleybus";
                case "2" -> "Bus";
                case "3" -> "Tram";
                case "7" -> "Nightbus";
                default -> "Unknown";
            };

            double lat = Integer.parseInt(vehicle.get("VehicleLatitude")) / 1_000_000.0;
            double lon = Integer.parseInt(vehicle.get("VehicleLongitude")) / 1_000_000.0;

            Object[] row = {
                    typeText,
                    vehicle.get("VehicleLine"),
                    lat,
                    lon,
                    vehicle.get("VehicleHeading"),
                    vehicle.get("VehicleTAK"),
                    vehicle.get("IsVehicleLowGroundVehicle"),
                    vehicle.get("VehicleDestination")
            };
            model.addRow(row);

            MapMarkerDot marker = new MapMarkerDot(lat, lon);
            marker.setName(typeText + " " + vehicle.get("VehicleLine"));
            map.addMapMarker(marker);
        }
    }
}
