using UnityEngine;

namespace NBF
{
    public class PlayerFishing : PlayerStateBase
    {
        public override uint StateId => States.Player.Fishing;


        protected override void onExit()
        {
            _owner.PlayerAnimatorCtrl.PodsakAction = false;
        }

        protected override void onEnter()
        {
            Debug.Log("进入钓鱼中");

            if (_owner.PlayerAnimatorCtrl.ThrowMode == ThrowModeEnum.Spin)
            {
                _owner.PlayerAnimatorCtrl.PodsakAction = true;
                _owner.PlayerAnimatorCtrl.Telestick = false;
            }
            else
            {
                _owner.PlayerAnimatorCtrl.PodsakAction = false;
                _owner.PlayerAnimatorCtrl.Telestick = true;
            }
        }

        protected override uint onUpdate()
        {
            var ret = States.None;
            var isUpRod = false;
            var isSubLine = false;

            if (Input.GetKeyDown(KeyCode.P))
            {
                return States.Player.Idle;
            }

            if (InputManager.IsOp1)
            {
                if (_owner.PlayerAnimatorCtrl.ThrowMode == ThrowModeEnum.Float)
                {
                    //抬杆
                    isUpRod = true;
                }
                else if (_owner.PlayerAnimatorCtrl.ThrowMode == ThrowModeEnum.Spin)
                {
                    //收线
                    isSubLine = true;
                }
            }

            if (InputManager.IsOp2)
            {
                if (_owner.PlayerAnimatorCtrl.ThrowMode == ThrowModeEnum.Spin)
                {
                    //抬杆
                    isUpRod = true;
                }
            }

            if (isUpRod || _owner.PlayerAnimatorCtrl.FishingFinal > 0)
            {
                var upForce = 1;
                var addNum = upForce * Time.deltaTime;
                if (!isUpRod)
                {
                    addNum *= -1;
                }
                else
                {
                    addNum *= 0.5f;
                }

                // Debug.Log($"addNum={addNum}");
                _owner.PlayerAnimatorCtrl.FishingFinal += addNum;
                Debug.LogError($"ishingFinal={_owner.PlayerAnimatorCtrl.FishingFinal}");
                if (_owner.PlayerAnimatorCtrl.FishingFinal >= 1)
                {
                    _owner.PlayerAnimatorCtrl.FishingFinal = 1;
                }
                else if (_owner.PlayerAnimatorCtrl.FishingFinal < 0)
                {
                    _owner.PlayerAnimatorCtrl.FishingFinal = 0;
                }

                if (_owner.PlayerAnimatorCtrl.FishingFinal >= 0.8f)
                {
                    ret = CheckTackFish();
                }

                _owner.PlayerAnimatorCtrl.PullUpRod = true;
            }
            else
            {
                _owner.PlayerAnimatorCtrl.PullUpRod = false;
            }

            if (isSubLine)
            {
                var subLineSpeed = 2;
                var addNum = subLineSpeed * Time.deltaTime;
                if (_owner.Data.lineLength > 0.4f)
                {
                    _owner.Data.lineLength -= addNum;
                }

                _owner.Data.reelSpeed = 1;
                if (_owner.Data.lineLength <= 0.4f)
                {
                    _owner.Data.lineLength = 0.4f;
                    ret = CheckTackFish();
                }
            }
            else
            {
                _owner.Data.reelSpeed = 0;
            }

            return ret;
        }

        #region 检查上鱼或者返回待机

        private uint CheckTackFish()
        {
            return States.Player.Idle;
        }

        #endregion
    }
}