using System.Collections.Generic;
using Intrepid.Utilities;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Temporary
{
    public sealed class MeshColorSetter : MonoBehaviour
    {
        [GroupInjections, SerializeField] private List<Renderer> meshRenderers;
        [GroupConfigurations, SerializeField] private Color color = Color.orange;

        private static readonly int BaseColor = Shader.PropertyToID("_BaseColor");
        public Color Color
        {
            get => color;
            set
            {
                color = value;
                Paint();
            }
        }

        private void Awake()
        {
            Paint();
        }

        [GroupDebug, Button(ButtonSizes.Medium)]
        private void Detect()
        {
            var meshRendererArray = GetComponentsInChildren<Renderer>();
            if (meshRendererArray.Length > 0)
            {
                meshRenderers.AddRange(meshRendererArray);
            }
        }
        
        [GroupDebug, Button(ButtonSizes.Medium)]
        private void Paint()
        {
            foreach (var meshRenderer in meshRenderers)
            {
                var block = new MaterialPropertyBlock();
                meshRenderer.GetPropertyBlock(block);
                block.SetColor(BaseColor, color);
                meshRenderer.SetPropertyBlock(block);
            }
        }
    }
}