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.Functions
{
    public class ModalEigenComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the SuperElementComponent class.
        /// </summary>
        public ModalEigenComponent()
          : base("Modal Eigen", "ME", "Defines modal eigen load case 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.AddIntegerParameter("Max number of Modes", "Max", "The maximum number of modes requested", GH_ParamAccess.item, 20);
            pManager[pManager.ParamCount - 1].Optional = true;

            pManager.AddIntegerParameter("Min number of Modes", "Min", "The minimum number of modes requested", GH_ParamAccess.item, 1);
            pManager[pManager.ParamCount - 1].Optional = true;

            pManager.AddGenericParameter("Initial Case", "IC", "This is blank, None, or the name of an existing analysis case. This item specifies " +
                "if the load case starts from zero initial conditions, that is, an unstressed state, or if it starts using the stiffness that occurs" +
                " at the end of a nonlinear static or nonlinear direct integration time history load case", GH_ParamAccess.item);
            pManager[pManager.ParamCount - 1].Optional = true;

            pManager.AddGenericParameter("Modal Ritz Loads", "MRL", "Modal ritz acceleration loads", GH_ParamAccess.list);
            pManager[pManager.ParamCount - 1].Optional = true;

            pManager.AddNumberParameter("Eigen value shift frequency", "ESF", "The eigenvalue shift frequency. [cyc/s]", GH_ParamAccess.item, 0);
            pManager[pManager.ParamCount - 1].Optional = true;

            pManager.AddNumberParameter("Eigen cutoff frequency", "ECO", "The eigencutoff frequency radius. [cyc/s]", GH_ParamAccess.item, 0);
            pManager[pManager.ParamCount - 1].Optional = true;

            pManager.AddNumberParameter("Eigen tolerance", "ET", "The relative convergence tolerance for eigenvalues", GH_ParamAccess.item, 1.000E-09);
            pManager[pManager.ParamCount - 1].Optional = true;

            pManager.AddBooleanParameter("Allow Auto Frequency Shift", "ET", "Indicating if automatic frequency shifting is allowed", GH_ParamAccess.item, true);
            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("Eigen Modal", "EM", "The Eigen Modal", 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;
            int maxModes = 0;
            int minModes = 0;
            GH_Case initialCase = null; 
            var loadType = new List<GH_FunctionDefinition>();

            double eigenShiftFreq = 0;
            double eigenCutOff = 0;
            double eigenTol = 0;
            bool allowAutoFreqShift = true;

            if (!DA.GetData(0, ref name))
                return;
            DA.GetData(1, ref maxModes);
            DA.GetData(2, ref minModes);
            DA.GetData(3, ref initialCase);
            DA.GetDataList(4, loadType);
            DA.GetData(5, ref eigenShiftFreq);
            DA.GetData(6, ref eigenCutOff);
            DA.GetData(7, ref eigenTol);
            DA.GetData(8, ref allowAutoFreqShift);

            LoadCaseModel loadCaseModel = null;
            if (initialCase != null)
            {
                if (initialCase.Value != null && initialCase.Value is LoadCaseModel lccc)
                    loadCaseModel = new LoadCaseModel(lccc);
                else
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Input case must be a load case");
                    return;
                }
            }
            if (loadType.Any(i => i.Value.GetType() != typeof(ModalTargetLoadModel)))
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Input case must be a modale ritz acceleration load");
                return;
            }

            var data = new ModalEigenModel()
            {
                Name = name,
                MaxModes = maxModes,
                MinModes = minModes,
                LoadType = [.. loadType.Select(i => (ModalTargetLoadModel)i.Value)],
                EigenCutOff = eigenCutOff,
                EigenTol = eigenTol,
                AllowAutoFreqShift = allowAutoFreqShift,
                EigenShiftFreq = eigenShiftFreq,
            };

            if (initialCase != null)
                data.InitialCase = loadCaseModel;

            var modalEigenModel = new ModalEigenModel(data);
            var gH_Function = new GH_Case(modalEigenModel);
            Message = name;

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("03d4d9f2-5ba7-43a7-9ae8-7bb471b16835");
    }
}
303 files24 directories