public async Task <bool> RemoveNode(LiteGraph.Node node)
        {
            return(await Task.Run(() =>
            {
                if (engine == null)
                {
                    return false;
                }

                Nodes.Node oldNode = engine.GetNode(node.id);
                if (oldNode == null)
                {
                    engine.LogEngineError($"Can`t remove node [{node.id}]. Does not exist.");
                    return false;
                }

                if (oldNode.GetNodeOptions().ProtectedAccess)
                {
                    if (!User.HasClaim(x => x.Type == UserClaims.EditorProtectedAccess))
                    {
                        engine.LogEngineError(
                            $"Can`t remove node [{oldNode.Category}/{oldNode.Type}]. No permissions for protected access.");
                        return false;
                    }
                }

                engine.RemoveNode(oldNode);
                return true;
            }));
        }
        public async Task <bool> AddNode(LiteGraph.Node node)
        {
            return(await Task.Run(() =>
            {
                if (engine == null)
                {
                    return false;
                }

                string type = node.properties["ObjectType"];
                string assemblyName = node.properties["Assembly"];

                Nodes.Node newNode = CreateNode(type, assemblyName);

                if (newNode == null)
                {
                    engine.LogEngineError($"Can`t create node [{node.properties["ObjectType"]}]. Type does not exist.");
                    return false;
                }

                if (newNode.GetNodeOptions().ProtectedAccess)
                {
                    if (!User.HasClaim(x => x.Type == UserClaims.EditorProtectedAccess))
                    {
                        engine.LogEngineError(
                            $"Can`t create node [{node.properties["ObjectType"]}]. No permissions for protected access.");
                        return false;
                    }
                }

                newNode.Position = new Position {
                    X = node.pos[0], Y = node.pos[1]
                };
                if (node.size.Length == 2)
                {
                    newNode.Size = new Size {
                        Width = node.size[0], Height = node.size[1]
                    }
                }
                ;
                //newNode.Id = node.id;
                newNode.PanelId = node.panel_id ?? MAIN_PANEL_ID;

                engine.AddNode(newNode);

                return true;
            }));
        }
        public async Task <bool> UpdateNode(LiteGraph.Node node)
        {
            return(await Task.Run(() =>
            {
                if (engine == null)
                {
                    return false;
                }

                Nodes.Node oldNode = engine.GetNode(node.id);
                if (oldNode == null)
                {
                    engine.LogEngineError($"Can`t update node [{node.id}]. Does not exist.");
                    return false;
                }

                if (oldNode.Position == null || oldNode.Size == null ||
                    oldNode.Position.X != node.pos[0] || oldNode.Position.Y != node.pos[1] ||
                    oldNode.Size.Width != node.size[0] || oldNode.Size.Height != node.size[1])
                {
                    if (node.pos != null)
                    {
                        oldNode.Position = new Position {
                            X = node.pos[0], Y = node.pos[1]
                        }
                    }
                    ;

                    if (node.size != null)
                    {
                        oldNode.Size = new Size {
                            Width = node.size[0], Height = node.size[1]
                        }
                    }
                    ;

                    engine.UpdateNodeInEditor(oldNode);
                    engine.UpdateNodeInDb(oldNode);
                }

                return true;
            }));
        }
        public LiteGraph.Node ConvertNodeToLiteGraphNode(Nodes.Node node)
        {
            LiteGraph.Node litegraphNode = new LiteGraph.Node
            {
                title    = node.Type,
                type     = node.Category + "/" + node.Type,
                id       = node.Id,
                panel_id = node.PanelId
            };


            litegraphNode.properties["ObjectType"] = node.GetType().ToString();

            if (node.Position != null)
            {
                litegraphNode.pos = new[] { node.Position.X, node.Position.Y }
            }
            ;

            if (node.Size != null)
            {
                litegraphNode.size = new[] { node.Size.Width, node.Size.Height }
            }
            ;

            litegraphNode.inputs  = new List <LiteGraph.Input>();
            litegraphNode.outputs = new List <LiteGraph.Output>();


            if (node.Inputs != null)
            {
                List <Nodes.Input> orderedInputs = node.Inputs.OrderBy(x => x.SlotIndex).ToList();
                foreach (var input in orderedInputs)
                {
                    litegraphNode.inputs.Add(new LiteGraph.Input
                    {
                        name       = input.Name,
                        type       = (int)input.Type,
                        link       = engine.GetLinkForInput(input)?.Id,
                        isOptional = input.IsOptional
                    });
                }
            }

            if (node.Outputs != null)
            {
                List <Nodes.Output> orderedOutputs = node.Outputs.OrderBy(x => x.SlotIndex).ToList();
                foreach (var output in orderedOutputs)
                {
                    List <Nodes.Link> links = engine.GetLinksForOutput(output);
                    if (links != null)
                    {
                        string[] linksIds = new string[links.Count];
                        for (int i = 0; i < links.Count; i++)
                        {
                            linksIds[i] = links[i].Id;
                        }
                        litegraphNode.outputs.Add(new LiteGraph.Output
                        {
                            name  = output.Name,
                            type  = (int)output.Type,
                            links = linksIds
                        });
                    }
                    else
                    {
                        litegraphNode.outputs.Add(new LiteGraph.Output
                        {
                            name = output.Name,
                            type = (int)output.Type
                        });
                    }
                }
            }

            if (node.Settings != null && node.Settings.Count > 0)
            {
                litegraphNode.properties["Settings"] = JsonConvert.SerializeObject(node.Settings);
            }


            if (node.Settings.ContainsKey("Name") &&
                !string.IsNullOrEmpty(node.Settings["Name"].Value))
            {
                litegraphNode.title += " [" + node.Settings["Name"].Value + "]";
            }

            if (node is PanelNode)
            {
                litegraphNode.title = node.Settings["Name"].Value;
            }



            return(litegraphNode);
        }
        public LiteGraph.Node ConvertNodeToLiteGraphNode(Nodes.Node node)
        {
            LiteGraph.Node litegraphNode = new LiteGraph.Node
            {
                title = node.Type,
                type = node.Category + "/" + node.Type,
                id = node.Id,
                panel_id = node.PanelId
            };


            litegraphNode.properties["ObjectType"] = node.GetType().ToString();

            if (node.Position != null)
                litegraphNode.pos = new[] { node.Position.X, node.Position.Y };

            if (node.Size != null)
                litegraphNode.size = new[] { node.Size.Width, node.Size.Height };

            litegraphNode.inputs = new List<LiteGraph.Input>();
            litegraphNode.outputs = new List<LiteGraph.Output>();


            if (node.Inputs != null)
            {
                List<Nodes.Input> orderedInputs = node.Inputs.OrderBy(x => x.SlotIndex).ToList();
                foreach (var input in orderedInputs)
                {
                    litegraphNode.inputs.Add(new LiteGraph.Input
                    {
                        name = input.Name,
                        type = (int)input.Type,
                        link = engine.GetLinkForInput(input)?.Id,
                        isOptional = input.IsOptional
                    });
                }
            }

            if (node.Outputs != null)
            {
                List<Nodes.Output> orderedOutputs = node.Outputs.OrderBy(x => x.SlotIndex).ToList();
                foreach (var output in orderedOutputs)
                {
                    List<Nodes.Link> links = engine.GetLinksForOutput(output);
                    if (links != null)
                    {
                        string[] linksIds = new string[links.Count];
                        for (int i = 0; i < links.Count; i++)
                        {
                            linksIds[i] = links[i].Id;
                        }
                        litegraphNode.outputs.Add(new LiteGraph.Output
                        {
                            name = output.Name,
                            type = (int)output.Type,
                            links = linksIds
                        });
                    }
                    else
                    {
                        litegraphNode.outputs.Add(new LiteGraph.Output
                        {
                            name = output.Name,
                            type = (int)output.Type
                        });
                    }
                }
            }

            if (node.Settings != null && node.Settings.Count > 0)
                litegraphNode.properties["Settings"] = JsonConvert.SerializeObject(node.Settings);


            if (node.Settings.ContainsKey("Name")
                && !string.IsNullOrEmpty(node.Settings["Name"].Value))
                litegraphNode.title += " [" + node.Settings["Name"].Value + "]";

            if (node is PanelNode)
            {
                litegraphNode.title = node.Settings["Name"].Value;
            }



            return litegraphNode;
        }