using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ShotManager : MonoBehaviour
{
    // Start is called before the first frame update
    public float deathTimer;
    public float thrust;
    public Rigidbody rb;
    public bool bounce;
    public Material alienRojo;
    public Material alienAzul;
    public Material alienAmarillo;
    public Material alienVerde;
    public Material alienMorado;
    string thisAlienTag;

    public AudioSource myASource;
    public AudioClip happy;
    public AudioClip sad;

    float delta = 0;
    void Start()
    {
        rb = GetComponent<Rigidbody>();

        gameObject.tag = PlayerMananger.alienTag;
        thisAlienTag = gameObject.tag;
        rb.AddForce(Mathf.Sin(transform.eulerAngles.y * Mathf.PI / 180) * thrust * PlayerMananger.charge, 0, Mathf.Cos(transform.eulerAngles.y * Mathf.PI / 180) * thrust * PlayerMananger.charge, ForceMode.Impulse);

        PlayerMananger.charge = 0;

        switch (gameObject.tag)
        {
            case "alienRojo":
                gameObject.GetComponentInChildren<MeshRenderer>().material = alienRojo;
                break;
            case "alienAzul":
                gameObject.GetComponentInChildren<MeshRenderer>().material = alienAzul;
                break;
            case "alienAmarillo":
                gameObject.GetComponentInChildren<MeshRenderer>().material = alienAmarillo;
                break;
            case "alienVerde":
                gameObject.GetComponentInChildren<MeshRenderer>().material = alienVerde;
                break;
            case "alienMorado":
                gameObject.GetComponentInChildren<MeshRenderer>().material = alienMorado;
                break;
            default:
                break;
        }
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        delta += Time.deltaTime;
        if (delta > deathTimer)
            //
            Destroy(gameObject);
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "asteroide")
        {
            //lose
            Destroy(gameObject);
        }

        switch (gameObject.tag)
        {
            case "alienRojo":
                if (collision.gameObject.tag == "planetaRojo")
                {
                    //win
                    Scene currentScene = SceneManager.GetActiveScene();
                    int cSI = currentScene.buildIndex;
                    SceneManager.LoadScene(++cSI);
                    Destroy(gameObject);
                }

                break;
            case "alienAzul":
                if (collision.gameObject.tag == "planetaAzul")
                {
                    //win
                    Scene currentScene = SceneManager.GetActiveScene();
                    int cSI = currentScene.buildIndex;
                    SceneManager.LoadScene(++cSI);
                    Destroy(gameObject);
                }
                break;
            case "alienAmarillo":
                if (collision.gameObject.tag == "planetaAmarillo")
                {
                    //win
                    Scene currentScene = SceneManager.GetActiveScene();
                    int cSI = currentScene.buildIndex;
                    SceneManager.LoadScene(++cSI);
                    Destroy(gameObject);
                }
                break;
            case "alienVerde":
                if (collision.gameObject.tag == "planetaVerde")
                {
                    //win
                    Scene currentScene = SceneManager.GetActiveScene();
                    int cSI = currentScene.buildIndex;
                    SceneManager.LoadScene(++cSI);
                    Destroy(gameObject);
                }
                break;
            case "alienMorado":
                if (collision.gameObject.tag == "planetaMorado")
                {
                    //win
                    Scene currentScene = SceneManager.GetActiveScene();
                    int cSI = currentScene.buildIndex;
                    SceneManager.LoadScene(++cSI);
                    Destroy(gameObject);
                }
                break;
            default:
                break;
        }
    }
}
