using System;
using Assets.Source.Core.Interfaces;
using UnityEngine;

namespace Assets.Source.Core.LevelDesign
{
    [Serializable]
    public class IntegerCurve : IGOValidatable
    {
        public AnimationCurve Curve = AnimationCurve.Linear(0, 0, 1, 1);

        public static implicit operator AnimationCurve(IntegerCurve curve)
        {
            return curve.Curve;
        }

        public void Validate(GameObject source)
        {
            ValidateCurve(source);
        }

        private void ValidateCurve(GameObject entity)
        {
            for (var i = 0; i < Curve.keys.Length; i++)
            {
                var key = Curve.keys[i];
                if (i == 0 || i == Curve.keys.Length - 1)
                {
                    var newKey = key;
                    newKey.value = Mathf.Round(newKey.value / 1) * 1;
                    if (newKey.time < 0)
                        newKey.time = 0;
                    if (newKey.time > 1)
                        newKey.time = 1;
                    Curve.MoveKey(i, newKey);
                }
            }
        }
    }
}
