package com.nisemoe.nise.osu

import com.nisemoe.nise.replays.WTC
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import org.junit.jupiter.api.Assertions
import org.testcontainers.shaded.org.bouncycastle.util.Arrays
import java.nio.file.Paths
import kotlin.test.assertEquals

class WtcTest {
    private val resourcesPath = Paths.get("src", "test", "resources", "wtc")

    /**
     * Compares the bytes produced from the `wtcCompress()` function with bytes produced from the Python implementation of WTC.
     */
    @ParameterizedTest
    @ValueSource(strings = ["replay_1", "replay_2", "replay_3", "replay_4"])
    fun wtcCompressReturnsCorrectBytes(replayName: String) {
        val expected = resourcesPath.resolve("${replayName}_compressed.dat").toFile().readBytes()
        val replayEvents = resourcesPath.resolve("${replayName}_events.txt").toFile().readText()

        val wtcCompressed = WTC.compress(replayEvents)

        // We include a version header at the start of the compressed byte array - create a new array excluding these
        // so we can compare just the raw data with the Python WTC implementation's output.
        // (remove the first two bytes since the version header is a Short)
        val wtcCompressedWithoutVersionHeader = Arrays.copyOfRange(wtcCompressed, 2, wtcCompressed.size)

        Assertions.assertArrayEquals(expected, wtcCompressedWithoutVersionHeader)
    }

    @ParameterizedTest
    @ValueSource(strings = ["replay_1", "replay_2", "replay_3", "replay_4"])
    fun wtcDecompressReturnsCorrectReplayFrames(replayName: String) {
        val expected = resourcesPath.resolve("${replayName}_decompressed.txt").toFile().readText()
        val compressedReplay = resourcesPath.resolve("${replayName}_compressed.dat").toFile().readBytes()

        val wtcDecompressed = WTC.decompress(compressedReplay, false)

        assertEquals(expected, wtcDecompressed)
    }
}