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

namespace FeMM.Grasshopper.Components
{
    public class MassSourceComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the SuperElementComponent class.
        /// </summary>
        public MassSourceComponent()
          : base("Mass Source", "MS", "Defines the default mass source in SAP", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_DEFINITIONS)
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddTextParameter("Name", "N", "The name of the Modal Eigen", GH_ParamAccess.item);
            pManager.AddBooleanParameter("Mass From Elements", "ME", "If this item is True then element self mass is included in the mass", GH_ParamAccess.item, true);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddBooleanParameter("Mass From Masses", "MM", "If this item is True then assigned masses are included in the mass", GH_ParamAccess.item, true);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddBooleanParameter("Mass From Loads", "ML", "If this item is True then specified load patterns are included in the mass", GH_ParamAccess.item, true);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddGenericParameter("Load Patter Name", "LP", "This is an array of load pattern names specified for the mass source. This item is only applicable when the " +
                "Mass From Loads item is True", GH_ParamAccess.list);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddNumberParameter("Load Pattern Multipliers", "LPM", "This is an array of load pattern multipliers specified for the mass source", GH_ParamAccess.list, new double[] { });
            pManager[pManager.ParamCount - 1].Optional = true;
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Mass Source", "MS", "The Mass Source", 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)
        {
            string name = string.Empty;
            bool massFromElements = true;
            bool massFromMasses = true;
            bool massFromLoads = true;
            var loadNames = new List<GH_Case>();
            var multipliers = new List<double>();

            if (!DA.GetData(0, ref name))
                return;
            DA.GetData(1, ref massFromElements);
            DA.GetData(2, ref massFromMasses);
            DA.GetData(3, ref massFromLoads);
            DA.GetDataList(4, loadNames);
            DA.GetDataList(5, multipliers);

            if (loadNames.Count != multipliers.Count)
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Load case names and Multipliers must have the same length");

            var functionData = new MassSourceModel()
            {
                Name = name,
                LoadPatternNames = [.. loadNames.Select(i => i.Value.Name)],
                LoadPatternMultipliers = [.. multipliers],
                MassFromElements = massFromElements,
                MassFromMasses = massFromMasses,
                MassFromLoads = massFromLoads,
            };

            var massSourceModel = new MassSourceModel(functionData);
            var gH_Function = new GH_FunctionDefinition(massSourceModel);

            DA.SetData(0, gH_Function);
        }

        public override GH_Exposure Exposure => GH_Exposure.tertiary;

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("975ea556-5030-46ca-9672-3c0d2b6090eb");
    }
}
303 files24 directories