﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Arugula.UI
{
    public class LevelSelector : MonoBehaviour
    {
        [System.Serializable]
        public class LevelInfo
        {
            public string id;
            public string displayName;
        }

        public Transform buttonParent;
        public GameObject buttonPrefab;
        public LevelInfo[] levels;

        public int firstSelectedIndex = -1;

        List<LevelSelectButton> buttons = new List<LevelSelectButton>();

        private void Start()
        {
            foreach (var i in levels)
            {
                var go = Instantiate(buttonPrefab, buttonParent);
                var btn = go.GetComponent<LevelSelectButton>();
                btn.Initialize(i.id, i.displayName);
                buttons.Add(btn);
            }

            SetFirstSelected();
        }

        private void OnEnable()
        {
            SetFirstSelected();
        }

        void SetFirstSelected()
        {
            if(firstSelectedIndex != -1 && buttons.Count > firstSelectedIndex)
                EventSystem.current.SetSelectedGameObject(buttons[firstSelectedIndex].gameObject);
        }

    }
}
