using System;
using KINEMATION.MagicBlend.Runtime;
using NBC;
using UnityEngine;

namespace NBF
{
    public class PlayerAnimator : MonoBehaviour
    {
        public Animator _Animator;
        public FPlayer Player { get; private set; }

        private bool _isRodLayerEnabled;
        private bool _isInit;
        private PlayerIK _IK;
        private MagicBlending _magicBlending;
        private bool _IsInVehicle;

        #region 参数定义

        // public static readonly int IsSwiming = Animator.StringToHash("Swim");
        //
        // public static readonly int ThrowFar = Animator.StringToHash("ThrowFar");
        //
        // public static readonly int BoatDriving = Animator.StringToHash("BoatDriving");
        //
        // public static readonly int BaitInWater = Animator.StringToHash("BaitInWater");
        //
        // public static readonly int HeldRod = Animator.StringToHash("HeldRod");
        //
        // public static readonly int RodArming = Animator.StringToHash("RodArming");

        public static readonly int Forward = Animator.StringToHash("Forward");

        public static readonly int Turn = Animator.StringToHash("Turn");

        public static readonly int OnGroundHash = Animator.StringToHash("OnGround");
        public static readonly int PrepareThrowHash = Animator.StringToHash("PrepareThrow");
        public static readonly int StartThrowHash = Animator.StringToHash("StartThrow");
        public static readonly int BaitThrownHash = Animator.StringToHash("BaitThrown");
        private static readonly int FishingUpHash = Animator.StringToHash("FishingUp");

        public static readonly string LureRodLayer = "LureRod";
        public static readonly string HandRodLayer = "HandRod";


        public float FishingUp
        {
            get => _Animator.GetFloat(FishingUpHash);
            set => _Animator.SetFloat(FishingUpHash, value);
        }

        public bool OnGround
        {
            get => _Animator.GetBool(OnGroundHash);
            set => _Animator.SetBool(OnGroundHash, value);
        }

        public bool StartThrow
        {
            get => _Animator.GetBool(StartThrowHash);
            set => _Animator.SetBool(StartThrowHash, value);
        }

        public bool BaitThrown
        {
            get => _Animator.GetBool(BaitThrownHash);
            set => _Animator.SetBool(BaitThrownHash, value);
        }

        public bool PrepareThrow
        {
            get => _Animator.GetBool(PrepareThrowHash);
            set => _Animator.SetBool(PrepareThrowHash, value);
        }

        #endregion


        private void Awake()
        {
            Player = GetComponentInParent<FPlayer>();
            _magicBlending = GetComponent<MagicBlending>();
            _Animator = GetComponent<Animator>();
            _Animator.keepAnimatorStateOnDisable = true;
            _IK = GetComponent<PlayerIK>();
            _isInit = true;
            Player.OnFishingSetEquiped += OnFishingSetEquiped_OnRaised;
            Player.OnFishingSetUnequip += OnFishingSetUnequip;
            Player.Data.OnStateChange += PlayerFSMState_OnValueChanged;
        }


        private void OnDestroy()
        {
            Player.OnFishingSetEquiped -= OnFishingSetEquiped_OnRaised;
            Player.OnFishingSetUnequip -= OnFishingSetUnequip;
            Player.Data.OnStateChange += PlayerFSMState_OnValueChanged;
        }


        private void OnFishingSetUnequip()
        {
            _isRodLayerEnabled = false;
            // _IK.SetBipedLeftHandIK(enabled: false, null);
        }


        private void OnFishingSetEquiped_OnRaised(FHandItem item)
        {
            if (item is FRod rod)
            {
                _isRodLayerEnabled = true;
                // var reel = Player.Rod.Reel;
                // _IK.SetBipedLeftHandIK(enabled: false, reel.FingersIKAnchor);
            }
            else
            {
            }
        }


        public void SetLayerWeight(string layer, float weight)
        {
            _Animator.SetLayerWeight(_Animator.GetLayerIndex(layer), weight);
        }

