const MamiAnimate = function(info) {
    if(typeof info !== 'object')
        throw 'info must be an object';

    let onUpdate;
    if('update' in info && typeof info.update === 'function')
        onUpdate = info.update;
    if(onUpdate === undefined)
        throw 'update is a required parameter for info';

    let duration;
    if('duration' in info)
        duration = parseFloat(info.duration);
    if(duration <= 0)
        throw 'duration is a required parameter for info and must be greater than 0';

    let onStart;
    if('start' in info && typeof info.start === 'function')
        onStart = info.start;

    let onEnd;
    if('end' in info && typeof info.end === 'function')
        onEnd = info.end;

    let onCancel;
    if('cancel' in info && typeof info.cancel === 'function')
        onCancel = info.cancel;

    let easing;
    if('easing' in info)
        easing = info.easing;

    let delayed;
    if('delayed' in info)
        delayed = !!info.delayed;

    let async;
    if('async' in info)
        async = !!info.async;

    const easingType = typeof easing;
    if(easingType !== 'function') {
        if(easingType === 'string'
            && easing.substring(0, 4) === 'ease'
            && easing in MamiAnimate)
            easing = MamiAnimate[easing];
        else
            easing = MamiAnimate.easeLinear;
    }

    let tStart, tLast,
        cancel = false,
        tRawCompletion = 0,
        tCompletion = 0,
        started = !delayed;

    const update = function(tCurrent) {
        if(tStart === undefined) {
            tStart = tCurrent;
            if(onStart !== undefined)
                onStart();
        }

        const tElapsed = tCurrent - tStart;

        tRawCompletion = Math.min(1, Math.max(0, tElapsed / duration));
        tCompletion = easing(tRawCompletion);

        onUpdate(tCompletion, tRawCompletion);

        if(tElapsed < duration) {
            if(cancel) {
                if(onCancel !== undefined)
                    onCancel(tCompletion, tRawCompletion);
                return;
            }

            tLast = tCurrent;
            requestAnimationFrame(update);
        } else if(onEnd !== undefined)
            onEnd();
    };

    let promise;
    if(async)
        promise = new Promise((resolve, reject) => {
            if(onCancel === undefined) {
                onCancel = reject;
            } else {
                const realOnCancel = onCancel;
                onCancel = (...args) => {
                    realOnCancel(...args);
                    reject(...args);
                };
            }

            if(onEnd === undefined) {
                onEnd = resolve;
            } else {
                const realOnEnd = onEnd;
                onEnd = (...args) => {
                    realOnEnd(...args);
                    resolve(...args);
                };
            }

            if(!delayed)
                requestAnimationFrame(update);
        });

    if(!delayed) {
        if(promise !== undefined)
            return promise;

        requestAnimationFrame(update);
    }

    return {
        getCompletion: function() {
            return tCompletion;
        },
        getRawCompletion: function() {
            return tRawCompletion;
        },
        start: () => {
            if(!started) {
                started = true;
                requestAnimationFrame(update);
            }

            if(promise !== undefined)
                return promise;
        },
        cancel: function() {
            cancel = true;
        },
    };
};

