package ballistics.orbitalPoints.integrator

import ballistics.types.BallisticsError
import ballistics.types.EarthType
import ballistics.types.ModDVType

internal class IntegratorRK4(mdType: ModDVType, earthType: EarthType) : AbstractIntegrator(mdType, earthType) {
    override fun accelerate(x: Array<Double>): BallisticsError {
        return BallisticsError.OK
    }

    override fun setSBall(s: Double) {
        prav.sBall = s
    }

    override fun nextStep(x: Array<Double>): BallisticsError {
        val step6 = step / 6
        var r = arrayOf(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)

        for (i in 0..6) {
            y[i] = x[i]
            r[i] = x[i]
        }
        for (j in 0..3) {
            var er = prav.calculate(r)
            if (er != BallisticsError.OK) {
                return er
            }

            for (i in 0..5)
                y[i] = y[i] + step6 * a[j] * prav.y[i]
            if (j == 3) {
                break
            }
            for (i in 0..5)
                r[i] = x[i] + step / a[j + 1] * prav.y[i]
        }
        y[6] = x[6] + step

        return BallisticsError.OK
    }
}
