using System.Collections;
using UnityEngine;

public class Carbehavior : MonoBehaviour
{
    public AudioSource _carAlarm;

    [Header("Spotlights")]
    public Light spotlight1; // First spotlight
    public Light spotlight2; // Second spotlight
    private bool lightsAreOn = false;

    [Header("Material Emission")]
    public GameObject carObjectWithRenderer; // GameObject holding the Renderer
    private Renderer carRenderer;
    private Material carMaterial;
    private Color originalEmissionColor;

    [Header("Light Intensity Settings")]
    public float maxLightIntensity = 800f;
    public float minLightIntensity = 0f;

    private Coroutine flashingRoutine;

    void Start()
    {
        // Fetch the Renderer component from the provided GameObject
        if (carObjectWithRenderer != null)
        {
            carRenderer = carObjectWithRenderer.GetComponent<Renderer>();

            if (carRenderer != null && carRenderer.material.HasProperty("_EmissionColor"))
            {
                carMaterial = carRenderer.material;
                originalEmissionColor = carMaterial.GetColor("_EmissionColor");
            }
            else
            {
                Debug.LogError("Car Renderer or Emission property not found on the GameObject!");
            }
        }
        else
        {
            Debug.LogError("GameObject holding the Renderer is not assigned!");
        }

        // Ensure lights start at full intensity
        SetLightIntensity(maxLightIntensity);
    }

    // Detect when another collider enters the trigger
    private void OnTriggerEnter(Collider other)
    {
        if (!_carAlarm.isPlaying)
        {
            _carAlarm.Play();
            Debug.Log("Sound Played: Collider entered trigger.");

            // Start flashing lights and emission
            flashingRoutine = StartCoroutine(FlashLightsAndEmission());
        }
    }

    // Detect when another collider exits the trigger
    private void OnTriggerExit(Collider other)
    {
        if (_carAlarm.isPlaying)
        {
            _carAlarm.Stop();
            Debug.Log("Sound Stopped: Collider exited trigger.");

            // Stop flashing lights and emission
            if (flashingRoutine != null)
            {
                StopCoroutine(flashingRoutine);
                flashingRoutine = null;
            }

            // Reset light intensity and material emission
            SetLightIntensity(maxLightIntensity);
            ResetEmission();
        }
    }

    // Coroutine to flash lights and emission
    IEnumerator FlashLightsAndEmission()
    {
        while (true)
        {
            // Toggle lights
            lightsAreOn = !lightsAreOn;
            float currentIntensity = lightsAreOn ? maxLightIntensity : minLightIntensity;
            SetLightIntensity(currentIntensity);

            // Toggle material emission
            if (carMaterial != null && carMaterial.HasProperty("_EmissionColor"))
            {
                if (lightsAreOn)
                {
                    carMaterial.SetColor("_EmissionColor", originalEmissionColor);
                }
                else
                {
                    carMaterial.SetColor("_EmissionColor", Color.black);
                }
            }

            yield return new WaitForSeconds(0.5f);
        }
    }

    // Helper method to set light intensity for both spotlights
    void SetLightIntensity(float intensity)
    {
        if (spotlight1 != null) spotlight1.intensity = intensity;
        if (spotlight2 != null) spotlight2.intensity = intensity;
    }

    // Reset material emission to its original state
    void ResetEmission()
    {
        if (carMaterial != null && carMaterial.HasProperty("_EmissionColor"))
        {
            carMaterial.SetColor("_EmissionColor", originalEmissionColor);
        }
    }
}