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

public class MegaSpotlightScript : MonoBehaviour
{
    public Light spotlight;
    SphereCollider sphere;

    public float delayBeforeShutdown;
    public float startTimer;
    float timer;
    float startAngle;
    float startRadius;
    Transform player;

    void Awake()
    {
        spotlight = transform.parent.GetComponent<Light>();
        sphere = GetComponent<SphereCollider>();
        SetSpotlight();
        player = GameObject.Find("CHARACTER").transform;
    }

    void SetSpotlight()
    {
        timer = startTimer;
        startAngle = spotlight.spotAngle;
        startRadius = sphere.radius;
    }

    void OnEnable()
    {
        Invoke("Closing",1f+((player.position - transform.position).magnitude/7f));
    }

    void Closing()
    {
        if (transform.parent.gameObject.activeSelf == true) StartCoroutine(CloseSpotlight());
    }


    void OnTriggerStay(Collider objetTouche)
    {
        if (objetTouche.tag == "Player")
        {
            objetTouche.SendMessage("AddScore", 100f*Time.fixedDeltaTime, SendMessageOptions.DontRequireReceiver);
        }
    }


    void OnTriggerEnter(Collider objetTouche)
    {
        if (objetTouche.tag == "Player")
        {
            objetTouche.SendMessageUpwards("HardDancing", true, SendMessageOptions.DontRequireReceiver);
        }
    }

    void OnTriggerExit(Collider objetTouche)
    {
        if (objetTouche.tag == "Player")
        {
             objetTouche.SendMessageUpwards("HardDancing", false, SendMessageOptions.DontRequireReceiver);
        }
    }

    IEnumerator CloseSpotlight()
    {
        while (timer>0)
        {
            timer -= Time.deltaTime;
            spotlight.spotAngle -= (startAngle/startTimer)*Time.deltaTime;
            sphere.radius -= (startRadius/startTimer)*Time.deltaTime;
            yield return null;
        }

        ResetSpotlight();
        gameObject.SendMessageUpwards("SpotlightDisabled", SendMessageOptions.DontRequireReceiver);
        transform.parent.gameObject.SetActive(false);
        //Destroy(this.transform.parent.gameObject);
    }




    void ResetSpotlight()
    {
        timer = startTimer;
        spotlight.spotAngle = startAngle;
        sphere.radius = startRadius;
    }



} // FIN DU SCRIPT
