package ballistics

import ballistics.orbitalPoints.timeStepper.TLEStepper
import ballistics.types.BallisticsError
import ballistics.types.EarthType
import ballistics.types.InitialConditions
import ballistics.types.OPKatObj
import ballistics.types.TLE
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import java.time.LocalDateTime
import java.time.ZoneOffset
import kotlin.math.PI

internal class BallisticsTest {
    @Test
    fun parseTLE() {
        var s1 = "1 51824U 22019A   22058.83400146  .00001503  00000-0  10000-3 0  9990"
        var s2 = "2 51824  97.5213 135.6262 0019439 268.1769  91.7238 15.08515864   114"

        var s = TLEStepper(s1, s2, EarthType.PZ90d02)
        var r = s.baseEpoch
        println("${ LocalDateTime.ofEpochSecond(r.toLong(),(r % 1 * 1e9).toInt(), ZoneOffset.UTC)} ")

        var p = s.calculate(r)

        println(
            "${p.vit}  ${ p.let { LocalDateTime.ofEpochSecond(p.t.toLong(),(p.t % 1 * 1e9).toInt(), ZoneOffset.UTC)} }  " +
                "${p.r.x} " +
                " ${p.r.y}  " +
                "${p.r.z} " +
                " ${p.v.x}  " +
                "${p.v.y} " +
                " ${p.v.z}",
        )
    }

    @Test
    fun testTLE() {
        val tle =
//            TLE(
//                "1 51824U 22019A   22058.83400146  .00001503  00000-0  10000-3 0  9990",
//                "2 51824  97.5213 135.6262 0019439 268.1769  91.7238 15.08515864   114",
//            )

            TLE(
                "1 45621U 11037Q   22018.21004500  .07428171  00000-0  19069+0 0  9999",
                "2 45621  51.4607 277.9348 0211635  34.9395 326.5259 15.14655454 76661",
            )


        val bal1 = Ballistics()
        val bal2 = Ballistics()

        bal1.sunAngleMin = -90 * PI / 180
        bal2.sunAngleMin = -90 * PI / 180

        val op = bal1.parseTLE(tle)

        val tn = op.t
        val tk = tn + 86400 * 2

        val r = bal1.calculateOrbPoints(tle, tn, tk)
        assertTrue(r == BallisticsError.OK)
        bal2.calculateOrbPoints(InitialConditions(op, 0.0, 0.0), tn, tk)

        bal1.calculateFlightLine(tn, tk)
        bal2.calculateFlightLine(tn, tk)

        val objs = listOf<OPKatObj>(OPKatObj(1, 1, "t",1, 60 * PI / 180, 20 * PI / 180, 100.0, 0.0, 90.0, -90.0))

        bal1.calculateMPL(tn, tk, objs)
        bal2.calculateMPL(tn, tk, objs)

        println("${bal1.mpl.count()} - ${bal2.mpl.count()}")

        assertEquals(bal1.mpl.count(), bal2.mpl.count())

        if (bal1.mpl.toList().isNotEmpty()) {
            val v1 = bal1.mpl.first()
            val v2 = bal2.mpl.first()
            assertTrue(v1.traverz - v2.traverz < 60)
        }
    }
}
