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

namespace FeMM.Grasshopper.Components.Functions
{
    public class ResponseSpectrumComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the SuperElementComponent class.
        /// </summary>
        public ResponseSpectrumComponent()
          : base("Response Spectrum", "RS", "Defines the Response Spectrum 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 Response Spectrum", GH_ParamAccess.item);
            pManager.AddNumberParameter("Function Period", "P", "This is an array that includes the period for each data point", GH_ParamAccess.list);
            pManager.AddNumberParameter("Function Value", "V", "This is an array that includes the function value for each data point", GH_ParamAccess.list);
            pManager.AddNumberParameter("Damp Function", "D", "The damping ratio for the function, 0 <= DampRatio < 1", GH_ParamAccess.item);
            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("Response Spectrum", "RS", "Response Spectrum", 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;
            var value = new List<double>();
            var period = new List<double>();
            double damp = 0;

            if (!DA.GetData(0, ref name))
                return;
            if (!DA.GetDataList(1, period))
                return;
            if (!DA.GetDataList(2, value))
                return;
            DA.GetData<double>(3, ref damp);


            if (value.Count != period.Count)
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Value  and perios must have the same length");
            if (damp < 0 || damp > 1)
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"DampRatio must be major than 0 and minor than 1");

            var functionModel = new ResponseSpectrumFunctionModel()
            {
                Name = name,
                Value = [.. value],
                Period = [.. period],
                DampingRatio = damp,
            };

            var model = new ResponseSpectrumFunctionModel(functionModel);
            var gH_Function = new GH_FunctionDefinition(model);

            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.ResponseSpectrumComponentIcon;

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("a5d9afef-308f-44b6-9652-45405c13efda");
    }
}
303 files24 directories