using UnityEngine;

public class RockDynamite : InteractableObject {

    [SerializeField] private Object bateauCasse = null;

    private static GameObject dynamite = null;
    private static GameObject etincelleDynamite = null;
    private static AudioSource sonDynamite = null;
    private static AudioClip[] sonsDynamite = null;
    private static GameObject explosion = null;
    private static GameObject rocks = null;
    private static GameObject boat = null;
    private static float fps = 50f;
    private static float vitesseRocksAvance = 60f;
    private static float vitesseRocksLeft = 30f;
    private static GameObject BateauFume = null;
    private bool isDynamiteWithPlayer = true;


    protected override void Start() {
        base.Start();
        maxDistance = 8f;
        sonDynamite = GameObject.Find("AudioDynamiteRock").GetComponent<AudioSource>();
        sonsDynamite = Resources.LoadAll<AudioClip>("Sounds/Dynamite");
        dynamite = GameObject.Find("Dynamite");
        etincelleDynamite = dynamite.GetComponentInChildren<ParticleSystem>().gameObject;
        explosion = GameObject.Find("Explosion");
        rocks = GameObject.Find("Rocks");
        BateauFume = GameObject.Find("FumeeBoat");
        boat = GameObject.Find("Boat");
        BateauFume.SetActive(false);
        explosion.SetActive(false);
        etincelleDynamite.SetActive(false);
    }

    protected override void DoActionUp()                    // Pose dynamite et s'allume auto
    {
        base.DoActionUp();
        dynamite.SetActive(true);
        sonDynamite.clip = sonsDynamite[1];
        sonDynamite.Play();
        Invoke("exploser", 7);
        isVisible = false;
        myMat.shader = originalShader;
        etincelleDynamite.SetActive(true);
    }
    void exploser()                                         //Explosion --> declencher la chute des Rocks 
    {
        sonDynamite.Stop();
        sonDynamite.clip = sonsDynamite[0];
        sonDynamite.Play();
        SoundManager.PlaySound("pierreRoule");
        explosion.SetActive(true);
        Destroy(dynamite);
        Transform[] rockschildren = rocks.GetComponentsInChildren<Transform>();
        for(int i = 1; i<rockschildren.Length; i++)
        {
            Rigidbody rb = rockschildren[i].gameObject.AddComponent<Rigidbody>();
            rb.mass = 1000;
        }

        InvokeRepeating("MoveRocks", 0, 1 / fps);
    }
    void MoveRocks ()
    {
        rocks.transform.Translate(-Vector3.forward * Time.deltaTime * vitesseRocksAvance);
        rocks.transform.Translate(Vector3.left * Time.deltaTime * vitesseRocksLeft);
    }
    void OnCollisionEnter(Collision bateau)
    {
        if (bateau.transform.tag == "Boat")
        {
            SoundManager.PlaySound("BateauBreak");
            BateauFume.SetActive(true);
            Invoke("SwitchBoat", 4f);
        }
    }
    void SwitchBoat()
    {
        Instantiate(bateauCasse, new Vector3(27.38653f, 20.33f, -56.98233f), Quaternion.Euler(-0.7998047f, -0.7525635f, -2.791687f));
        Destroy(boat);
        Destroy(rocks);
    }

    protected override void Update()
    {
        base.Update();
        if (isDynamiteWithPlayer)
        {
            dynamite.transform.position = Camera.main.transform.position + 3 * Camera.main.transform.forward;
            if (Vector3.Distance(transform.position, dynamite.transform.position) < maxDistance && (Input.GetMouseButtonDown(0) || Input.GetButtonDown("Action")))
            {
                isDynamiteWithPlayer = false;
            }
        }
    }
}