using UnityEngine;
using UnityEngine.Playables;
using Utility;

namespace CombatLibrary.CombatLibrary.CombatCore.CustomizeTime.CameraShakingAsset
{
    public class CameraShakingBehaviour : PlayableBehaviour
    {
        public Camera Camera;

        public ShakeDirection direction;
        public int type;
        public float totalTime;
        public int count;
        public float qiangDu;
        public AnimationCurve AnimationCurve;

        private float _effectTime;
        private Vector3 _startPos;
        private bool _inEffect;
        private float cosAdd;
        private float currCosTime;
        
        public override void OnBehaviourPlay(Playable playable, FrameData info)
        {
            if (Camera == null)
            {
                return;
            }
            _startPos = Camera.transform.position;
            _effectTime = 0;
            _inEffect = true;
            cosAdd = 360F*count / totalTime;
            currCosTime = 0;
        }


        public override void ProcessFrame(Playable playable, FrameData info, object playerData)
        {
            if (Camera == null)
            {
                return;
            }

            ShakingPos(info.deltaTime);

            // Debug.Log("ProcessFrame" + Mathf.Cos(271 * Mathf.Deg2Rad));
        }

        private void ShakingPos(float time)
        {
            if (!_inEffect)
            {
                return;
            }

            _effectTime += time;
            currCosTime += time * cosAdd;
            float value = Mathf.Cos((90 + currCosTime) * Mathf.Deg2Rad);
            // Debug.Log(value);
            float _strength = AnimationCurve.Evaluate(_effectTime / totalTime);


            switch (direction)
            {
                case ShakeDirection.LeftRightUpDown:
                    Camera.transform.position = _startPos + new Vector3(1,1,0)* value* _strength*qiangDu;
                    break;
                case ShakeDirection.LeftRight:
                    Camera.transform.position = _startPos + new Vector3(1,0,0)* value* _strength*qiangDu;
                    break;
                case ShakeDirection.UpDown:
                    Camera.transform.position = _startPos +  new Vector3(0,1,0)* value* _strength*qiangDu;
                    break;
                case ShakeDirection.FrontBack:
                    Camera.transform.position = _startPos +  new Vector3(0,0,1)* value* _strength*qiangDu;
                    break;
            }

            if (_effectTime > totalTime)
            {
                _inEffect = false;
                Camera.transform.position = _startPos;
            }
        }
    }
}