using NBC;
using UnityEngine;

namespace NBF
{
    /// <summary>
    /// 闲置状态
    /// </summary>
    public class PlayerIdle : PlayerStateBase
    {
        public override uint StateId => States.Player.Idle;

        public enum Phase
        {
            /// <summary>
            /// 完全闲置的
            /// </summary>
            Idle,

            /// <summary>
            /// 准备拿东西
            /// </summary>
            PrepareTake,

            /// <summary>
            /// 拿着东西
            /// </summary>
            TakeItem,
        }

        public Phase Stage = Phase.Idle;

        private bool _nextState = false;

        protected override void onEnter()
        {
            _nextState = false;
            _owner.Data.reelSpeed = 0;
            _owner.PlayerAnimatorCtrl.LureThrown = false;
            _owner.MinorArm.IK.enabled = false;
            if (_owner.Gears.Rod && _owner.Gears.Reel)
            {
                _owner.Gears.Reel.Unlock(false);
            }


            Stage = Phase.Idle;

            InputManager.OnOp1Action += OnOp1Action;
        }

        protected override void onExit()
        {
            base.onExit();
            InputManager.OnOp1Action -= OnOp1Action;
        }

        private void OnOp1Action(bool performed)
        {
            if (Stage == Phase.TakeItem)
            {
                if (_owner.Gears.Rod)
                {
                    //如果准备好了杆子，则可进入抛竿
                    if (performed)
                    {
                        _nextState = true;
                    }
                }
            }
        }

        protected override uint onUpdate()
        {
            if (Stage == Phase.Idle)
            {
                if (_owner && _owner.Gears.Rod)
                {
                    Stage = Phase.PrepareTake;
                }
            }
            else if (Stage == Phase.PrepareTake)
            {
                if (_owner.Data.currentGear.Type == GearType.Pole)
                {
                    var handPoint = _owner.MinorArm.LeftRigMagnet;
                    var ret = _owner.PutLineToLeftHandHelper(handPoint, 3f);

                    if (ret)
                    {
                        _owner.PlayerAnimatorCtrl.RodReady = true;
                        Stage = Phase.TakeItem;
                    }
                }
                else
                {
                    _owner.PlayerAnimatorCtrl.RodReady = true;
                    Stage = Phase.TakeItem;
                }
            }
            else if (Stage == Phase.TakeItem)
            {
                if (_owner.Gears.Rod)
                {
                    //如果准备好了杆子，则可进入抛竿
                    // if (_nextState)
                    // {
                    //     return States.Player.WaitThrow;
                    // }
                }
            }


            return States.None;
        }
    }
}