using System;
using NaughtyAttributes;
using UnityEngine;

namespace EscapeRoomEngine.Desert.Runtime.Dispenser
{
    [Serializable]
    public struct DispenserLightRow
    {
        public bool[] lights;
    }
    
    /// <summary>
    /// This component controls the grid of lights on the dispenser.
    /// </summary>
    public class DispenserLights : MonoBehaviour
    {
        [Tooltip("Control the pattern of lights on a dispenser.")]
        [SerializeField]
        private DispenserLightRow[] lights;
        [BoxGroup("Internal")] [SerializeField]
        private GameObject lightPrefab;
        [BoxGroup("Internal")] [SerializeField]
        private Vector2Int gridDimensions;
        [BoxGroup("Internal")] [SerializeField]
        private Vector2 lightOffset;

        private void Start()
        {
            for (var y = 0; y < gridDimensions.y; y++)
            {
                var row = lights[y].lights;
                for (var x = 0; x < gridDimensions.x; x++)
                {
                    var instance = Instantiate(lightPrefab, transform, false);
                    instance.transform.localPosition = new Vector3(-x * lightOffset.x, 0, y * lightOffset.y);
                    instance.GetComponent<Emission>().active = row[x];
                }
            }
        }
    }
}