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

namespace RPGCreationKit.BehaviourTree
{
    [CreateNodeMenu("RPGCK_BehaviourTree/Math/Random Int", order = 1)]
    [System.Serializable]
    public class ARandomIntNode : BTNode
    {
        public int min;
        public int max;

        public bool inclusive;

        public bool useVariable = true;
        public BT_Int storedValue;

        public int instantValue;


        public override void OnStart()
        {
            if (useVariable)
                // Solve references
                storedValue = (BT_Int)BTReference.SolveReference(this.graph as RPGCK_BT, storedValue.name);
            else
            {
                // Create a storedValue from instantreference
                BT_Int newbtInt = CreateInstance<BT_Int>();
                newbtInt.Name = "TEMP_BT_INT";
                newbtInt.value = instantValue;

                storedValue = newbtInt;
            }

            STARTED = true;
        }

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

            if (!STARTED)
                OnStart();

            if (inclusive)
                storedValue.value = Random.Range(min, max + 1);
            else
                storedValue.value = Random.Range(min, max);

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

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