using System;
using Actions;
using UnityEngine;
using UnityEngine.Serialization;

public class UnitSelectedVisual : MonoBehaviour {
    [FormerlySerializedAs("Unit")] [SerializeField] private Unit unit;
    private MeshRenderer meshRenderer;

    private void Awake() => meshRenderer = GetComponent<MeshRenderer>();

    private void Start() {
        UnitActionSystem.Instance.OnSelectedUnitChanged += UnitActionSystem_OnSelectedUnitChanged;
        UpdateVisual();
    }

    private void UnitActionSystem_OnSelectedUnitChanged(object sender, EventArgs e) => UpdateVisual();

    private void UpdateVisual() => meshRenderer.enabled = UnitActionSystem.Instance.SelectedUnit == unit;

    private void OnDestroy() => UnitActionSystem.Instance.OnSelectedUnitChanged -= UnitActionSystem_OnSelectedUnitChanged;
}