/**
 * 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.Physics
{
    /// 
    /// Physics simulation controller.
    /// 
    [CubismMoveOnReimportCopyComponentsOnly]
    public class CubismPhysicsController : MonoBehaviour, ICubismUpdatable
    {
        /// 
        /// Simulation target rig.
        /// 
        private CubismPhysicsRig Rig
        {
            get { return _rig; }
            set { _rig = value; }
        }
        [SerializeField]
        private CubismPhysicsRig _rig;
        /// 
        /// Cubism model parameters for simulation.
        /// 
        public CubismParameter[] Parameters { get; private set; }
        /// 
        /// Model has update controller component.
        /// 
        [HideInInspector]
        public bool HasUpdateController { get; set; }
        public int ExecutionOrder
        {
            get { return CubismUpdateExecutionOrder.CubismPhysicsController; }
        }
        public bool NeedsUpdateOnEditing
        {
            get { return false; }
        }
        public void OnLateUpdate()
        {
            var deltaTime = Time.deltaTime;
            // Use fixed delta time if required.
            if (CubismPhysics.UseFixedDeltaTime)
            {
                deltaTime = Time.fixedDeltaTime;
            }
            // Evaluate rig.
            Rig.Evaluate(deltaTime);
        }
        /// 
        /// Sets rig and initializes .
        /// 
        /// 
        public void Initialize(CubismPhysicsRig rig)
        {
            Rig = rig;
            Awake();
        }
        #region Unity Event Handling
        /// 
        /// Called by Unity or . Initializes  if  exists.
        /// 
        public void Awake()
        {
            // Check rig existence.
            if (Rig == null)
            {
                return;
            }
            // Initialize rig.
            Rig.Controller = this;
            for (var i = 0; i < Rig.SubRigs.Length; ++i)
            {
                Rig.SubRigs[i].Rig = Rig;
            }
            Parameters = this.FindCubismModel().Parameters;
            Rig.Initialize();
        }
        /// 
        /// Called by Unity.
        /// 
        public void Start()
        {
            // Get cubism update controller.
            HasUpdateController = (GetComponent() != null);
        }
        /// 
        /// Called by Unity. Updates controller.
        /// 
        /// Must be call after animation update.
        private void LateUpdate()
        {
            if (!HasUpdateController)
            {
                OnLateUpdate();
            }
        }
    #endregion
    }
}