using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PXP.Crawler.API.Model;
using PXP.Crawler.API.Utils;

namespace PXP.Crawler.API.Plugins.Events
{
     [NodeInfo("Change (Boolean)", typeof(ChangeStringNode), Category = "Events", Description = "Detect boolean changed...",
        DefaultWidth = 70, DefaultHeight = 40, LockHeight = true)]
    public class ChangeBooleanValue : ChangeNodeBase<bool>
    {
        public ChangeBooleanValue(Program program) : base(program)
        {
            currValue = false;
            TrueOnly = true;
        }

         public override void OnInputPinConnected(string name)
         {
             base.OnInputPinConnected(name);
             if (inPropConns.ContainsKey(name))
             {
                 NodeConnectionProperty ncProp = inPropConns[name];
                 currValue = Null.BoolFromDBNull(ncProp.sourcePropInfo.GetValue(ncProp.source));
             }
         }

        public override bool CompareValueChanged(bool val)
        {
            if (TrueOnly)
            {
                if (val)
                {
                    if (!currValue)
                        return true;
                }
                else
                {
                    currValue = false;
                    return false;
                }
            }
            return currValue != val;
        }

         [NodeSerializeProperty]
         public bool TrueOnly
         {
             get; set;
         }
    }
}
