let slowBefore = 100
let defaultSpeed = 20

let angle = 0
let dest = 0

const normalize = val => {
    let result = val % 360
    if (result < 0) result = 360 + result
    return result
}

const to = val => {
    dest = val
}

const get = () => normalize(angle)

const update = dt => {
    const dif = Math.abs(dest - angle)
    if (dif < .1) return

    let speed = Math.sign(dest - angle) * defaultSpeed
    if (dif < slowBefore) {
        // const easeOut = progress => Math.pow(--progress, 5) + 1
        const k = dif / slowBefore * 2
        speed = speed * k
    }

    const normalized_dt = dt / 100
    angle += speed * normalized_dt
    angle = angle
}

const inc = (val = 10) => {
    dest = angle + val
}

const setOptions = (speed, distanceBeforeSlow) => {
    defaultSpeed = speed
    slowBefore = distanceBeforeSlow
}

export default {
    update,
    to,
    get,
    inc,
    setOptions
}
