using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UI;
using static UnityEngine.EventSystems.EventTrigger;

public class FleetUI : MonoBehaviour
{
    public GameObject UI; //Container for every UI part
    public GameObject Camera;
    public GameObject entryPrefab; // Prefab for the entry UI element
    public Transform contentParent; // Parent transform for the content area
    public List<GameObject> EntryList;


    public FleetManager fleetManager;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void SwitchFleetManagerUI() 
    {
        if (UI.activeInHierarchy == true)
        {

            DeleteOldEntries();
            UI.SetActive(false);
        }
        else
        {
            UpdateUI();
            UI.SetActive(true);
            
        }
    }
    void DeleteOldEntries() 
    {
        foreach (GameObject f in EntryList)
        {
            Destroy(f);
        }
        EntryList.Clear();
    }
    public void UpdateUI()
    {
        DeleteOldEntries();
        fleetManager.UpdateFleetsList();

        Vector3 v = new Vector3();
        int id = 0;
        foreach (Fleet fleet in fleetManager.Fleets)
        {
            GameObject entryObject = Instantiate(entryPrefab, contentParent);
            EntryList.Add(entryObject);
            v.y = (-1) * id * (entryPrefab.GetComponent<RectTransform>().sizeDelta.y);
            entryObject.transform.localPosition = v;
            EntryUI entryUI = entryObject.GetComponent<EntryUI>();
            
            // Set the information for the entry
            entryUI.SetEntryInfo(fleet, this.GetComponent<FleetManager>());
            entryUI.IDText.text = id.ToString();
            id++;
        }
    }    
}
