using FeMM.Common.Models;
using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
using System;
using System.Collections.Generic;
using System.Runtime.Versioning;

namespace FeMM.Grasshopper.Components.Attributes
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class NodeRestraintsComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the NodeRestraintsComponent class.
        /// </summary>
        public NodeRestraintsComponent()
          : base("Node Restraint", "NR", "Define a node restraints \n false = not restrained; true = restrained", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_ATTRIBUTES)
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddGenericParameter("Node", "N", "The node where define the restraits", GH_ParamAccess.item);
            int i = 0;
            i = pManager.AddGenericParameter("Transational", "T", "The transational restraints.\n false = not restrained; true = restrained", GH_ParamAccess.list);
            pManager[i].Optional = true;
            i = pManager.AddGenericParameter("Rotation", "R", "The rotation restraints.\n false = not restrained; true = restrained", GH_ParamAccess.list);
            pManager[i].Optional = true;
            i = pManager.AddGenericParameter("Freedom Cases", "FC", "The freedom cases", GH_ParamAccess.list);
            pManager[i].Optional = true;
            i = pManager.AddGenericParameter("Coordinate System", "CS", "The restraint coordiante system", GH_ParamAccess.item);
            pManager[i].Optional = true;
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Node", "N", "The modified node", GH_ParamAccess.item);
        }

        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            GH_Node node = null;
            var trans = new List<IGH_Goo>();
            var rot = new List<IGH_Goo>();
            var freedomCases = new List<GH_FreedomCase>();
            GH_FunctionDefinition coordinateSystemModel = null;

            if (!DA.GetData("Node", ref node))
                return;
            DA.GetDataList(1, trans);
            DA.GetDataList(2, rot);
            DA.GetDataList(3, freedomCases);
            DA.GetData(4, ref coordinateSystemModel);

            var newNode = new GH_Node(node);

            var attr = new NodeRestraintAttributeModel.AttributeData();
            for (int i = 0; i < trans.Count && i < 3; i++)
            {
                if (trans[i].CastTo(out bool b))
                {
                    if (i == 0)
                        attr.Tx = b;
                    else if (i == 1)
                        attr.Ty = b;
                    else if (i == 2)
                        attr.Tz = b;
                    continue;
                }
                if (trans[i].CastTo(out double n))
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Defines the displacement using Displacement load");
                    DA.AbortComponentSolution();
                    return;
                }
                if (trans[i].CastTo(out string s))
                {
                    if (bool.TryParse(s, out b))
                    {
                        if (i == 0)
                            attr.Tx = b;
                        else if (i == 1)
                            attr.Ty = b;
                        else if (i == 2)
                            attr.Tz = b;
                    }
                    else if (double.TryParse(s, out n))
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Defines the displacement using Displacement load");
                        DA.AbortComponentSolution();
                        return;
                    }
                    continue;
                }
            }

            for (int i = 0; i < rot.Count && i < 3; i++)
            {
                if (rot[i].CastTo(out bool b))
                {
                    if (i == 0)
                        attr.Rx = b;
                    else if (i == 1)
                        attr.Ry = b;
                    else if (i == 2)
                        attr.Rz = b;
                }
                if (rot[i].CastTo(out double n))
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Defines the displacement using Displacement load");
                    DA.AbortComponentSolution();
                    return;
                }
                if (rot[i].CastTo(out string s))
                {
                    if (bool.TryParse(s, out b))
                    {
                        if (i == 0)
                            attr.Rx = b;
                        else if (i == 1)
                            attr.Ry = b;
                        else if (i == 2)
                            attr.Rz = b;
                    }
                    else if (double.TryParse(s, out n))
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Defines the displacement using Displacement load");
                        DA.AbortComponentSolution();
                        return;
                    }
                }
            }

            var nodeAttr = new NodeRestraintAttributeModel(attr);
            if (freedomCases.Count > 0)
            {
                for (int i = 0; i < freedomCases.Count; i++)
                    nodeAttr.Value.FreedomCases.Add(new FreedomCaseModel(freedomCases[i].Value));
            }
            else
                nodeAttr.Value.FreedomCases.Add(FreedomCaseModel.UnsetFreedomCase);

            if (coordinateSystemModel != null)
            {
                if(coordinateSystemModel.Value is CoordinateSystemModel cs)
                    nodeAttr.Value.CoordinateSystem = cs;
            }
            else
                nodeAttr.Value.CoordinateSystem = CoordinateSystemModel.Global;

            newNode.Value.Attributes.Add(nodeAttr);

            DA.SetData("Node", newNode);
        }

        /// <summary>
        /// Provides an Icon for the component.
        /// </summary>
        protected override System.Drawing.Bitmap Icon => Properties.Resources.NodeRestraintsIcon;

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("79186ea8-b0db-4a86-8f95-2414b580759b");
    }
}
303 files24 directories