
using UnityEngine;

// Nouvelle version du Platform Character Controller de Gwen

public class PlayerController : MonoBehaviour
{ 
    [Header("Main Components")]
    [Tooltip("Position de départ du personnage")] [SerializeField] private Transform spawn;
    [Tooltip("Corps du personnage responsable des forces et mouvements")] [SerializeField] private Rigidbody2D rb2D;
    [Tooltip("Corps du personnage responsable des collisions")] [SerializeField] private Collider2D bodyCollider; 
    [Tooltip("Position du vérificateur de sol nécessaire au saut")] [SerializeField] private Transform groundCheck;
    [Tooltip("Layer à utiliser pour identifier le sol")] [SerializeField] private LayerMask groundMask;
    [Tooltip("Position du vérificateur de mur nécessaire au wall jump")] [SerializeField] private Transform wallCheck;
    [Tooltip("Layer a utiliser pour identifier un mur")] [SerializeField] private LayerMask wallMask;
    [Tooltip("Liste de sons jouable par le joueur (exemple : Saut,Mort)")] [SerializeField] private AudioClip[] cliplist;

    [Header("Move Variables")]
    [Tooltip("Vitesse de marche")] [SerializeField] private float defaultSpeed;
    [Tooltip("Multiplieur amplificateur de vitesse")][Range(1f,3f)] [SerializeField] private float sprintMultiplier;
    [Tooltip("Multiplieur réducteur de vitesse")][Range(0,1f)] [SerializeField] private float crouchMultiplier;
    private float _moveSpeed;
    private float _horizontal; // Movement Input
    private float _vertical; // Crouch Input
    private SpriteRenderer _spriteRenderer; 

    [Header("Jump Variables")]
    [Tooltip("Hauteur du saut")][Min(20)][SerializeField] private float jumpForce;
    [Tooltip("Temps d'attente entre 2 sauts")][SerializeField] private float jumpCooldown;
    [Tooltip("Multiplieur réducteur pour ralentir la vitesse pendant le saut et la distance par la même occasion")][Range(0,1f)] [SerializeField] private float jumpMultiplier;
    private float _defaultJump; 
    private float _lastJump;
    private bool _isGrounded;
    private Collider2D _groundCollider;

    [Header("Death Variables")]
    [Tooltip("Hauteur du saut lors de l'animation de mort")][Min(15)] [SerializeField] private float jumpHeight;
    
    private static bool _isDead = false;


    // [Header("Wall Sliding and Jumping variables")]


    // Start is called before the first frame update
    void Start()
    {
        rb2D = gameObject.GetComponent<Rigidbody2D>();
        if(spawn != null)
        {
            rb2D.position = spawn.position;
        }
        _moveSpeed = defaultSpeed;
        _isGrounded = false;
        _defaultJump = jumpForce;
        _isDead = false;
        _lastJump = Time.time;
        _spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
        
    }

    // Update is called once per frame
    void Update()
    {

        if (_isDead)
        {
            return;
        }
        _horizontal = Input.GetAxisRaw("Horizontal");
        _vertical = Input.GetAxisRaw("Vertical");
        Speed();
    }

    void FixedUpdate()
    {
        if (!_isDead)
        {
            Moving();
            Flip();
            Jumping();
        }
    }

    void Speed()
    {
       
        if (!_isGrounded)
        {
            _moveSpeed = defaultSpeed *jumpMultiplier;
            return;
        }
        if (_vertical < -0.99f || Input.GetButton("Crouch"))
        {
            _moveSpeed = defaultSpeed * crouchMultiplier;
            //anim.CrossFade("Crouch", 0f, 0);
        }
        else if (_horizontal < -.9f || _horizontal > 0.1f)
        {
            if (Input.GetButton("Sprint"))
            {
                _moveSpeed = defaultSpeed * sprintMultiplier;
                //anim.CrossFade("Sprint", 0f, 0);
            }
            else
            {
                //anim.CrossFade("Walk", 0f, 0);
                _moveSpeed = defaultSpeed;
            }
        }
        else
        {
            //anim.CrossFade("Idle", 0f, 0);
        }
        
    }
    void Moving()
    {
        if (_horizontal > 0.1f || _horizontal < -0.99f)
        {

            rb2D.AddForce(new Vector2(_horizontal * _moveSpeed, 0f), ForceMode2D.Impulse);
        }
    }
    void Jumping()
    {
        _isGrounded = Physics2D.OverlapBox(groundCheck.position, new Vector2(0.3f, 0.03f),0f, groundMask) ;
        

        if (_isGrounded && (Input.GetButton("Jump") || _vertical > 0.1f))
        {
            if(Time.time - _lastJump > jumpCooldown)
            {


                _lastJump = Time.time;   
                rb2D.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
                _isGrounded = false;
                //anim.CrossFade("Jump", 0f, 0);
            }
            
        }
    }

    void Flip()
    {
        if(_horizontal < -0.99f)
        {
            _spriteRenderer.flipX = true;
        }
        else if (_horizontal > 0.01f)
        {
            _spriteRenderer.flipX = false;
        }
    }

    void Kill()
    {
        bodyCollider.enabled = false;
        rb2D.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
        _isDead = true;
        //anim.CrossFade("Death", 0f,0);
    }



    public static bool IsDead()
    {
        return _isDead;
    }
    public void IncreaseSpeed(float value)
    {
        defaultSpeed += value;
    }

    public static void Resurrect()
    {
        _isDead = false;
    }

}