package de.arxsilex.maptools.converter.desktop

import kotlin.test.Test
import kotlin.test.assertEquals

class WorkerProgressFormatterTest {
    @Test
    fun `formats worker heartbeat for users`() {
        assertEquals(
            "Mapsforge-Karte wird erzeugt · 02:05",
            WorkerProgressFormatter.formatOrNull(
                "MTC-WORKER phase=mapsforge-map status=running elapsedSeconds=125",
            ),
        )
    }

    @Test
    fun `formats quantitative mapsforge progress`() {
        assertEquals(
            "Wege werden verarbeitet · 50.000 / 100.000 (50 %)",
            WorkerProgressFormatter.formatOrNull("Progress: Ways 50.000 / 100.000"),
        )
        assertEquals(
            0.5f,
            WorkerProgressFormatter.progressOrNull("Progress: Ways 50.000 / 100.000")?.progress,
        )
    }

    @Test
    fun `formats large unbounded object counters without inventing a percentage`() {
        val progress = WorkerProgressFormatter.progressOrNull("Progress: Nodes 1,250,000")

        assertEquals("Knoten werden verarbeitet · 1.250.000", progress?.message)
        assertEquals(null, progress?.progress)
    }

    @Test
    fun `exposes indeterminate crop and completed phase`() {
        val crop = WorkerProgressFormatter.progressOrNull("MTC-WORKER phase=crop status=running elapsedSeconds=12")
        val completed = WorkerProgressFormatter.progressOrNull("MTC-WORKER phase=mapsforge-map status=completed")

        assertEquals(null, crop?.progress)
        assertEquals("crop", crop?.phase)
        assertEquals(1f, completed?.progress)
    }

    @Test
    fun `formats quantitative parallel pipeline progress`() {
        val progress = WorkerProgressFormatter.progressOrNull(
            "MTC-WORKER phase=mtci-build status=running current=25 total=100",
        )

        assertEquals("Paralleles Zwischenformat wird aufgebaut · 25 / 100 (25 %)", progress?.message)
        assertEquals(0.25f, progress?.progress)
        assertEquals("mtci-build", progress?.phase)
    }

    @Test
    fun `shows total and percentage during spatial selection`() {
        val progress = WorkerProgressFormatter.progressOrNull(
            "MTC-WORKER phase=mtci-select status=running current=38289 total=100000",
        )

        assertEquals(
            "Kartenausschnitt und Abhängigkeiten werden ausgewählt · 38.289 / 100.000 (38 %)",
            progress?.message,
        )
        assertEquals(0.38289f, progress?.progress)
    }

    @Test
    fun `formats multi source crop and merge phases`() {
        assertEquals(
            "Quellen werden zugeschnitten · 2 / 3 (66 %)",
            WorkerProgressFormatter.formatOrNull(
                "MTC-WORKER phase=combine-crop status=running current=2 total=3",
            ),
        )
        assertEquals(
            "Quellen werden zusammengeführt · 00:12",
            WorkerProgressFormatter.formatOrNull(
                "MTC-WORKER phase=combine-merge status=running elapsedSeconds=12",
            ),
        )
        assertEquals(
            "Mapsforge-Quelle wird für den Ausschnitt vorbereitet",
            WorkerProgressFormatter.formatOrNull(
                "MTC-WORKER phase=mapsforge-source status=running objects=8000 blocks=1",
            ),
        )
    }
}
