import Vector from './vector'

class MassObject {
    constructor() {
        this.objects = []
        this.friction = 0
    }

    config(opts) {

    }

    add(obj) {
        this.objects.push(obj)
        const initProperty = prop => obj[prop] || (obj[prop] = new Vector())
        const props = ['position', 'velocity', 'acceleration']
        props.forEach(initProperty)
    }

    remove(obj) {
        this.objects = this.objects.filter(item => item !== obj)
    }

    clear() {
        this.objects = []
    }

    applyForce(force, obj) {
        const objs = obj ? [obj] : this.objects
        objs.forEach(obj => obj.acceleration = force)
    }

    updatePostition(dt, obj) {
        const objs = obj ? [obj] : this.objects
        const friction = this.friction
        objs.forEach(obj => {
            obj.velocity.add(obj.acceleration).scale(friction * dt)
            // const dtVelocity = this.velocity.copy().scale(dt)
            const newPosition = obj.position.copy().add(obj.velocity)
            obj.position = newPosition
        })

        // const acceleration = this.acceleration.scale(speed).limit(maxSpeed)
        // this.velocity.add(this.acceleration).scale(this.friction)
        // this.velocity.add(this.acceleration).scale(this.friction * dt)
        // const dtVelocity = this.velocity.copy().scale(dt)
        // const newPosition = this.position.copy().add(this.velocity)
        // this.position = newPosition
    }
}

export default new MassObject()

// MassObject.add(obj)
// MassObject.applyForce(v, obj)
// MassObject.updatePostition(dt, obj)
