using FeMM.Common.Models;
using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using FeMM.Rhino.Elements;
using FeMM.Rhino.Models;
using Grasshopper.Kernel;
using Rhino;
using Rhino.Commands;
using Rhino.DocObjects;
using Rhino.Input;
using Rhino.Input.Custom;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.Versioning;

namespace FeMM.Grasshopper.Components.Parameters
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class NodeParam : GH_PersistentParam<GH_Node>
    {
        public NodeParam()
            : base("Node", "N", "Contains a collection of nodess", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_PARAMETERS)
        {
        }

        protected override GH_Node InstantiateT()
        {
            return new GH_Node();
        }

        protected override GH_GetterResult Prompt_Singular(ref GH_Node value)
        {
            RhinoDoc doc = RhinoDoc.ActiveDoc;
            if (doc == null)
                return GH_GetterResult.cancel;

            var go = new GetObject();
            go.SetCommandPrompt("Select a node");
            GetResult res = go.Get();
            if (res == GetResult.Object)
            {
                RhinoObject rhinoObject = go.Object(0).Object();
                if (rhinoObject is NodeObject nodeObject)
                {
                    NodeModel node = ((NodeUserData)nodeObject.Model).Data;
                    value = new GH_Node(node);
                    return GH_GetterResult.success;
                }
            }

            return GH_GetterResult.cancel;
        }

        protected override GH_GetterResult Prompt_Plural(ref List<GH_Node> values)
        {
            RhinoDoc doc = RhinoDoc.ActiveDoc;
            if (doc == null)
                return GH_GetterResult.cancel;

            values = [];

            var go = new GetObject();
            go.SetCommandPrompt("Select nodes");
            go.GetMultiple(1, 0);
            if (go.CommandResult() == Result.Success)
            {
                for (int i = 0; i < go.ObjectCount; i++)
                {
                    RhinoObject rhinoObject = go.Object(i).Object();
                    if (rhinoObject is NodeObject nodeObject)
                    {
                        NodeModel node = ((NodeUserData)nodeObject.Model).Data;
                        values.Add(new GH_Node(node));
                    }
                }
                doc.Views.Redraw();
                return GH_GetterResult.success;
            }

            return GH_GetterResult.cancel;
        }

        protected override Bitmap Icon => Properties.Resources.NodeParamIcon;

        public override Guid ComponentGuid => new("85194998-F6F8-430B-A5BF-4E92A12CB4D4");
    }
}
303 files24 directories