import ballistics.Ballistics
import ballistics.types.EarthType
import ballistics.types.InitialConditions
import ballistics.types.IntegrationType
import ballistics.types.ModDVType
import ballistics.types.OrbitalPoint
import ballistics.utils.astro.AstronomerJ2000
import ballistics.utils.fromDateTime
import ballistics.utils.math.Vector3D
import ballistics.utils.toDateTime
import java.time.LocalDateTime
import java.time.ZoneOffset


fun main() {

    var ts = System.currentTimeMillis()
    val r = Ballistics()

    r.modDVType = ModDVType.KONDOR // BARS
    r.integrationType = IntegrationType.RUNG4



    val tt2 = LocalDateTime.of(2028, 12, 10, 22, 24, 24, 257000000)

    val op =             OrbitalPoint(
        fromDateTime(tt2),
        1,
        Vector3D(
            2610612.004898679,
            -3507514.947081291,
            -5291534.374006776,
        ),
        Vector3D(
            -4935.515395115775,
            3497.047795669036,
            -4742.501356855924,
        ),
    )


    val mnu = mutableListOf<InitialConditions>()
    mnu.add(
        InitialConditions(
            op,
            0.06 / 9.8,
            125.0,
        ),
    )


    mnu.sortBy { it.point.t }

    // r.calculateOrbPoints(mnu.toTypedArray(), mnu.first().point.t, mnu.last().point.t+86400.0 * 5 )
    r.calculateOrbPoints(mnu.toTypedArray(), mnu.first().point.t, mnu.first().point.t + 86400.0 * 5)

    println("время расчета = ${System.currentTimeMillis() - ts}мс")

    val astro = AstronomerJ2000(EarthType.PZ90d02)
    for (p in r.revolutions) {
        val op = astro.grinvToASK(p.vuz)
        println(
            "${op.vit}  ${toDateTime(op.t)}  " +
                    "${op.r.x}  ${op.r.y}  ${op.r.z}  ${op.v.x}  ${op.v.y}  ${op.v.z}"
        )
    }

    ts = System.currentTimeMillis()
    r.calculateFlightLine(mnu.first().point.t, mnu.first().point.t + 86400.0 * 5)
    println("время расчета = ${System.currentTimeMillis() - ts}мс")

//       var p = r.points.last()
//      println("${p.vit}  ${LocalDateTime.ofEpochSecond(p.t.toLong(),0, ZoneOffset.UTC)}  ${p.x}  ${p.y}  ${p.z}  ${p.vx}  ${p.vy}  ${p.vz}")
//
//       var st = r.getStepper()
//       st?.let{
//           var p = it.calculate(nu.point.t + 120.0)//452.4)
//           p?.let {
//               println(
//                   "${p.vit}  ${
//                       LocalDateTime.ofEpochSecond(
//                           p.t.toLong(),
//                           0,
//                           ZoneOffset.UTC
//                       )
//                   }  ${p.x}  ${p.y}  ${p.z}  ${p.vx}  ${p.vy}  ${p.vz}"
//               )
//           }
//       } ?: println("is null")
//
// //
// //    val ts2 = System.currentTimeMillis()
//    var ppi = mutableListOf<PPI>(PPI(1000, 0, 0.36617007706841037, 1.8417344524829609,
//        0.0, 0.08726646259971647, 1.5707963267948966))
//    try{
//        r.calculateZRV(ppi,  mnu.first().point.t, mnu.last().point.t+86400.0 * 5)
//        println(r.zrv.size)
//    }
//    catch(e : Exception){
//        println("err")
//    }
//    println("время расчета = ${System.currentTimeMillis()-ts}мс  ЗРВ : ${r.zrv.size}")
}
