//----------------------------------------------
//            Behaviour Machine
// Copyright © 2014 Anderson Campos Cardoso
//----------------------------------------------

using UnityEngine;
using System.Collections;

namespace BehaviourMachine {

    /// <summary>
    /// Sets the value of a float parameter.
    /// </summary>
    [NodeInfo ( category = "Action/Mecanim/",
                icon = "Animator",
                description = "Sets the value of a float parameter")]
    public class AnimatorSetFloat : ActionNode {

        /// <summary>
        /// A game object with an Animator in it.
        /// </summary>
        [VariableInfo(requiredField = false, nullLabel = "Use Self", tooltip = "A game object with an Animator in it")]
        public GameObjectVar gameObject;

        /// <summary>
        /// Parameter name.
        /// </summary>
        [VariableInfo (tooltip = "Parameter name")]
        public StringVar parameterName;

        /// <summary>
        /// The new value for the parameter.
        /// </summary>
        [VariableInfo(tooltip = "The new value for the parameter")]
        public FloatVar newValue;

        [System.NonSerialized]
        Animator m_Animator = null;

        public override void Reset () {
            gameObject = new ConcreteGameObjectVar(this.self);
            parameterName = new ConcreteStringVar();
            newValue = new ConcreteFloatVar();
        }

        public override Status Update () {
            // Get the animator
            if (m_Animator == null || m_Animator.gameObject != gameObject.Value)
                m_Animator = gameObject.Value != null ? gameObject.Value.GetComponent<Animator>() : null;

            // Validate Members
            if (m_Animator == null || parameterName.isNone || newValue.isNone)
                return Status.Error;

            m_Animator.SetFloat(parameterName.Value, newValue.Value);
            return Status.Success;
        }
    }
}