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

public class DisplayScrolls : MonoBehaviour
{
    public string preferredStartScrollName;

    static public List<Scroll> AllowedScrolls;
    public GameObject[] ScrollButtons;
    public GameObject ScrollPrefab;
    public GameObject DetailScreen;

    public Transform scrollscreenContent;

    void Start()
    {
        BuildScrollGUI();
    }

    void BuildScrollGUI()
    {
        AllowedScrolls = GlobalBehaviour.AvailableScrolls
            .Where(s => StageStatic.stage.AllowedScrolls?.Contains(s.@ref) ?? true)
            .ToList();

        //Build Selection-GUI of Scrolls
        ScrollButtons = new GameObject[AllowedScrolls.Count()];
        for (int i = 0; i < AllowedScrolls.Count; i++)
        {
            var obj = Instantiate(ScrollPrefab, scrollscreenContent);
            obj.GetComponent<ScrollClickedScript>().scroll = AllowedScrolls[i];
            obj.GetComponent<ScrollClickedScript>().DetailScreen = this.DetailScreen;
            obj.transform.GetChild(0).gameObject.GetComponent<TextMeshProUGUI>().text = AllowedScrolls[i].label;
            ScrollButtons[i] = obj;
        }

        Scroll preferredStartScroll = AllowedScrolls.Find(x => x.label.Equals(preferredStartScrollName));
        if (preferredStartScroll != null)
            this.DetailScreen.GetComponent<ScrollDetails>().setScroll(preferredStartScroll);
    }
}