
using SerializableTypes;
using System.ComponentModel;
using UnityEngine;

[System.Serializable]
public class SerializableTransform
{
    public Space Space = Space.Self;
    public SVector3 Position = Vector3.zero;
    public SVector3 Rotation = Vector3.zero;
    public SVector3 Scale = Vector3.one;

    public SerializableTransform(){}

    public SerializableTransform(Transform t, Space space = UnityEngine.Space.Self)
    {
        Space = space;
        if (space == Space.World)
        {
            Position = t.position;
            Rotation = t.eulerAngles;
            Scale = t.localScale; //lossyscale is read-only, so can't be used later
        }
        else
        {
            Position = t.localPosition;
            Rotation = t.localEulerAngles;
            Scale = t.localScale;
        }
    }

    public SerializableTransform(SerializableTransform q)
    {
        Space = q.Space;
        Position = q.Position;
        Rotation = q.Rotation;
        Scale = q.Scale;
    }

    public void ApplyTo(Transform t)
    {
        if (Space == Space.World)
        {
            t.position = Position;
            t.eulerAngles = Rotation;
            t.localScale = Scale;
        }
        else
        {
            t.localPosition = Position;
            t.localEulerAngles = Rotation;
            t.localScale = Scale;
        }
    }

    public SerializableTransform LerpWith(SerializableTransform target, float amount)
    {
        return new SerializableTransform()
        {
            Position = Vector3.Lerp(this.Position, target.Position, amount),
            Rotation = Quaternion.Lerp(Quaternion.Euler(this.Rotation), Quaternion.Euler(target.Rotation), amount).eulerAngles,
            Scale = Vector3.Lerp(this.Scale, target.Scale, amount)
        };
    }

    #region JSON
    public bool ShouldSerializeSpace() { return (Space != Space.Self); }
    public bool ShouldSerializePosition() { return (Position != Vector3.zero); }
    public bool ShouldSerializeRotation() { return (Rotation != Vector3.zero); }
    public bool ShouldSerializeScale() { return (Scale != Vector3.one); }
    #endregion
}
