using Godot;
using Godot.Collections;
using System;

public partial class VisibilityControll : Node
{
        private InteractableObject parent;
        private int step = 0;
        [Export] public Array<string> show_flags;
        [Export] public Array<string> hide_flags;

        public override void _Ready()
        {
                parent = GetParent<InteractableObject>();
                //This needs to be connected to "SceneSetup" instead
                GameManager.Instance.AllActionsApplied += _ProcessVisibility;
                //_ProcessVisibility();
        }

        public override void _ExitTree()
        {
                GameManager.Instance.AllActionsApplied -= _ProcessVisibility;
        }

        public void _ProcessVisibility()
        {
                if (step == 0)
                {
                        bool show = true;

                        if (show_flags != null)
                        {
                                foreach (string flag in show_flags)
                                {
                                        show = show && GameManager.Instance.GetFlag(flag);
                                }
                        }

                        if (show)
                        {
                                GD.Print("Node [" + parent.Data.Name + "] goes from Start-Hidden to Shown");
                                parent.ShowUI();
                                step = 1;
                        }
                }
                else if (step == 1)
                {
                        bool hide = true;
                        if (hide_flags != null)
                        {
                                if (hide_flags.Count == 0)
                                {
                                        hide = false;
                                }
                                foreach (string flag in hide_flags)
                                {
                                        hide = hide && GameManager.Instance.GetFlag(flag);
                                }
                        }
                        else
                        {
                                hide = false;
                        }

                        if (hide)
                        {
                                GD.Print("Node [" + parent.Data.Name + "] goes from Shown to Final-Hidden");
                                step = 2;
                                parent.HideUI();
                                GameManager.Instance.AllActionsApplied -= _ProcessVisibility;
                        }
                }
        }

}
