using Serein.Library.Api;
using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace Serein.Library
{
    /// <summary>
    /// 节点调试设置，用于中断节点的运行
    /// </summary>
    [FlowDataProperty(ValuePath = NodeValuePath.DebugSetting)]
    public partial class NodeDebugSetting
    {
        /// <summary>
        /// 创建属于某个节点的调试设置
        /// </summary>
        /// <param name="nodeModel"></param>
        public NodeDebugSetting(IFlowNode nodeModel)
        {
            NodeModel = nodeModel;
        }


        /// <summary>
        /// 是否保护参数
        /// </summary>
        [DataInfo(IsNotification = true)]
        private bool _isProtectionParameter = false;

        /// <summary>
        /// 对应的节点
        /// </summary>
        [DataInfo(IsProtection = true)]
        private IFlowNode _nodeModel;

        /// <summary>
        /// 是否使能
        /// </summary>
        [DataInfo(IsNotification = true)]
        private bool _isEnable = true;

        /// <summary>
        ///  是否中断节点。
        /// </summary>
        [DataInfo(IsNotification = true)]
        private bool _isInterrupt = false;

    }

    /// <summary>
    /// 节点中断
    /// </summary>
    public partial class NodeDebugSetting
    {
        /// <summary>
        /// 取消中断的回调函数
        /// </summary>
        private Action _cancelInterrupt { get; set; }
        /// <summary>
        /// 取消中断
        /// </summary>
        public Action CancelInterrupt => _cancelInterrupt;
        /// <summary>
        /// 中断节点
        /// </summary>
        public Func<Task> _getInterruptTask;

        /// <summary>
        /// 获取中断的Task
        /// </summary>
        public Func<Task> GetInterruptTask => _getInterruptTask;



        partial void OnIsInterruptChanged(bool oldValue, bool newValue)
        {
            Debug.WriteLine($" {nameof(NodeDebugSetting)}.{nameof(NodeDebugSetting.OnIsInterruptChanged)} 暂未实现，需要重新设计中断逻辑");
            /*if (newValue && _getInterruptTask is null)
            {
                // 设置获取中断的委托
                _getInterruptTask = () => NodeModel.Env.IOC.Get<FlowInterruptTool>().WaitTriggerAsync(NodeModel.Guid);

            }
            else if (!newValue)
            {
                if (_getInterruptTask is null)
                {

                }
                else
                {
                    // 设置解除中断的委托
                    _cancelInterrupt = () => NodeModel.Env.IOC.Get<FlowInterruptTool>().InvokeTrigger(NodeModel.Guid);
                    _cancelInterrupt.Invoke();
                    _getInterruptTask = null;
                }

            }*/
        }




    }
}



