using FeMM.Common.Helpers;
using FeMM.Common.Models;
using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Parameters;
using System;
using System.Collections.Generic;
using System.Linq;

namespace FeMM.Grasshopper.Components.ElementProperties
{
    public class LinkPropertyComponent : GH_Component
    {
        public LinkPropertyComponent()
          : base("CSI Link Property", "LP", "Link property - Available only for Sap2000/Etabs", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_PROPERTIES)
        {
        }

        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddTextParameter("Name", "Name", "Name", GH_ParamAccess.item);
            pManager.AddIntegerParameter("Link type", "LT", "Link type", GH_ParamAccess.item);
            foreach (LinkPropertyModel.LinkTypes v in Enum.GetValues(typeof(LinkPropertyModel.LinkTypes)))
                ((Param_Integer)pManager[pManager.ParamCount - 1]).AddNamedValue(v.GetDescription(), (int)v);
            pManager.AddNumberParameter("Mass", "M", "Mass [M]", GH_ParamAccess.item, 0);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddNumberParameter("Weight", "W", "Weight [F]", GH_ParamAccess.item, 0);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddNumberParameter("Rotational inertia 1", "R1", "Rotational inertia 1 [ML^2]", GH_ParamAccess.item, 0);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddNumberParameter("Rotational inertia 2", "R2", "Rotational inertia 2 [ML^2]", GH_ParamAccess.item, 0);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddNumberParameter("Rotational inertia 3", "R3", "Rotational inertia 3 [ML^2]", GH_ParamAccess.item, 0);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddNumberParameter("Length", "Length", "The link property is defined for this length in a line (frame) spring. [L]", GH_ParamAccess.item, 0);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddNumberParameter("Area", "Area", "The link property is defined for this area in an area spring. [L2]", GH_ParamAccess.item, 0);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddNumberParameter("Distance end 2 U2", "DJ2", "The distance from the J-End of the link to the U2 shear spring [L]", GH_ParamAccess.item, 0);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddNumberParameter("Distance end 2 U1", "DJ1", "The distance from the J-End of the link to the U1 shear spring [L]", GH_ParamAccess.item, 0);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddGenericParameter("Link direction properties", "LDP", "Link direction properties", GH_ParamAccess.list);
        }

        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Link Property", "LP", "The link property", GH_ParamAccess.item);
        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            string name = string.Empty;

            int linkType = 0;
            double mass = 0;
            double weight = 0;
            double r1 = 0;
            double r2 = 0;
            double r3 = 0;
            double lenght = 0;
            double area = 0;
            double dj2u2 = 0;
            double dj2u1 = 0;

            var linkProperties = new List<GH_LinkDirectionProperty>();

            if (!DA.GetData(0, ref name))
                return;
            if (!DA.GetData(1, ref linkType))
                return;
            if (!Enum.IsDefined(typeof(LinkPropertyModel.LinkTypes), linkType))
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid link type");
            if (!DA.GetData(2, ref mass))
                return;
            if (!DA.GetData(3, ref weight))
                return;
            if (!DA.GetData(4, ref r1))
                return;
            if (!DA.GetData(5, ref r2))
                return;
            if (!DA.GetData(6, ref r3))
                return;
            if (!DA.GetData(7, ref lenght))
                return;
            if (!DA.GetData(8, ref area))
                return;
            if (!DA.GetData(9, ref dj2u2))
                return;
            if (!DA.GetData(10, ref dj2u1))
                return;
            DA.GetDataList(11, linkProperties);

            if (linkProperties.Count == 0 || linkProperties.Where(i => i != null).Count() == 0)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Link direction property can't be empty or null");
                return;
            }

            if (linkProperties.Where(j => j != null).Select(i => i.Value.Direction).GroupBy(x => x).Any(g => g.Count() > 1))
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "More than one direction property for the same Degree of freedom");
                return;
            }

            var linkProperty = new LinkPropertyModel()
            {
                Name = name,
                LinkType = (LinkPropertyModel.LinkTypes)linkType,
                Mass = mass,
                Weight = weight,
                R1 = r1,
                R2 = r2,
                R3 = r3,
                Lenght = lenght,
                Area = area,
                Dj2u2 = dj2u2,
                Dj2u1 = dj2u1,
                LinkDirectionProperties = [.. linkProperties.Where(j => j != null).Select(i => i.Value)],
            };

            var dataType = new GH_LinkProperty(linkProperty);
            DA.SetData(0, dataType);
        }

        public override GH_Exposure Exposure => GH_Exposure.secondary;

        protected override System.Drawing.Bitmap Icon => Properties.Resources.SupportSapLinkMultiLinearPlasticIcon;

        public override Guid ComponentGuid => new("454e763b-bad7-4e10-9cd1-5adc748c54cd");
    }
}
303 files24 directories