/**
 * Copyright(c) Live2D Inc. All rights reserved.
 *
 * Use of this source code is governed by the Live2D Open Software license
 * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
 */
using Live2D.Cubism.Core;
using UnityEngine;
namespace Live2D.Cubism.Framework.LookAt
{
    /// 
    /// Look at parameter.
    /// 
    public sealed class CubismLookParameter : MonoBehaviour
    {
        /// 
        /// Look axis.
        /// 
        [SerializeField]
        public CubismLookAxis Axis;
        /// 
        /// Factor.
        /// 
        [SerializeField]
        public float Factor;
        #region Unity Event Handling
        /// 
        /// Called by Unity. Guesses best settings.
        /// 
        private void Reset()
        {
            var parameter = GetComponent();
            // Fail silently.
            if (parameter == null)
            {
                return;
            }
            // Guess axis.
            if (parameter.name.EndsWith("Y"))
            {
                Axis = CubismLookAxis.Y;
            }
            else if (parameter.name.EndsWith("Z"))
            {
                Axis = CubismLookAxis.Z;
            }
            else
            {
                Axis = CubismLookAxis.X;
            }
            // Guess factor.
            Factor = parameter.MaximumValue;
        }
        #endregion
        #region Interface for Controller
        /// 
        /// Updates and evaluates the instance.
        /// 
        /// Delta to target.
        /// Evaluation result.
        internal float TickAndEvaluate(Vector3 targetOffset)
        {
            var result = (Axis == CubismLookAxis.X)
                ? targetOffset.x
                : targetOffset.y;
            if (Axis == CubismLookAxis.Z)
            {
                result = targetOffset.z;
            }
            return result * Factor;
        }
        #endregion
    }
}