#nullable enable


namespace TagFighter.Effects.Steps
{
    using System.Collections.Generic;
    using System.Linq;

    [StepType(StepTypeAttribute.TesterStep)]
    public class BoolTester : OutputStep<bool>
    {
        [UnityEngine.SerializeField]
        SinglePort<bool> _in = new();

        [UnityEngine.SerializeField]
        bool _value = true;

        [UnityEngine.SerializeField]
        string _displayName = nameof(BoolTester);

        public override IEnumerable<IPort> Inputs {
            get {
                yield return _in;
            }
        }

        public override bool IsValid => true;

        public override bool Run(EffectInput blackBoard) {
            var value = _in.Node != null && _in.Node.Run(blackBoard);
            if (value == _value) {
                UnityEngine.Debug.Log($"<color=green>PASSED: {this} Got {value}</color>");
            }
            else {
                UnityEngine.Debug.Log($"<color=red>FAILED: {this} Got {value}</color>");
            }

            return value;
        }
        public override string ToString() {
            return $"{_displayName} Expected {_value}";
        }

        public override EffectStepNodeData ToData() {
            var effectStepNodeData = base.ToData();
            effectStepNodeData.Const = new() { _value == false ? 0d : 1d };
            effectStepNodeData.DisplayName = _displayName;
            return effectStepNodeData;
        }

        public override bool FromData(EffectStepNodeData effectStepNodeData, Dictionary<string, EffectStepNode> guidToNode) {
            base.FromData(effectStepNodeData, guidToNode);
            _value = effectStepNodeData.Const != null && effectStepNodeData.Const.FirstOrDefault() != 0;
            _displayName = effectStepNodeData.DisplayName ?? "";

            return true;
        }
    }
}