var math = use('math/radians')
var SOLAR_MASS = 4 * pi * pi;
var DAYS_PER_YEAR = 365.24;

function Body(p) {
    return {x: p.x, y: p.y, z: p.z, vx: p.vx, vy: p.vy, vz: p.vz, mass: p.mass};
}

function Jupiter() {
    return Body({
        x: 4.84143144246472090e+00,
        y: -1.16032004402742839e+00,
        z: -1.03622044471123109e-01,
        vx: 1.66007664274403694e-03 * DAYS_PER_YEAR,
        vy: 7.69901118419740425e-03 * DAYS_PER_YEAR,
        vz: -6.90460016972063023e-05 * DAYS_PER_YEAR,
        mass: 9.54791938424326609e-04 * SOLAR_MASS
    });
}

function Saturn() {
    return Body({
        x: 8.34336671824457987e+00,
        y: 4.12479856412430479e+00,
        z: -4.03523417114321381e-01,
        vx: -2.76742510726862411e-03 * DAYS_PER_YEAR,
        vy: 4.99852801234917238e-03 * DAYS_PER_YEAR,
        vz: 2.30417297573763929e-05 * DAYS_PER_YEAR,
        mass: 2.85885980666130812e-04 * SOLAR_MASS
    });
}

function Uranus() {
    return Body({
        x: 1.28943695621391310e+01,
        y: -1.51111514016986312e+01,
        z: -2.23307578892655734e-01,
        vx: 2.96460137564761618e-03 * DAYS_PER_YEAR,
        vy: 2.37847173959480950e-03 * DAYS_PER_YEAR,
        vz: -2.96589568540237556e-05 * DAYS_PER_YEAR,
        mass: 4.36624404335156298e-05 * SOLAR_MASS
    });
}

function Neptune() {
    return Body({
        x: 1.53796971148509165e+01,
        y: -2.59193146099879641e+01,
        z: 1.79258772950371181e-01,
        vx: 2.68067772490389322e-03 * DAYS_PER_YEAR,
        vy: 1.62824170038242295e-03 * DAYS_PER_YEAR,
        vz: -9.51592254519715870e-05 * DAYS_PER_YEAR,
        mass: 5.15138902046611451e-05 * SOLAR_MASS
    });
}

function Sun() {
    return Body({x: 0.0, y: 0.0, z: 0.0, vx: 0.0, vy: 0.0, vz: 0.0, mass: SOLAR_MASS});
}

var bodies = Array(Sun(), Jupiter(), Saturn(), Uranus(), Neptune());

function offsetMomentum() {
    var px = 0;
    var py = 0;
    var pz = 0;
    var size = length(bodies);
    var i = 0
    var body = null
    var mass = null
    for (i = 0; i < size; i++) {
        body = bodies[i];
        mass = body.mass;
        px += body.vx * mass;
        py += body.vy * mass;
        pz += body.vz * mass;
    }

    body = bodies[0];
    body.vx = -px / SOLAR_MASS;
    body.vy = -py / SOLAR_MASS;
    body.vz = -pz / SOLAR_MASS;
}

function advance(dt) {
    var size = length(bodies);
    var i = 0
    var j = 0
    var bodyi = null
    var bodyj = null
    var vxi = null
    var vyi = null
    var vzi = null
    var dx = null
    var dy = null
    var dz = null
    var d2 = null
    var mag = null
    var massj = null
    var massi = null
    var body = null

    for (i = 0; i < size; i++) {
        bodyi = bodies[i];
        vxi = bodyi.vx;
        vyi = bodyi.vy;
        vzi = bodyi.vz;
        for (j = i + 1; j < size; j++) {
            bodyj = bodies[j];
            dx = bodyi.x - bodyj.x;
            dy = bodyi.y - bodyj.y;
            dz = bodyi.z - bodyj.z;

            d2 = dx * dx + dy * dy + dz * dz;
            mag = dt / (d2 * math.sqrt(d2));

            massj = bodyj.mass;
            vxi -= dx * massj * mag;
            vyi -= dy * massj * mag;
            vzi -= dz * massj * mag;

            massi = bodyi.mass;
            bodyj.vx += dx * massi * mag;
            bodyj.vy += dy * massi * mag;
            bodyj.vz += dz * massi * mag;
        }
        bodyi.vx = vxi;
        bodyi.vy = vyi;
        bodyi.vz = vzi;
    }

    for (i = 0; i < size; i++) {
        body = bodies[i];
        body.x += dt * body.vx;
        body.y += dt * body.vy;
        body.z += dt * body.vz;
    }
}

function energy() {
    var e = 0;
    var size = length(bodies);
    var i = 0
    var j = 0
    var bodyi = null
    var bodyj = null
    var dx = null
    var dy = null
    var dz = null
    var distance = null

    for (i = 0; i < size; i++) {
        bodyi = bodies[i];

        e += 0.5 * bodyi.mass * ( bodyi.vx * bodyi.vx +
            bodyi.vy * bodyi.vy + bodyi.vz * bodyi.vz );

        for (j = i + 1; j < size; j++) {
            bodyj = bodies[j];
            dx = bodyi.x - bodyj.x;
            dy = bodyi.y - bodyj.y;
            dz = bodyi.z - bodyj.z;

            distance = math.sqrt(dx * dx + dy * dy + dz * dz);
            e -= (bodyi.mass * bodyj.mass) / distance;
        }
    }
    return e;
}

var n = arg[0] || 100000
var i = 0

offsetMomentum();

log.console(`n = ${n}`)
log.console(energy().toFixed(9))
for (i = 0; i < n; i++)
    advance(0.01);
log.console(energy().toFixed(9))

$stop()