using REST_JSON_API;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using static CommunicationEvents;

public class ScrollDetails : ScrollView
{
    public static ScrollDetails Instance
    {
        get => _Instance;
        set
        {
            if (_Instance != value)
                Destroy(_Instance);

            _Instance = value;
        }
    }
    private static ScrollDetails _Instance;

    public WorldCursor cursor;
    public GameObject parameterDisplayPrefab;
    public GameObject mmtAnswerPopUp;
    private PopupBehavior Popup;

    public static List<RenderedScrollFact> ParameterDisplays { get; private set; }

    void Awake()
    {
        Instance = this;

        if (cursor == null)
            cursor = FindObjectOfType<WorldCursor>();

        Popup = mmtAnswerPopUp.GetComponent<PopupBehavior>();
        Popup.gameObject.SetActive(true); // force Awake
    }

    private void OnEnable()
    {
        ActiveScroll.OnScrollChanged.AddListener(ShowScroll);
        ActiveScroll.OnFactAssignmentUpdated.AddListener(OnFactAssignmentUpdated);
        ActiveScroll.OnScrollDynamicInfoUpdated.AddListener(UpdateScrollDescription);
    }

    private void OnDisable()
    {
        ActiveScroll.OnScrollChanged.RemoveListener(ShowScroll);
        ActiveScroll.OnFactAssignmentUpdated.RemoveListener(OnFactAssignmentUpdated);
        ActiveScroll.OnScrollDynamicInfoUpdated.RemoveListener(UpdateScrollDescription);
    }

    private void OnDestroy()
    {
        _Instance = null;
    }

    /// <summary>
    /// Update the scroll details screen to display the currently active scroll
    /// </summary>
    /// <param name="activeScroll"></param>
    private void ShowScroll(ActiveScroll activeScroll)
    {
        Transform originalScroll = gameObject.transform.GetChild(1);
        Transform originalScrollView = originalScroll.GetChild(1);
        Transform originalViewport = originalScrollView.GetChild(0);

        //Clear all current ScrollFacts
        originalViewport.GetChild(0).gameObject.DestroyAllChildren();
        Scroll scroll = activeScroll.Scroll;

        originalScroll.GetChild(0).GetComponent<TextMeshProUGUI>().text = scroll.description;

        //ParameterDisplays.ForEach(gameObj => Destroy(gameObj));
        ParameterDisplays = new();
        // generate parameter display slots
        foreach (var slot in activeScroll.Assignments)
        {
            GameObject originalObj =
                Instantiate(parameterDisplayPrefab, originalViewport.GetChild(0));

            RenderedScrollFact originalRSF =
                originalObj.GetComponentInChildren<RenderedScrollFact>();

            ParameterDisplays.Add(originalRSF);

            originalRSF.Populate(scroll, slot.Key);
            originalRSF.transform.parent.gameObject.SetActive(slot.Value.IsVisible);
        }
    }

    private void UpdateScrollDescription(Scroll rendered)
    {
        if (ActiveScroll.Instance.DynamicScrollDescriptionsActive)
        { // Update scroll-description
            Transform scroll = gameObject.transform.GetChild(1).transform;
            scroll.GetChild(0).GetComponent<TextMeshProUGUI>().text = rendered.description;
        }
    }

    private void OnFactAssignmentUpdated(string slotUri, ActiveScroll.SlotAssignment slotAssignment)
    {
        RenderedScrollFact changed = ParameterDisplays.Find(RSF => RSF.ScrollFactURI == slotUri);

        if (changed != null)
        {
            changed.Fact = slotAssignment.fact;
        }
    }
}