package ballistics.orbitalPoints.integrator.modDv

import ballistics.types.BallisticsError
import ballistics.types.EarthType
import ballistics.utils.earth.AbstractEarth
import ballistics.utils.earth.getEarth

internal abstract class AbstractMDV(val earthType: EarthType) {
    protected val hminimum = 160000.0
    protected var r = 0.0
    protected var v = 0.0
    protected var fik = 0.0
    protected var bca = 0.0
    protected var oma = 0.0
    protected var earth: AbstractEarth = getEarth(earthType)
    var sBall = 0.0
    var f81 = 100.0
    var y = arrayOf(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)

    fun prepare(x: Array<Double>) {
        var z2 = x[5] * x[5]
        var r2 = Math.pow(x[3], 2.0) + Math.pow(x[4], 2.0) + z2
        var v2 = x[2] * x[2] + x[1] * x[1] + x[0] * x[0]
        r = Math.sqrt(r2)
        v = Math.sqrt(v2)
        fik = z2 / r2
        var d = 5 * fik
        var c = earth.ekvRadius * earth.ekvRadius / r2 * 1.5 * earth.c20
        var b = earth.fM / r / r2
        var a = b * (1 + c * (d - 1))
        bca = 2 * b * c - a
        oma = earth.wEarth * earth.wEarth - a
    }

    abstract fun calculate(x: Array<Double>): BallisticsError
}
