using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Player : MonoBehaviour
{
    public int nivelActual;
    private Vector2[] posicionInicial = new Vector2[10];
    private float[] posicionInicialCamara = new float[10];
    private Rigidbody2D rb;
    private Animator animator;
    private bool enSuelo;
    private bool jetpackOn;


    public AudioSource sonidoJetPack;
    public AudioSource sonidoMuerte;
    public AudioSource sonidoSalto;

    public GameObject particulasMuerte;
    public GameObject particulasChapita;
    public GameObject jetpack;
    public GameObject fuego;
    public Image barraJetpack;
    public Transform camara;
    public Animator animatorCanvas;

    [Header("Stats del Jugador")]
    public float velocidad;
    public float fuerzaSalto;

    public int tapitas;
    public bool tapitaRecogida;

    public float maxDuracionJetpack;
    private float duracionJetpack;

    
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        enSuelo = true;
        posicionInicial[0] = new Vector2(-15, -5);
        posicionInicial[1] = new Vector2(26, 9);
        posicionInicial[2] = new Vector2(66, 7);
        posicionInicial[3] = new Vector2(109, 0);
        posicionInicial[4] = new Vector2(181, -6);
        posicionInicial[5] = new Vector2(224, 9);
        posicionInicial[6] = new Vector2(267, -5);

        posicionInicialCamara[0] = 0;
        posicionInicialCamara[1] = 41;
        posicionInicialCamara[2] = 81;
        posicionInicialCamara[3] = 124;
        posicionInicialCamara[4] = 166;
        posicionInicialCamara[5] = 209;
        posicionInicialCamara[6] = 252;
    }
    
    void Update()
    {
        if(rb.velocity.y > 0)
        {
            animator.SetBool("Saltando", true);
            animator.SetBool("Moviendo", false);
        }
        else
        {
            animator.SetBool("Saltando", false);
            if (rb.velocity.x != 0)
            {
                animator.SetBool("Moviendo", true);
            }
            else
            {
                animator.SetBool("Moviendo", false);
            }
        }
        
        if(Input.GetAxis("Horizontal") > 0)
        {
            GetComponent<SpriteRenderer>().flipX = false;
            jetpack.transform.localPosition = new Vector2(-0.48f, jetpack.transform.localPosition.y);
        }else if(Input.GetAxis("Horizontal")< 0)
        {
            GetComponent<SpriteRenderer>().flipX = true;
            jetpack.transform.localPosition = new Vector2(0.50f , jetpack.transform.localPosition.y);

        }

        rb.velocity = new Vector2 (Input.GetAxis("Horizontal") * velocidad, rb.velocity.y);
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (enSuelo)
            {
                rb.velocity = new Vector2(rb.velocity.x, fuerzaSalto);
                sonidoSalto.Play();
                enSuelo = false;
            }
            else
            {
                duracionJetpack -= 0.2f;
            }
        }
        if (Input.GetKeyUp(KeyCode.Space))
        {
            if (!enSuelo)
            {

                jetpackOn = true;
            }
            rb.gravityScale = 4;
            fuego.SetActive(false);
            sonidoJetPack.Stop();
        }
        if (Input.GetKey(KeyCode.Space))
        {
            if (!enSuelo && jetpackOn && duracionJetpack > 0)
            {
                if (!sonidoJetPack.isPlaying)
                {
                    sonidoJetPack.Play();
                }
                fuego.SetActive(true);
                rb.velocity = new Vector2(rb.velocity.x, fuerzaSalto/1.5f);
                //rb.AddForce(transform.up * 30, ForceMode2D.Force);
                duracionJetpack -= Time.deltaTime;
                rb.gravityScale = 0;
            }
        }

        if (duracionJetpack <= 0)
        {
            jetpack.SetActive(false);
            rb.gravityScale = 4;
        }

        barraJetpack.fillAmount = duracionJetpack/maxDuracionJetpack;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Floor" && collision.transform.parent.tag == "PlataformaRompible")
        {
            collision.transform.parent.gameObject.GetComponent<PlataformaRompible>().RomperPlataforma();
        }
        if (collision.gameObject.tag == "Floor")
        {
            enSuelo = true;
            jetpackOn = false;
            duracionJetpack = maxDuracionJetpack;
            jetpack.SetActive(true);
        }
        if (collision.gameObject.tag == "Enemigo")
        {
            Instantiate(particulasMuerte, transform.position, Quaternion.identity, null);
            transform.position = posicionInicial[nivelActual];
            tapitaRecogida = false;
            sonidoMuerte.Play();
        }
        if (collision.gameObject.tag == "NivelSuperado")
        {
            Invoke("NivelSuperado", 0.20f);
            animatorCanvas.Play("CambiarEscena");
            collision.gameObject.SetActive(false);
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "PlataformaMovil")
        {
            transform.parent = collision.transform;
        }
        if (collision.gameObject.tag == "Recolectable")
        {
            tapitaRecogida = true;
            Instantiate(particulasChapita, transform.position, Quaternion.identity, null);
            collision.gameObject.GetComponent<AudioSource>().Play();
        }
        if(collision.gameObject.tag == "FinalJuego")
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2(0,0);
            animator.enabled = false;
            this.enabled = false;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "PlataformaMovil")
        {
            transform.parent = null;
        }
    }

    private void NivelSuperado()
    {
        nivelActual++;
        if(nivelActual > 6)
        {
            nivelActual = 0;
        }
        transform.position = posicionInicial[nivelActual];
        camara.position = new Vector3(posicionInicialCamara[nivelActual], camara.position.y, camara.position.z);
        if (tapitaRecogida)
        {
            tapitas++;
            tapitaRecogida = false;
        }
    }
    /* ERROR DE SALTO EN EL AIRE
    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Floor")
        {
            enSuelo = false;
            jetpackOn = true;
        }
    }*/
}
