using System; using System.Diagnostics; using DynamicData; using Intromat.PersistentModel; using Intromat.PersistentModel.Nodes; using Intromat.Views; using NodeNetwork.Toolkit.Group.AddEndpointDropPanel; using NodeNetwork.ViewModels; using ReactiveUI; using Splat; namespace Intromat.ViewModels.Nodes { public class GroupNodeViewModel : CodeGenNodeViewModel { private CodeNodeGroupIOBinding? _ioBinding; static GroupNodeViewModel() { Locator.CurrentMutable.Register(() => new GroupNodeView(), typeof(IViewFor)); } public GroupNodeViewModel() : base(NodeType.Group) { } public NetworkViewModel? Subnet { get; set; } public AddEndpointDropPanelViewModel? AddEndpointDropPanelVM { get; private set; } public CodeNodeGroupIOBinding? IOBinding { get => _ioBinding; set { if (_ioBinding != null) throw new InvalidOperationException("IOBinding is already set."); _ioBinding = value; AddEndpointDropPanelVM = new AddEndpointDropPanelViewModel { NodeGroupIOBinding = IOBinding }; } } public override NodeModelBase CreateModel() { return new GroupModel(); } public override void SaveModel(NodeModelBase model) { Debug.Assert(Subnet != null, nameof(Subnet) + " != null"); Debug.Assert(IOBinding != null, nameof(IOBinding) + " != null"); base.SaveModel(model); var group = (GroupModel)model; var network = ProjectSerializer.SaveModel((CodeGenNetworkViewModel)Subnet); group.Network = network; foreach (var input in Inputs.Items) { var endpointType = input.GetType().GenericTypeArguments[0]; var list = endpointType.IsGenericType && typeof(IObservableList<>) == endpointType.GetGenericTypeDefinition(); if (list) endpointType = endpointType.GenericTypeArguments[0]; group.Inputs.Add(new GroupEndpointModel { Name = input.Name, EndpointType = endpointType.ToString(), SortIndex = input.SortIndex, PortType = ((CodeGenPortViewModel)input.Port).PortType, List = list }); } foreach (var output in Outputs.Items) { var endpointType = output.GetType().GenericTypeArguments[0]; var list = endpointType.IsGenericType && typeof(IObservableList<>) == endpointType.GetGenericTypeDefinition(); if (list) endpointType = endpointType.GenericTypeArguments[0]; group.Outputs.Add(new GroupEndpointModel { Name = output.Name, EndpointType = endpointType.ToString(), SortIndex = output.SortIndex, PortType = ((CodeGenPortViewModel)output.Port).PortType, List = list }); } } } }