internal void CreateAnimation(
            string animationName,
            IEnumerable <MWAnimationKeyframe> keyframes,
            IEnumerable <MWAnimationEvent> events,
            MWAnimationWrapMode wrapMode,
            MWSetAnimationStateOptions initialState,
            bool isInternal,
            bool managed,
            Action onCreatedCallback)
        {
            var continuation = new MWContinuation(AttachedActor, null, (result) =>
            {
                var animationPlayer = GetOrCreateGodotAnimationPlayer(animationName);
                var animation       = new GodotAnimation();
                animation.Loop      = wrapMode.IsInterpolationLoopWrap();

                var curves = new Dictionary <string, int>();

                int GetOrCreateCurve(Type type, string propertyName)
                {
                    if (!curves.TryGetValue(propertyName, out int trackIndex))
                    {
                        trackIndex = animation.AddTrack(GodotAnimation.TrackType.Bezier);
                        curves.Add(propertyName, trackIndex);
                    }

                    return(trackIndex);
                }

                void AddFloatPatch(Type type, string propertyName, float time, float?value)
                {
                    if (value.HasValue)
                    {
                        var trackIndex = GetOrCreateCurve(type, propertyName);
                        animation.TrackSetPath(trackIndex, propertyName);
                        animation.BezierTrackInsertKey(trackIndex, time, value.Value);
                    }
                }

                void AddVector3Patch(Type type, string propertyName, float time, Vector3Patch value)
                {
                    AddFloatPatch(type, String.Format("{0}:x", propertyName), time, value?.X);
                    AddFloatPatch(type, String.Format("{0}:y", propertyName), time, value?.Y);
                    AddFloatPatch(type, String.Format("{0}:z", propertyName), time, value?.Z);
                }

                void AddQuaternionPatch(Type type, string propertyName, float time, QuaternionPatch value)
                {
                    AddFloatPatch(type, String.Format("{0}:x", propertyName), time, value?.X);
                    AddFloatPatch(type, String.Format("{0}:y", propertyName), time, value?.Y);
                    AddFloatPatch(type, String.Format("{0}:z", propertyName), time, value?.Z);
                    AddFloatPatch(type, String.Format("{0}:w", propertyName), time, value?.W);
                }

                void AddTransformPatch(float time, ScaledTransformPatch value)
                {
                    // Work around a Unity bug/feature where all position components must be specified
                    // in the keyframe or the missing fields get set to zero.
                    Vector3Patch position = value?.Position;
                    if (position != null && position.IsPatched())
                    {
                        if (!position.X.HasValue)
                        {
                            position.X = Transform.origin.x;
                        }
                        if (!position.Y.HasValue)
                        {
                            position.Y = Transform.origin.y;
                        }
                        if (!position.Z.HasValue)
                        {
                            position.Z = Transform.origin.z;
                        }
                    }
                    // Work around a Unity bug/feature where all scale components must be specified
                    // in the keyframe or the missing fields get set to one.
                    Vector3Patch scale = value?.Scale;
                    if (scale != null && scale.IsPatched())
                    {
                        if (!scale.X.HasValue)
                        {
                            scale.X = Scale.x;
                        }
                        if (!scale.Y.HasValue)
                        {
                            scale.Y = Scale.y;
                        }
                        if (!scale.Z.HasValue)
                        {
                            scale.Z = Scale.z;
                        }
                    }
                    AddVector3Patch(typeof(Transform), "..:translation", time, value?.Position);
                    var ratation = value?.Rotation;
                    var quat     = new Quat(ratation.X.Value, ratation.Y.Value, ratation.Z.Value, ratation.W.Value);
                    var vector3  = quat.GetEuler();
                    AddVector3Patch(typeof(Transform), "..:rotation_degrees", time, new Vector3Patch(vector3));
                    AddVector3Patch(typeof(Transform), "..:scale", time, value?.Scale);
                }

                void AddActorPatch(float time, ActorPatch value)
                {
                    AddTransformPatch(time, value?.Transform.Local);
                }

                void AddKeyframe(MWAnimationKeyframe keyframe)
                {
                    AddActorPatch(keyframe.Time, keyframe.Value);
                    if (animation.Length < keyframe.Time)
                    {
                        animation.Length = keyframe.Time;
                    }
                }

                foreach (var keyframe in keyframes)
                {
                    AddKeyframe(keyframe);
                }

                _animationData[animationName] = new AnimationData()
                {
                    IsInternal = isInternal,
                    Managed    = managed
                };

                float initialTime   = 0f;
                float initialSpeed  = 1f;
                bool initialEnabled = false;

                if (initialState != null)
                {
                    initialTime    = initialState.Time ?? initialTime;
                    initialSpeed   = initialState.Speed ?? initialSpeed;
                    initialEnabled = initialState.Enabled ?? initialEnabled;
                }

                animationPlayer.AddAnimation(animationName, animation);
                animationPlayer.AssignedAnimation = animationName;

                SetAnimationState(animationName, initialTime, initialSpeed, initialEnabled);

                onCreatedCallback?.Invoke();
            });

            continuation.Start();
        }