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

namespace RPGCreationKit.BehaviourTree
{
    [CreateNodeMenu("RPGCK_BehaviourTree/Comparison/Math/Float Comparison", order = 1)]
    [System.Serializable]
    public class CFloatComparisonNode : BTNode
    {
        public bool firstUseVariable;
        public BT_Float firstStoredValue;
        public float firstInstantValue;

        public ComparisionOperators operation;

        public bool secondUseVariable;
        public BT_Float secondStoredValue;
        public float secondInstantValue;


        public override void OnStart()
        {
            if (firstUseVariable)
                // Solve references
                firstStoredValue = (BT_Float)BTReference.SolveReference(this.graph as RPGCK_BT, firstStoredValue.name);
            else
            {
                // Create a storedValue from instantreference
                BT_Float newBtFloat = CreateInstance<BT_Float>();
                newBtFloat.Name = "TEMP_BT_FLOAT_FIRST";
                newBtFloat.value = firstInstantValue;

                firstStoredValue = newBtFloat;
            }

            if (secondUseVariable)
                // Solve references
                secondStoredValue = (BT_Float)BTReference.SolveReference(this.graph as RPGCK_BT, secondStoredValue.name);
            else
            {
                // Create a storedValue from instantreference
                BT_Float newBtFloat = CreateInstance<BT_Float>();
                newBtFloat.Name = "TEMP_BT_FLOAT_SECOND";
                newBtFloat.value = secondInstantValue;

                secondStoredValue = newBtFloat;
            }

            STARTED = true;
        }

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

            if (!STARTED)
                OnStart();

            switch (operation)
            {
                case ComparisionOperators.Equal:
                    m_NodeState = (firstStoredValue.value == secondStoredValue.value) ? m_NodeState = NodeState.Success : NodeState.Failure;
                    break;

                case ComparisionOperators.GreaterOrEqualThan:
                    m_NodeState = (firstStoredValue.value >= secondStoredValue.value) ? m_NodeState = NodeState.Success : NodeState.Failure;
                    break;

                case ComparisionOperators.GreaterThan:
                    m_NodeState = (firstStoredValue.value > secondStoredValue.value) ? m_NodeState = NodeState.Success : NodeState.Failure;
                    break;

                case ComparisionOperators.LessOrEqualThan:
                    m_NodeState = (firstStoredValue.value <= secondStoredValue.value) ? m_NodeState = NodeState.Success : NodeState.Failure;
                    break;

                case ComparisionOperators.LessThan:
                    m_NodeState = (firstStoredValue.value < secondStoredValue.value) ? m_NodeState = NodeState.Success : NodeState.Failure;
                    break;

                case ComparisionOperators.NotEqual:
                    m_NodeState = (firstStoredValue.value != secondStoredValue.value) ? m_NodeState = NodeState.Success : NodeState.Failure;
                    break;

                default:
                    m_NodeState = NodeState.Failure;
                    break;
            }

            hasEvaluated = true;
            return m_NodeState;
        }

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