using Godot;

public partial class StateBasedActions : Node
{

        [Export] public DialogueBoxSide dialogue_box;
        [Export] public Godot.Collections.Array<string> budgets;

        public override void _Ready()
        {
                GameManager.Instance.AllActionsApplied += _Upkeep;
                //GameManager.Instance.EndStep += _EndStep;
                GameManager.Instance.Reset();
        }


        public override void _ExitTree()
        {
                GameManager.Instance.AllActionsApplied -= _Upkeep;
                //GameManager.Instance.EndStep -= _EndStep;
        }

        public void _EndStep()
        {
                if (GameManager.Instance.TurnCounter == 1)
                {
                        dialogue_box.invoke("res://resources/dialogues/oni_introduction.tres");
                }
        }

        public void _Upkeep()
        {
                GD.Print("Running SateBasedActions::Upkeep on turn [" + GameManager.Instance.TurnCounter.ToString() + "]");

                //Remove once end step is connected
                if (GameManager.Instance.TurnCounter == 1)
                {
                        dialogue_box.invoke("res://resources/dialogues/oni_introduction.tres");
                }

                if (GameManager.Instance.GetFlag("unlocked_ErrorAccumulationRepair") && !GameManager.Instance.GetFlag("used_thrown_exception"))
                        GameManager.Instance.NewModifyRes("thrown_exceptions", 1);
                if (GameManager.Instance.GetFlag("used_thrown_exception"))
                        GameManager.Instance.SetFlag("used_thrown_exception", false);
                if (GameManager.Instance.GetFlag("active_Generator"))
                        GameManager.Instance.ModifyRes("Engine Fuel", -1);

                RefillBudgets();
        }

        private void RefillBudgets()
        {
                foreach (string id in budgets)
                {
                        GameManager.Instance.NewSetRes(id, GameManager.Instance.NewGetResMax(id));
                }
        }
}
