using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class ForcePanelScript : MonoBehaviour {
    [SerializeField] private GridLayoutGroup grid;
    [SerializeField] private ForceButtonScript buttonPrefab;

    private Dictionary<ForceType, ForceButtonScript> buttons;

    public void Initialize(IEnumerable<ForceType> forces) {
        foreach (Transform child in grid.transform) {
            GameObject.Destroy(child.gameObject);
        }

        buttons = new Dictionary<ForceType,ForceButtonScript>();
        foreach (ForceType force in forces) {
            ForceType forceCopy = force;
            ForceButtonScript button = GameObject.Instantiate(buttonPrefab);
            button.transform.SetParent(grid.transform, false);
            button.transform.localScale = Vector3.one;
            button.Initialize(force.spriteName, () => ForceActivated(forceCopy));
            buttons[forceCopy] = button;
        }
    }

    private void ForceActivated(ForceType force) {
        Debug.Log("Activating force: " + force.GetType().ToString());
        GameManager.Instance.ForceActivated(force);
    }

    public void ButtonClicked(ForceType force) {
        foreach (ForceType currentForce in buttons.Keys) {
            if (currentForce == force) {
                continue;
            }

            buttons[currentForce].Unclick();
        }
    }
}
