using UnityEngine;
using UnityEngine.Events;

public class TestBlock : MonoBehaviour
{
    [Header("Visual")]
    [SerializeField] private Renderer targetRenderer;
    [SerializeField] private Color offColor = Color.red;
    [SerializeField] private Color onColor = Color.green;

    [Header("Material")]
    [SerializeField] private string colorPropertyName = "_BaseColor";

    [Header("Optional Auto Wiring")]
    [Tooltip("If assigned, block turns ON while plate is pressed and OFF when released.")]
    [SerializeField] private PressurePlateButton[] pressurePlates;

    [Tooltip("If assigned, block toggles every time the wall button is interacted with.")]
    [SerializeField] private WallInteractButton[] wallButtons;

    private MaterialPropertyBlock m_propertyBlock;
    private bool m_isOn;

    private void Awake()
    {
        if (targetRenderer == null)
        {
            targetRenderer = GetComponentInChildren<Renderer>();
        }

        m_propertyBlock = new MaterialPropertyBlock();
    }

    private void Start()
    {
        SetOff();
    }

    private void OnEnable()
    {
        RegisterEvents();
    }

    private void OnDisable()
    {
        UnregisterEvents();
    }

    public void SetOn()
    {
        m_isOn = true;
        Debug.Log($"{gameObject.name} turned ON");
        ApplyColor(onColor);
    }

    public void SetOff()
    {
        m_isOn = false;
        Debug.Log($"{gameObject.name} turned OFF");
        ApplyColor(offColor);
    }

    public void Toggle()
    {
        if (m_isOn)
            SetOff();
        else
            SetOn();
    }

    // Generic interaction entry point for UnityEvents from any interactable.
    public void TriggerInteraction()
    {
        Toggle();
    }

    public void Activate()
    {
        SetOn();
    }

    public void Deactivate()
    {
        SetOff();
    }

    private void ApplyColor(Color color)
    {
        if (targetRenderer == null)
            return;

        targetRenderer.GetPropertyBlock(m_propertyBlock);
        m_propertyBlock.SetColor(colorPropertyName, color);
        targetRenderer.SetPropertyBlock(m_propertyBlock);
    }

    private void RegisterEvents()
    {
        if (pressurePlates != null)
        {
            for (int i = 0; i < pressurePlates.Length; i++)
            {
                if (pressurePlates[i] == null)
                    continue;

                pressurePlates[i].OnPressed.AddListener(SetOn);
                pressurePlates[i].OnReleased.AddListener(SetOff);
            }
        }

        if (wallButtons != null)
        {
            for (int i = 0; i < wallButtons.Length; i++)
            {
                if (wallButtons[i] == null)
                    continue;

                wallButtons[i].OnInteract.AddListener(Toggle);
            }
        }
    }

    private void UnregisterEvents()
    {
        if (pressurePlates != null)
        {
            for (int i = 0; i < pressurePlates.Length; i++)
            {
                if (pressurePlates[i] == null)
                    continue;

                pressurePlates[i].OnPressed.RemoveListener(SetOn);
                pressurePlates[i].OnReleased.RemoveListener(SetOff);
            }
        }

        if (wallButtons != null)
        {
            for (int i = 0; i < wallButtons.Length; i++)
            {
                if (wallButtons[i] == null)
                    continue;

                wallButtons[i].OnInteract.RemoveListener(Toggle);
            }
        }
    }
}
