using UnityEngine;
using System.Collections;

public class Ennemy : MonoBehaviour {

        public GameObject boom;
        public GameObject helice;
        public GameObject torpedoPrefab;
        public ParticleSystem traineePrincipale;
        public ParticleSystem traineeHelice;
       

        public AudioClip moteurON;
        public AudioClip torpedoFireSFX;

        public float speed = 50.0f;
        public float tilt;
        public float rotateSpeedHelice;
        public Boundary boundary;

        public AudioSource source1;
        public AudioSource source2;

        
        public bool hadShot = false;
        public int lancementlvl;
        public float missilefired;

        [HideInInspector] public Vector3 vecteurDirecteur;
        

    // Use this for initialization---------------------------------------------------------------------------------------------
    void Start()
        {
        
        lancementlvl = Mathf.FloorToInt(Time.time);
        boom.SetActive(false);  
         }

    // Update is called once per frame--------------------------------------------------------------------------------------
    void Update()
    {
        //ENEMY IA POUR MOUVEMENT***********************************************



        GameObject submarinePlayer = GameObject.Find("submarine(Clone)");
        //ENEMY IA POUR TIR***********************************************
        // ennemy lance la torpille

        if (tag != "Poursuite" && tag!="BigBoss" &&tag!="Vicelard")
        {
            if (submarinePlayer != null && (submarinePlayer.transform.position - transform.position).magnitude < 600.0f && submarinePlayer.transform.position.z < transform.position.z)
            {




                if (hadShot == false && (Mathf.FloorToInt(Time.time) - lancementlvl) % 2 == 0)
                {

                    //Vector3 precision = new Vector3(0, Random.Range(-100, 100.0f), Random.Range(-100, 100.0f));
                    vecteurDirecteur.y = (submarinePlayer.transform.position.y - transform.position.y);
                    vecteurDirecteur.z = -(submarinePlayer.transform.position.z - transform.position.z);
                    GameObject torpedo = Instantiate(torpedoPrefab, (transform.position + new Vector3(7.0f, -13.0f, 30.0f)), transform.rotation) as GameObject;
                    torpedo.GetComponent<EnemyMissile>().SetVaisseau(gameObject);
                    hadShot = true;// a tiré
                    missilefired = Mathf.FloorToInt(Time.time);
                    source2.clip = torpedoFireSFX;
                    source2.Play();
                }

                if (Time.time - missilefired > 1 && hadShot == true)
                {
                    hadShot = false;
                }



            }
        }
        if (tag == "Vicelard")
        {
            if (submarinePlayer != null && (submarinePlayer.transform.position - transform.position).magnitude < 800.0f && submarinePlayer.transform.position.z > transform.position.z)
            {




                if (hadShot == false && (Mathf.FloorToInt(Time.time) - lancementlvl) % 2 == 0)
                {

                    //Vector3 precision = new Vector3(0, Random.Range(-100, 100.0f), Random.Range(-100, 100.0f));
                    vecteurDirecteur.y = (submarinePlayer.transform.position.y - transform.position.y);
                    vecteurDirecteur.z = (submarinePlayer.transform.position.z - transform.position.z);
                    GameObject torpedo = Instantiate(torpedoPrefab, (transform.position + new Vector3(7.0f, -13.0f, 30.0f)), transform.rotation) as GameObject;
                    torpedo.GetComponent<EnemyMissile>().SetVaisseau(gameObject);
                    hadShot = true;// a tiré
                    missilefired = Mathf.FloorToInt(Time.time);
                    source2.clip = torpedoFireSFX;
                    source2.Play();
                }

                if (Time.time - missilefired > 1 && hadShot == true)
                {
                    hadShot = false;
                }
            }
            }

        if (tag == "BigBoss")
        {


            if (submarinePlayer != null && (submarinePlayer.transform.position - transform.position).magnitude < 1200.0f && submarinePlayer.transform.position.z < transform.position.z)
            {




                if (hadShot == false && (Mathf.FloorToInt(Time.time) - lancementlvl) % 2 == 0)
                {
                    //Vector3 precision = new Vector3(0, Random.Range(-100, 100.0f), Random.Range(-100, 100.0f));
                    vecteurDirecteur.y = (submarinePlayer.transform.position.y - transform.position.y);
                    vecteurDirecteur.z = -(submarinePlayer.transform.position.z - transform.position.z);
                    GameObject torpedo = Instantiate(torpedoPrefab, (transform.position + new Vector3(10.0f, -28.0f, 136.0f)), transform.rotation) as GameObject;
                    //Debug.Log("tir1");
                    GameObject torpedoaux1 = Instantiate(torpedoPrefab, (transform.position + new Vector3(17.4f, -32.8f, 27.2f)), transform.rotation) as GameObject;
                    //Debug.Log("tir2");
                    GameObject torpedoaux2 = Instantiate(torpedoPrefab, (transform.position + new Vector3(15.9f, 11.0f, 83.8f)), transform.rotation) as GameObject;
                    //Debug.Log("tir3");
                    torpedo.GetComponent<EnemyMissile>().SetVaisseau(gameObject);
                    torpedoaux1.GetComponent<EnemyMissile>().SetVaisseau(gameObject);
                    torpedoaux2.GetComponent<EnemyMissile>().SetVaisseau(gameObject);
                    hadShot = true;// a tiré
                    missilefired = Mathf.FloorToInt(Time.time);
                    source2.clip = torpedoFireSFX;
                    source2.Play();
                }

                if (Time.time - missilefired > 1 && hadShot == true)
                {
                    hadShot = false;
                }



            }
        }

        if (tag == "Poursuite")
        {
            if (submarinePlayer != null && (submarinePlayer.transform.position - transform.position).magnitude < 600.0f && submarinePlayer.transform.position.z > transform.position.z)
            {




                if (hadShot == false && (Mathf.FloorToInt(Time.time) - lancementlvl) % 2 == 0)
                {

                    //Vector3 precision = new Vector3(0, Random.Range(-100, 100.0f), Random.Range(-100, 100.0f));
                    vecteurDirecteur.y = (submarinePlayer.transform.position.y - transform.position.y);
                    vecteurDirecteur.z = (submarinePlayer.transform.position.z - transform.position.z);
                    GameObject torpedo = Instantiate(torpedoPrefab, (transform.position + new Vector3(7.0f, -13.0f, 30.0f)), transform.rotation) as GameObject;
                    torpedo.GetComponent<EnemyMissile>().SetVaisseau(gameObject);
                    hadShot = true;// a tiré
                    missilefired = Mathf.FloorToInt(Time.time);
                    source2.clip = torpedoFireSFX;
                    source2.Play();
                }

                if (Time.time - missilefired > 1 && hadShot == true)
                {
                    hadShot = false;
                }




            }




        }
    }


    void OnTriggerEnter(Collider collision)
    {

        MoveSubmarine submarinePlayer = collision.gameObject.GetComponent<MoveSubmarine>();

        if (submarinePlayer != null)
        {
            // For all others : kill!
            Destroy(collision.gameObject);

            //HealthSubmarine.HealthPoints -= 100;

            // Destruct myself
            //Destroy(gameObject);

            StartCoroutine("explosion");

        }
    }

    public IEnumerator explosion()
    {

        boom.SetActive(true);
        GetComponent<Renderer>().enabled = false;
        helice.GetComponent<Renderer>().enabled = false;
        
        yield return new WaitForSeconds(1.0f);

        Destroy(gameObject);
        StateMachine.instance.SetState(STATE.GAMEOVER);
    }


    // Update is called once per frame---------------------------------------------------------------------------------------
    void FixedUpdate()
    { 
        
           
            //HELICE*********************************************************************

            if (GetComponent<Rigidbody>().velocity.z != 0)
            {

                helice.transform.Rotate(Vector3.back, GetComponent<Rigidbody>().velocity.z * rotateSpeedHelice);

               
                        traineeHelice.Play();
                        traineePrincipale.Play();
                  

            }




        }
    }


