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

public class TurnMeshRedOnSelect : MonoBehaviour
{
    [SerializeField] private GameEvent _selectableUpdateEvent = default;
    [SerializeField] private Selectable _selectable = default;
    [SerializeField] private List<MeshRenderer> _meshesToChange = new List<MeshRenderer>();
    [SerializeField] private Material _normalMaterial = default;
    [SerializeField] private Material _selectedMaterial = default;


    private void OnEnable()
    {
        _selectableUpdateEvent.AddListener(HandleSelectableChange);
    }

    private void OnDisable()
    {
        _selectableUpdateEvent.RemoveListener(HandleSelectableChange);
    }

    private void HandleSelectableChange()
    {
        if (_selectable.Selected)
        {
            foreach (var mesh in _meshesToChange)
            {
                mesh.material = _selectedMaterial;
            }
        }
        else
        {
            foreach (var mesh in _meshesToChange)
            {
                mesh.material = _normalMaterial;
            }
        }
    }
}
