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

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

        [UnityEngine.SerializeField]
        List<double> _value = new();

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

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

        public override bool IsValid => true;

        public override IEnumerable<double> Run(EffectInput blackBoard) {
            var value = _in.Node.Run(blackBoard);
            if (value.SequenceEqual(_value)) {
                UnityEngine.Debug.Log($"<color=green>PASSED: {this} Got [{string.Join(",", value)}]</color>");
            }
            else {
                UnityEngine.Debug.Log($"<color=red>FAILED: {this} Got [{string.Join(",", value)}]</color>");
            }

            return value;
        }

        public override string ToString() {
            return $"{_displayName} Expected [{string.Join(",", _value)}]";
        }

        public override EffectStepNodeData ToData() {
            var effectStepNodeData = base.ToData();
            effectStepNodeData.Const = _value;
            effectStepNodeData.DisplayName = _displayName;
            return effectStepNodeData;
        }

        public override bool FromData(EffectStepNodeData effectStepNodeData, Dictionary<string, EffectStepNode> guidToNode) {
            base.FromData(effectStepNodeData, guidToNode);
            _value = effectStepNodeData.Const ?? new();
            _displayName = effectStepNodeData.DisplayName;

            return true;
        }
    }
}