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

public class NodeHolder : MonoBehaviour
{
    public DestroyableObject[] nodes { get; private set; }

    [SerializeField] private DestroyableObject stackNodeObject = null;

    private int index;

    public void MakeNodes(int numberOfNodes)
    {
        if(nodes == null)
            nodes = new DestroyableObject[numberOfNodes];
        for (int i = 0; i < nodes.Length; i++)
        {
            nodes[i]?.Kill();
        }
        nodes = new DestroyableObject[numberOfNodes];
        // The list is now empty
        float nodeHeight = stackNodeObject.gameObject.GetComponent<StackNode>().GetNodeHeight();
        float nodeHeightDifference = stackNodeObject.gameObject.GetComponent<StackNode>().GetNodeHeightDifference();
        for (int i = 0; i < numberOfNodes; i++)
        {
            DestroyableObject node = Instantiate(stackNodeObject);
            node.transform.parent = gameObject.transform;
            node.transform.localPosition = new Vector3(0f, i * (nodeHeight + nodeHeightDifference), 0f);
            nodes[i] = node;
        }
        index = 0;
    }

    public void RemoveBottomNode()
    {
        if (nodes.Length != 0)
        {
            Destroy(nodes[index].gameObject);
            nodes[index] = null;
            index++;
        }
    }

    public DestroyableObject GetBottomNode()
    {
        return nodes[index];
    }

    public bool AreNodesFinished()
    {
        return (index == nodes.Length);
    }
}
