using System;
using CameraScripts;
using Unity.Mathematics;
using UnityEngine;

namespace Animators.Leonid_Animator
{
    public class AnimatorHandler : MonoBehaviour
    {
        public Animator anim;
        public bool canRotate;
        
        private int _horizontal;
        private int _vertical;
        public bool isCrouching = false;
        private bool _isJumping;

        private int _crouch;
        private int _jump;
        private int _fired;

        public void Initialize()
        {
            anim = GetComponent<Animator>();
            _vertical = Animator.StringToHash(nameof(_vertical));
            _horizontal = Animator.StringToHash(nameof(_horizontal));
            _crouch = Animator.StringToHash(nameof(_crouch));
            _jump = Animator.StringToHash(nameof(_jump));
            _fired = Animator.StringToHash(nameof(_fired));
        }

        public void UpdateAnimatorValues(float verticalMovement, float horizontalMovement,
            bool pressedJumped, bool pressedCrouching, bool firePressed)
        {
            #region Vertical Movement

            var vertical = 0f;
            if (verticalMovement > 0 && verticalMovement < 0.55)
                vertical = 0.5f;
            else if (verticalMovement > 0.55)
                vertical = 1;
            else if (verticalMovement < 0 && verticalMovement > -0.55)
            {
                vertical = -0.5f;
            }
            else if (verticalMovement < -0.55)
            {
                vertical = -1;
            }
            else
            {
                vertical = 0;
            }
            #endregion
            
            #region Vertical Movement

            var horizontal = 0f;
            if (horizontalMovement > 0 && horizontalMovement < 0.55)
                horizontal = 0.5f;
            else if (horizontalMovement > 0.55)
                horizontal = 1;
            else if (horizontalMovement < 0 && horizontalMovement > -0.55)
            {
                horizontal = -0.5f;
            }
            else if (horizontalMovement < -0.55)
            {
                horizontal = -1;
            }
            else
            {
                horizontal = 0;
            }
            #endregion
            
            anim.SetFloat(_horizontal, horizontal, 0.1f, Time.deltaTime);
            anim.SetFloat(_vertical, vertical, 0.1f, Time.deltaTime);

            if (pressedCrouching == true)
            {
                isCrouching = !isCrouching;
                anim.SetBool(_crouch, isCrouching);
            }
            
            anim.SetBool(_jump, pressedJumped);
            anim.SetBool(_fired, firePressed);
        }
    }
}