package ballistics.orbitalPoints.integrator.modDv

import ballistics.types.BallisticsError
import ballistics.types.EarthType
import ballistics.utils.atmosphere.getAtm81
import ballistics.utils.geopotential.Geopotencial1990
import ballistics.utils.math.Vector3D

internal open class MDVBars(earthType: EarthType) : AbstractMDV(earthType) {
    protected var geopotential = Geopotencial1990()

    override fun calculate(x: Array<Double>): BallisticsError {
        prepare(x)
        y[0] = oma * x[3] + earth.wEarth * x[1] * 2
        y[1] = oma * x[4] - earth.wEarth * x[0] * 2
        y[2] = bca * x[5]

        y[3] = x[0]
        y[4] = x[1]
        y[5] = x[2]

        // УЧЕТ АТМОСФЕРЫ //

        val h = r - earth.ekvRadius * (1.0 - earth.alphaEllips * fik)
        if (h < hminimum) {
            return BallisticsError.H_MINIMUM_ERROR
        }

        val ro = getAtm81(h)

        val srv = v * ro * sBall

        y[0] -= srv * x[0]
        y[1] -= srv * x[1]
        y[2] -= srv * x[2]

        // УЧЕТ АНОМАЛИЙ ГЕОПОТЕНЦИАЛА //

        var xyz = Vector3D(0.0, 0.0, 0.0)
        var rez: BallisticsError = geopotential.anomkond(x, 16, 16, xyz)
        if (rez != BallisticsError.OK) {
            return rez
        }

        y[0] = y[0] + xyz.x / 1000000.0
        y[1] = y[1] + xyz.y / 1000000.0
        y[2] = y[2] + xyz.z / 1000000.0

        return BallisticsError.OK
    }
}
