using Serein.Library.Api;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Serein.NodeFlow.Model.Operation
{

    /// <summary>
    /// 取出节点操作
    /// </summary>
    internal class ContainerTakeOutNodeOperation : OperationBase
    {
        public override string Theme => nameof(ContainerTakeOutNodeOperation);

        /// <summary>
        /// 所在画布
        /// </summary>
        public string CanvasGuid { get; set; }

        /// <summary>
        /// 子节点，该数据为此次事件的主节点
        /// </summary>
        public string NodeGuid { get; set; }


        /// <summary>
        /// 父节点
        /// </summary>
        private INodeContainer ContainerNode;

        /// <summary>
        /// 子节点，该数据为此次事件的主节点
        /// </summary>
        private IFlowNode Node;



        public override bool ValidationParameter()
        {
            if (!flowModelService.ContainsCanvasModel(CanvasGuid))
            {
                flowEnvironment.WriteLine(Library.InfoType.INFO, $"节点取出失败，目标画布不存在[{NodeGuid}]");
                return false;
            }
            // 获取目标节点与容器节点
            if (!flowModelService.TryGetNodeModel(NodeGuid, out var nodeModel))
            {
                flowEnvironment.WriteLine(Library.InfoType.INFO, $"节点取出失败，目标节点不存在[{NodeGuid}]");
                return false;
            }
            if (nodeModel.ContainerNode is not INodeContainer containerNode)
            {
                flowEnvironment.WriteLine(Library.InfoType.INFO, $"节点取出失败，节点并非容器节点[{nodeModel.Guid}]");
                return false;
            }
            Node = nodeModel;
            ContainerNode = containerNode;
            return true;
        }

        public override async Task<bool> ExecuteAsync()
        {
            if (!ValidationParameter()) return false;

            ContainerNode.TakeOutNode(Node);
            await TriggerEvent(() =>
            {
                flowEnvironmentEvent.OnNodeTakeOut(new NodeTakeOutEventArgs(CanvasGuid, NodeGuid)); // 重新放置在画布上
            });
           return true;
        }

        public override bool Undo()
        {
            ContainerNode.PlaceNode(Node);
            if (ContainerNode is IFlowNode containerFlowNode)
            {
                flowEnvironmentEvent.OnNodePlace(new NodePlaceEventArgs(CanvasGuid, NodeGuid, containerFlowNode.Guid)); // 通知UI更改节点放置位置

            }
            return true;
        }


        public override void ToInfo()
        {
        }
    }
}
