using UnityEngine;

[RequireComponent(typeof(Rigidbody2D), typeof(Collider2D), typeof(Animator))]
public class PlayerController : MonoBehaviour
{
    [SerializeField]
    private Rigidbody2D m_rigidbody;
    [SerializeField]
    private Collider2D m_collider;
    [SerializeField]
    private Animator m_animator;
    [SerializeField]
    private float m_maxSpeed = 50f;
    [SerializeField]
    private LayerMask m_ennemieLayerMask;
    [SerializeField]
    private LayerMask m_collectibleMask;
    [SerializeField]
    private int m_lifePoint = 3;
    [SerializeField]
    private int m_nbKillToWinLifePoint = 10;
    [SerializeField]
    private float m_invincibleCooldown = 2f;

    [Header("Sounds")]
    [SerializeField]
    private AudioClip[] m_whooshs;

    private int m_kill = 0;
    private int m_currentLifePoint = 3;
    private float m_currentInvincibleCooldown = 0f;

    public void ResetAnim()
    {
        m_animator.SetTrigger("Reset");
        m_kill = 0;
        m_currentLifePoint = m_lifePoint;
        m_currentInvincibleCooldown = 0f;
    }

    public void AddPoint()
    {
        m_kill++;
        if (m_kill == m_nbKillToWinLifePoint)
        {
            m_currentLifePoint++;
            m_kill = 0;
        }
    }

    public int LifePoint
    {
        get { return m_currentLifePoint; }
    }

    #region Unity Callback
    protected void OnEnable()
    {
        m_collider.enabled = true;
        m_animator.SetTrigger("Run");
        m_currentLifePoint = m_lifePoint;
    }

    protected void OnDisable()
    {
        m_rigidbody.velocity = Vector2.zero;
        m_collider.enabled = false;
    }

    protected void Update()
    {
        if (Input.GetButtonDown("FastAttack"))
        {
            m_animator.SetTrigger("FastAttack");
            PlayWhooshs();
        }

        if (m_currentInvincibleCooldown > 0f)
        {
            m_currentInvincibleCooldown -= Time.deltaTime;
        }        
    }

    protected void FixedUpdate()
    {
        // move player horizontaly only.
        float horizontal = Input.GetAxis("Horizontal") * m_maxSpeed;
        float vertical = Input.GetAxis("Vertical") * m_maxSpeed;
        m_rigidbody.velocity = new Vector2(horizontal, vertical);
    }
    #endregion

    protected void OnCollisionEnter2D(Collision2D _collision)
    {
        // check collision with the ennemies.
        if ((m_ennemieLayerMask.value & (1 << _collision.gameObject.layer)) != 0)
        {
            if (GameManager.State != GameManager.GameState.End && m_currentInvincibleCooldown <= 0f)
            {
                m_currentInvincibleCooldown = m_invincibleCooldown;

                m_currentLifePoint--;
                if (m_currentLifePoint <= 0)
                {
                    GameManager.State = GameManager.GameState.End;

                    // play throw up animation.
                    m_animator.SetTrigger("Death");

                    // disable control.
                    enabled = false;
                }
                else
                {
                    _collision.collider.gameObject.SetActive(false);
                    SoundManager.PlayGhostHitted();
                }
            }
        }
        // check collision with collectible.
        else if ((m_collectibleMask.value & (1 << _collision.gameObject.layer)) != 0)
        {
            GameManager.Score++;
            Destroy(_collision.gameObject);
        }
    }

    private void PlayWhooshs()
    {
        SoundManager.PlaySFX(m_whooshs[Random.Range(0, m_whooshs.Length)]);
    }
}
