using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XNode;
using RPGCreationKit.BehaviourTree;
using RPGCreationKit.BehaviourTree.Data;
using UnityEditor;

namespace RPGCreationKit.BehaviourTree
{
    [CreateNodeMenu("RPGCK_BehaviourTree/Graph/Set BTVariable", order = 1)]
    [System.Serializable]
    public class SetGraphVariableNode : BTNode
    {
        public BTVariable btVariable;
        public BTParameter instantValue;

        // Use this for initialization
        protected override void Init()
        {
            base.Init();
        }

        // Return the correct value of an output port when requested
        public override object GetValue(NodePort port)
        {
            return null; // Replace this
        }

        public override NodeState Execute()
        {
            if (m_NodeState == NodeState.Success || m_NodeState == NodeState.Failure)
                if (hasEvaluated == true)
                    return m_NodeState;

            if (!STARTED)
                OnStart();

            m_NodeState = NodeState.Running;


            switch (instantValue.parameterType)
            {
                case BTParameterType.BOOL:
                    btVariable.SetValue(instantValue.boolValue);
                    break;

                case BTParameterType.INT:
                    btVariable.SetValue(instantValue.intValue);
                    break;

                case BTParameterType.FLOAT:
                    btVariable.SetValue(instantValue.intValue);
                    break;
            }


            m_NodeState = NodeState.Success;
            hasEvaluated = true;
            return m_NodeState;
        }

        public override void ReEvaluate()
        {
            if (m_NodeState != NodeState.Running)
            {
                base.ReEvaluate();
                OnStart();
            }
        }


        public override void OnRemoveConnection(NodePort port)
        {
            base.OnRemoveConnection(port);
            indexInSequence = -1;
        }

        public override void OnStart()
        {
            btVariable = BTReference.SolveReference(this.graph as RPGCK_BT, btVariable.name);
            STARTED = true;
        }
    }
}