using NBC;
using UnityEngine;

namespace NBF
{
    /// <summary>
    /// 抛竿中
    /// </summary>
    public class PlayerThrow : PlayerStateBase
    {
        public override uint StateId => States.Player.Throw;

        public enum Phase
        {
            /// <summary>
            /// 等待动画事件回调
            /// </summary>
            Waiting,

            /// <summary>
            /// 前摇动画
            /// </summary>
            AnimBegin,

            /// <summary>
            /// 抛线动画
            /// </summary>
            ThrowAnim,

            /// <summary>
            /// 结束
            /// </summary>
            Done,
            ErrorDone
        }

        public Phase Stage = Phase.Waiting;

        public float ChargedProgress;

        protected override void onEnter()
        {
            _owner.PlayerAnimatorCtrl.StartThrow = true;

            ChargedProgress = (float)Params.Get(0);
            Debug.Log($"PlayerThrow ChargedProgress={ChargedProgress}");
            Stage = Phase.Waiting;

            _owner.Gears.Reel?.Unlock();
        }

        protected override uint onUpdate()
        {
            CheckStateTimeout(10);
            if (Stage == Phase.AnimBegin)
            {
                AnimBegin();
                ThrowPosition();
            }
            else if (Stage == Phase.ThrowAnim)
            {
                ThrowAnim();
            }
            else if (Stage == Phase.Done)
            {
                return States.Player.Fishing;
            }
            else if (Stage == Phase.ErrorDone)
            {
                return States.Player.Idle;
            }

            return base.onUpdate();
        }

        #region 动画回调

        /// <summary>
        /// 抛竿动画事件
        /// </summary>
        public void RodForceThrowStart()
        {
            // Debug.LogError($"RodForceThrowStart==");
            _owner.PlayerAnimatorCtrl.PrepareThrow = false;
            _owner.PlayerAnimatorCtrl.StartThrow = false;
            Stage = Phase.AnimBegin;
        }

        #endregion

        #region 抛线前摇动画

        private void AnimBegin()
        {
            _owner.PlayerAnimatorCtrl.PrepareThrow = false;
            _owner.PlayerAnimatorCtrl.StartThrow = false;
            _owner.PlayerAnimatorCtrl.LureThrown = true;
            Stage = Phase.ThrowAnim;
        }

        #endregion

        #region 抛竿线飞出相关动画

        private NTask _throwAnim;

        private void ThrowPosition()
        {
            if (_owner.PlayerAnimatorCtrl.ThrowMode == ThrowModeEnum.Float)
            {
                _owner.Gears.Rod.lineHandler.pinchController?.ReleasePinch();
                _throwAnim = new BobThrowAnim(_owner);
                _throwAnim.Run(DefRunner.Scheduler);
                // var lureHookWaterDisplacement = _owner.Gears.Rod.LureHookWaterDisplacement;
                // var angle = 25;
                // Vector3 forward = _owner.transform.forward; // 角色的前方向
                // Vector3 up = _owner.transform.up; // 角色的上方向
                // Vector3 forwardUpDirection =
                //     (forward * Mathf.Cos(angle * Mathf.Deg2Rad) + up * Mathf.Sin(angle * Mathf.Deg2Rad)).normalized;
                //
                // // 施加力
                // lureHookWaterDisplacement.rigidbody.AddForce(forwardUpDirection * 320, ForceMode.Impulse);
            }
            else
            {
                _throwAnim = new LureThrowAnim(_owner);
                _throwAnim.Run(DefRunner.Scheduler);
            }
        }

        private void ThrowAnim()
        {
            if (_throwAnim.IsDone)
            {
                if (_throwAnim.Status == NTaskStatus.Success)
                {
                    if (_owner.PlayerAnimatorCtrl.ThrowMode == ThrowModeEnum.Spin)
                    {
                        SetArm();
                    }

                    Stage = Phase.Done;
                    Debug.LogError($"抛线后，线长度={_owner.Data.lineLength}");
                }
                else
                {
                    Stage = Phase.ErrorDone;
                }
            }
        }

        #endregion
    }
}