using System.Collections.Generic;
using UnityEngine;

public class Tile
{

    public Index Index;
    public float TargetY;
    public int Upgrade;
    
    public Transform Element;
    public List<GameObject> Nodes = new();
    public List<GameObject> Buildings = new();

    public Tile(Index index, Transform element, float targetY)
    {   
        Index = index;
        Element = element;
        TargetY = targetY;
    }

    public void Update()
    {
        if (TargetY > Element.position.y)
        {
            var offset = System.Math.Min(1f * Time.deltaTime, TargetY - Element.position.y);
            if (offset > 0f)
            {
                Element.localPosition = new Vector3(Element.localPosition.x, Element.localPosition.y + offset, Element.localPosition.z);
            }
        }
    }

    public void Remove(GameObject node)
    {
        for (var i = Nodes.Count - 1; i >= 0; i--)
            if (Nodes[i] == node)
            {
                Nodes.RemoveAt(i);
                //Debug.Log($"Removed node {i} from tile {Index.Identifier}");
                break;
            }
    }

    public void Build(int buildingID, GameObject building)
    {
        Buildings.Add(building);
    }

}
