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

public class DroneSelectionListController : MonoBehaviour {

    List<DroneSelectionController> entries;

    public GameObject prefab;
    public float lineHeight = 15f;

    private void Awake()
    {
        entries = new List<DroneSelectionController>();
    }

    // Update is called once per frame
    void Update () {
        if (entries.Count == Game.instance.drones.Count)
            return;
        if (entries.Count > Game.instance.drones.Count)
        {
            foreach (var entry in entries)
            {
                if (!Game.instance.drones.Contains(entry.dc))
                    Destroy(entry.gameObject);
            }
        }
        if (entries.Count < Game.instance.drones.Count)
        {
            foreach (var drone in Game.instance.drones)
            {
                if (!entries.Any(e => e.dc == drone))
                {
                    var go = Instantiate(prefab);
                    var dsc = go.GetComponent<DroneSelectionController>();
                    dsc.dc = drone;
                    entries.Add(dsc);
                    go.transform.SetParent(transform);
                    go.SetActive(true);
                    var t = go.GetComponent<RectTransform>();
                    if (t.offsetMax.x < t.offsetMin.x)
                    {
                        var tmp = t.offsetMax.x;
                        t.offsetMax = new Vector2(t.offsetMin.x, t.offsetMax.y);
                        t.offsetMin = new Vector2(tmp, t.offsetMin.y);
                    }
                    //lineHeight * (entries.Count - 1) + 60);
                }
            }
        }
	}
}