// Yoinked from https://easings.net/
MamiAnimate.C1 = 1.70158;
MamiAnimate.C2 = MamiAnimate.C1 + 1.525;
MamiAnimate.C3 = MamiAnimate.C1 + 1;
MamiAnimate.C4 = (2 * Math.PI) / 3;
MamiAnimate.C5 = (2 * Math.PI) / 4.5;
MamiAnimate.D1 = 2.75;
MamiAnimate.N1 = 7.5625;
MamiAnimate.easeLinear = function(x) { return x; };
MamiAnimate.easeInSine = function(x) { return 1 - Math.cos((x * Math.PI) / 2); };
MamiAnimate.easeOutSine = function(x) { return Math.sin((x * Math.PI) / 2); };
MamiAnimate.easeInOutSine = function(x) { return -(Math.cos(Math.PI * x) - 1) / 2; };
MamiAnimate.easeInCubic = function(x) { return x * x * x; };
MamiAnimate.easeOutCubic = function(x) { return 1 - Math.pow(1 - x, 3); };
MamiAnimate.easeInOutCubic = function(x) {
    return x < .5
        ? (4 * x * x * x)
        : (1 - Math.pow(-2 * x + 2, 3) / 2);
};
MamiAnimate.easeInQuint = function(x) { return x * x * x * x * x; };
MamiAnimate.easeOutQuint = function(x) { return 1 - Math.pow(1 - x, 5); };
MamiAnimate.easeInOutQuint = function(x) {
    return x < .5
        ? (16 * x * x * x * x * x)
        : (1 - Math.pow(-2 * x + 2, 5) / 2);
};
MamiAnimate.easeInCirc = function(x) { return 1 - Math.sqrt(1 - Math.pow(x, 2)); };
MamiAnimate.easeOutCirc = function(x) { return Math.sqrt(1 - Math.pow(x - 1, 2)); };
MamiAnimate.easeInOutCirc = function(x) {
    return x < .5
        ? ((1 - Math.sqrt(1 - Math.pow(2 * x, 2))) / 2)
        : ((Math.sqrt(1 - Math.pow(-2 * x + 2, 2)) + 1) / 2);
};
MamiAnimate.easeInElastic = function(x) {
    if(x === 0.0)
        return 0;
    if(x === 1.0)
        return 1;
    return -Math.pow(2, 10 * x - 10) * Math.sin((x * 10 - 10.75) * MamiAnimate.C4);
};
MamiAnimate.easeOutElastic = function(x) {
    if(x === 0.0)
        return 0;
    if(x === 1.0)
        return 1;
    return Math.pow(2, -10 * x) * Math.sin((x * 10 - .75) * MamiAnimate.C4) + 1;
};
MamiAnimate.easeInOutElastic = function(x) {
    if(x === 0.0)
        return 0;
    if(x === 1.0)
        return 1;
    return x < .5
        ? (-(Math.pow(2, 20 * x - 10) * Math.sin((20 * x - 11.125) * MamiAnimate.C5)) / 2)
        : ((Math.pow(2, -20 * x + 10) * Math.sin((20 * x - 11.125) * MamiAnimate.C5)) / 2 + 1);
};
MamiAnimate.easeInQuad = function(x) { return x * x; };
MamiAnimate.easeOutQuad = function(x) { return 1 - (1 - x) * (1 - x); };
MamiAnimate.easeInOutQuad = function(x) {
    return x < .5
        ? (2 * x * x)
        : (1 - Math.pow(-2 * x + 2, 2) / 2);
};
MamiAnimate.easeInQuart = function(x) { return x * x * x * x; };
MamiAnimate.easeOutQuart = function(x) { return 1 - Math.pow(1 - x, 4); };
MamiAnimate.easeInOutQuart = function(x) {
    return x < .5
        ? (8 * x * x * x * x)
        : (1 - Math.pow(-2 * x + 2, 4) / 2);
};
MamiAnimate.easeInExpo = function(x) {
    if(x === 0.0)
        return 0;
    return Math.pow(2, 10 * x - 10);
};
MamiAnimate.easeOutExpo = function(x) {
    if(x === 1.0)
        return 1;
    return 1 - Math.pow(2, -10 * x);
};
MamiAnimate.easeInOutExpo = function(x) {
    if(x === 0.0)
        return 0;
    if(x === 1.0)
        return 1;
    return x < .5
        ? (Math.pow(2, 20 * x - 10) / 2)
        : ((2 - Math.pow(2, -20 * x + 10)) / 2);
};
MamiAnimate.easeInBack = function(x) { return MamiAnimate.C3 * x * x * x - MamiAnimate.C1 * x * x; };
MamiAnimate.easeOutBack = function(x) { return 1 + MamiAnimate.C3 * Math.pow(x - 1, 3) + MamiAnimate.C1 * Math.pow(x - 1, 2); };
MamiAnimate.easeInOutBack = function(x) {
    return x < .5
        ? ((Math.pow(2 * x, 2) * ((MamiAnimate.C2 + 1) * 2 * x - MamiAnimate.C2)) / 2)
        : ((Math.pow(2 * x - 2, 2) * ((MamiAnimate.C2 + 1) * (x * 2 - 2) + MamiAnimate.C2) + 2) / 2);
};
MamiAnimate.easeInBounce = function(x) { return 1 - MamiAnimate.easeOutBounce(1 - x); };
MamiAnimate.easeOutBounce = function(x) {
    if(x < 1 / MamiAnimate.D1)
        return MamiAnimate.N1 * x * x;

    if(x < 2 / MamiAnimate.D1) {
        x -= 1.5;
        return MamiAnimate.N1 * (x / MamiAnimate.D1) * x + .75;
    }

    if(x < 2.5 / MamiAnimate.D1) {
        x -= 2.25;
        return MamiAnimate.N1 * (x / MamiAnimate.D1) * x + .9375;
    }

    x -= 2.625;
    return MamiAnimate.N1 * (x / MamiAnimate.D1) * x + .984375;
};
MamiAnimate.easeInOutBounce = function(x) {
    return x < .5
        ? ((1 - MamiAnimate.easeOutBounce(1 - 2 * x)) / 2)
        : ((1 + MamiAnimate.easeOutBounce(2 * x - 1)) / 2);
};