        private void PlayerFSMState_OnValueChanged(PlayerState state)
        {
            // switch (Player.Data.PreviousState)
            // {
            //     case PlayerState.vehicle:
            //         _IsInVehicle = false;
            //         _Animator.SetBool(BoatDriving, value: false);
            //         break;
            //     case PlayerState.swiming:
            //         _Animator.SetBool(IsSwiming, value: false);
            //         break;
            //     case PlayerState.preciseCastIdle:
            //         _Animator.SetBool(PreciseIdle, value: false);
            //         break;
            //     case PlayerState.prepare:
            //         _Animator.SetBool(RodArming, value: false);
            //         break;
            //     case PlayerState.casting:
            //         _Animator.SetBool(ThrowFar, value: false);
            //         break;
            //     case PlayerState.collectFish:
            //         _magicBlending.BlendAsset.globalWeight = 0f;
            //         break;
            // }
            //
            // switch (state)
            // {
            // switch (Player.Data.PreviousState)
            // {
            //     case PlayerState.vehicle:
            //         _IsInVehicle = false;
            //         _Animator.SetBool(BoatDriving, value: false);
            //         break;
            //     case PlayerState.swiming:
            //         _Animator.SetBool(IsSwiming, value: false);
            //         break;
            //     case PlayerState.preciseCastIdle:
            //         _Animator.SetBool(PreciseIdle, value: false);
            //         break;
            //     case PlayerState.prepare:
            //         _Animator.SetBool(RodArming, value: false);
            //         break;
            //     case PlayerState.casting:
            //         _Animator.SetBool(ThrowFar, value: false);
            //         break;
            //     case PlayerState.collectFish:
            //         _magicBlending.BlendAsset.globalWeight = 0f;
            //         break;
            // }
            //
            // switch (state)
            // {
            //     case PlayerState.idle:
            //     case PlayerState.move:
            //         _Animator.SetBool(BaitInWater, value: false);
            //         _Animator.SetBool(HeldRod, value: false);
            //         _Animator.SetBool(ThrowFar, value: false);
            //         _Animator.SetBool(RodArming, value: false);
            //         break;
            //     case PlayerState.prepare:
            //         _Animator.SetBool(RodArming, value: true);
            //         _Animator.SetBool(HeldRod, value: true);
            //         break;
            //     case PlayerState.fishing:
            //         _Animator.SetBool(HeldRod, value: true);
            //         _Animator.SetBool(BaitInWater, value: true);
            //         break;
            //     case PlayerState.vehicle:
            //         _Animator.SetBool(BaitInWater, value: false);
            //         _Animator.SetBool(HeldRod, value: false);
            //         _Animator.SetBool(ThrowFar, value: false);
            //         _Animator.SetBool(RodArming, value: false);
            //         _Animator.SetBool(BoatDriving, value: true);
            //         _IK.SetBipedLeftHandIK(enabled: true);
            //         _IsInVehicle = true;
            //         break;
            //     case PlayerState.vehicleFishing:
            //         _Animator.SetBool(BaitInWater, value: false);
            //         _Animator.SetBool(HeldRod, value: false);
            //         _Animator.SetBool(ThrowFar, value: false);
            //         _Animator.SetBool(RodArming, value: false);
            //         _IsInVehicle = true;
            //         break;
            //     case PlayerState.swiming:
            //         _Animator.SetBool(IsSwiming, value: true);
            //         break;
            //     case PlayerState.collectFish:
            //         _Animator.SetBool(BaitInWater, value: false);
            //         _IK.SetAimIK(enabled: false);
            //         _magicBlending.BlendAsset.globalWeight = 1f;
            //         break;
            //     case PlayerState.preciseCastIdle:
            //         _Animator.SetBool(PreciseIdle, value: true);
            //         break;
            //     case PlayerState.casting:
            //     case PlayerState.baitFlies:
            //     case PlayerState.fight:
            //     case PlayerState.fishView:
            //     case PlayerState.throwFish:
            //     case PlayerState.flyModeDebug:
            //         break;
            // }
        }

        private void LateUpdate()
        {
            // if (Player.Data.State == PlayerState.swiming)
            // {
            //     float value = Mathf.Lerp(_Animator.GetFloat(Forward), Player.Data.Speed / 2.5f,
            //         Time.deltaTime * 5f);
            //     float value2 = Mathf.Lerp(_Animator.GetFloat(Turn), Player.Data.RotationSpeed, Time.deltaTime * 5f);
            //     _Animator.SetFloat(Forward, Mathf.Clamp01(value));
            //     _Animator.SetFloat(Turn, Mathf.Clamp(value2, -1f, 1f));
            // }
            // else
            {
                float value3 = Mathf.Lerp(_Animator.GetFloat(Forward), Player.Data.Speed / 5f,
                    Time.deltaTime * 20f);
                float value4 = Mathf.Lerp(_Animator.GetFloat(Turn), Player.Data.RotationSpeed, Time.deltaTime * 15f);
                _Animator.SetFloat(Forward, Mathf.Clamp01(value3));
                _Animator.SetFloat(Turn, Mathf.Clamp(value4, -1f, 1f));
            }

            // var rod = Vector3.zero;
            // if (Player.Rod)
            // {
            //     rod = Player.Rod.transform.position;
            // }

            _Animator.SetBool(OnGroundHash, _IsInVehicle || Player.Data.IsGrounded);


            var isHandRodLayerEnabled = _isRodLayerEnabled && !Player.Data.IsLureRod ? 1 : 0;

            float handRodLayerWeight = _Animator.GetLayerWeight(_Animator.GetLayerIndex(HandRodLayer));
            SetLayerWeight(HandRodLayer,
                Mathf.MoveTowards(handRodLayerWeight, isHandRodLayerEnabled, Time.deltaTime * 3f));


            var isLureRodLayerEnabled = _isRodLayerEnabled && Player.Data.IsLureRod ? 1 : 0;
            float lureRodLayerWeight = _Animator.GetLayerWeight(_Animator.GetLayerIndex(LureRodLayer));
            SetLayerWeight(LureRodLayer,
                Mathf.MoveTowards(lureRodLayerWeight, isLureRodLayerEnabled, Time.deltaTime * 3f));
        }

        #region 动画事件

        /// <summary>
        /// 抬杆到底动画事件
        /// </summary>
        public void OnRodPowerUp()
        {
        }

        /// <summary>
        /// 开始抛出动画事件
        /// </summary>
        public void OnRodThrowStart()
        {
            if (Player.Fsm.CurrentState is PlayerStateThrow playerStateThrow)
            {
                playerStateThrow.OnRodThrowStart();
            }
        }

        /// <summary>
        /// 抛竿结束动画事件
        /// </summary>
        public void OnRodThrownEnd()
        {
            if (Player.Fsm.CurrentState is PlayerStateThrow playerStateThrow)
            {
                playerStateThrow.OnRodThrownEnd();
            }
        }

        #endregion
    }
}