import javax.swing.*;
import java.util.*;
import java.util.concurrent.*;

public class Main {
    public static void main(String[] args) {
        List<Map<String, String>> initialData = fetchVehicleData();

        TLTVehicleDisplayGUI gui = new TLTVehicleDisplayGUI(initialData);
        SwingUtilities.invokeLater(() -> gui.setVisible(true));

        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        executor.scheduleAtFixedRate(() -> {
            List<Map<String, String>> newData = fetchVehicleData();
            SwingUtilities.invokeLater(() -> gui.refreshData(newData));
        }, 5, 5, TimeUnit.SECONDS);
    }

    private static List<Map<String, String>> fetchVehicleData() {
        List<Map<String, String>> vehicleList = new ArrayList<>();
        try {
            for (String line : DownloadPage.DownloadPlainData("https://transport.tallinn.ee/gps.txt").split("\\R")) {
                Map<String, String> data = ParseGpsFile.ParseTLT(line);
                if (data != null) vehicleList.add(data);
            }
        } catch (Exception e) {
            // Handle exceptions silently or log
        }
        return vehicleList;
    }
}
