using System;
using RootMotion.FinalIK;
using UnityEngine;

namespace NBF
{
    public class PlayerIK : MonoBehaviour
    {
        public enum UpdateType
        {
            Update = 0,
            FixedUpdate = 1,
            LateUpdate = 2,
            Default = 3
        }

        public UpdateType UpdateSelected;

        // [SerializeField] private Transform _LeftHandTransform;

        private LookAtIK _LookAtIK;

        // private AimIK _AimIK;

        // private FullBodyBipedIK _FullBodyIK;

        // private ArmIK _ArmIK;

        // private bool _isLeftHandEnabled;

        // private bool _isRightHandEnabled;

        // public bool isAimEnabled;

        private bool _isFishingLeftArmEnabled;

        [SerializeField] private float transitionWeightTimeScale = 1f;

        // public Transform CurrentTarget => _FullBodyIK.solver.leftHandEffector.target;

        // public Transform LeftHandTransform => _LeftHandTransform;

        private void Awake()
        {
            _LookAtIK = GetComponent<LookAtIK>();
            // _AimIK = GetComponent<AimIK>();
            // _FullBodyIK = GetComponent<FullBodyBipedIK>();
            // _ArmIK = GetComponent<ArmIK>();
            // SetAimIK(enabled: false);
        }

        

        public void SetBipedIK(bool enabled)
        {
        }

        public void SetFishingLeftArm(bool enabled)
        {
            _isFishingLeftArmEnabled = enabled;
        }

        // public void SetFishingLeftArm(bool enabled, Transform target)
        // {
        //     _isFishingLeftArmEnabled = enabled;
        //     _ArmIK.solver.arm.target = target;
        // }

        // public void SetBipedLeftHandIK(bool enabled, bool instant = false)
        // {
        //     _isLeftHandEnabled = enabled;
        //     if (instant)
        //     {
        //         _FullBodyIK.solver.leftArmMapping.weight = (enabled ? 1f : 0f);
        //     }
        // }

        // public void SetBipedRightHandIK(bool enabled, bool instant = false)
        // {
        //     _isRightHandEnabled = enabled;
        //     if (instant)
        //     {
        //         _FullBodyIK.solver.rightArmMapping.weight = (enabled ? 1f : 0f);
        //     }
        // }

        // public void SetBipedLeftHandIK(bool enabled, Transform target, bool instant = false)
        // {
        //     _isLeftHandEnabled = enabled;
        //     _FullBodyIK.solver.leftHandEffector.target = target;
        //     if (instant)
        //     {
        //         _FullBodyIK.solver.leftArmMapping.weight = (enabled ? 1f : 0f);
        //     }
        // }

        // public void SetBipedRightHandIK(bool enabled, Transform target, bool instant = false)
        // {
        //     _isRightHandEnabled = enabled;
        //     _FullBodyIK.solver.rightHandEffector.target = target;
        //     if (instant)
        //     {
        //         _FullBodyIK.solver.rightArmMapping.weight = (enabled ? 1f : 0f);
        //     }
        // }

        // public void SetAimIK(bool enabled)
        // {
        //     isAimEnabled = enabled;
        // }

        private void Update()
        {
            if (UpdateSelected == UpdateType.Update)
            {
                IKUpdateHandler();
            }
        }

        private void FixedUpdate()
        {
            if (UpdateSelected == UpdateType.FixedUpdate)
            {
                IKUpdateHandler();
            }
        }

        private void LateUpdate()
        {
            if (UpdateSelected == UpdateType.LateUpdate)
            {
                IKUpdateHandler();
            }
        }

        private void IKUpdateHandler()
        {
            // _AimIK.UpdateSolverExternal();
            _LookAtIK.UpdateSolverExternal();
            // _FullBodyIK.UpdateSolverExternal();
            // _FullBodyIK.solver.Update();
            // _AimIK.solver.IKPositionWeight = Mathf.MoveTowards(_AimIK.solver.IKPositionWeight, isAimEnabled ? 1f : 0f,
            //     Time.deltaTime * transitionWeightTimeScale);
            // _FullBodyIK.solver.leftArmMapping.weight = Mathf.MoveTowards(_FullBodyIK.solver.leftArmMapping.weight,
            //     _isLeftHandEnabled ? 1f : 0f, Time.deltaTime * transitionWeightTimeScale);
            // _FullBodyIK.solver.rightArmMapping.weight = Mathf.MoveTowards(_FullBodyIK.solver.rightArmMapping.weight,
            //     _isRightHandEnabled ? 1f : 0f, Time.deltaTime * transitionWeightTimeScale);
            // _FullBodyIK.solver.IKPositionWeight = Mathf.MoveTowards(_FullBodyIK.solver.IKPositionWeight,
            //     _isLeftHandEnabled ? 1f : 0f, Time.deltaTime * transitionWeightTimeScale);
            // _ArmIK.solver.IKPositionWeight = Mathf.MoveTowards(_ArmIK.solver.IKPositionWeight,
            //     _isFishingLeftArmEnabled ? 1f : 0f, Time.deltaTime * transitionWeightTimeScale);
            // _ArmIK.solver.IKRotationWeight = Mathf.MoveTowards(_ArmIK.solver.IKRotationWeight,
            //     _isFishingLeftArmEnabled ? 1f : 0f, Time.deltaTime * transitionWeightTimeScale);
        }
    }
}