using UnityEngine;
using UnityEngine.UI;

namespace eagle {
    public class CommandButtonPanelController : MonoBehaviour {
        public GameObject buttonsContainer;
        public Button buttonPrefab;
        public EagleGameController gameController;

        void Start() { ClearButtonContents(); }

        public void SetButtonContents(GUIContent[] contents) {
            ClearButtonContents();

            int contentsCount = contents.Length;
            for (int i = 0; i < contentsCount; i++) {
                int buttonIndex = i;
                var newButton = Instantiate(buttonPrefab, buttonsContainer.transform);
                newButton.GetComponentInChildren<RawImage>().texture = contents[buttonIndex].image;
                newButton.onClick.AddListener(() => gameController.SelectButtonIndex(buttonIndex));

                var tooltipTextProvider = newButton.GetComponent<HoveringTooltipTextProvider>();
                tooltipTextProvider.text = contents[buttonIndex].tooltip;
            }
        }

        public void ClearButtonContents() {
            int childCount = buttonsContainer.transform.childCount;
            for (int i = childCount - 1; i >= 0; i--) {
                Destroy(buttonsContainer.transform.GetChild(i).gameObject);
            }
        }
    }
}
