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 LoadCaseLinearStaticSAPComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the LoadCaseComponent class.
        /// </summary>
        public LoadCaseLinearStaticSAPComponent()
          : base("Linear Static Load Case for SAP", "LSLC", "A linear static load case definition", 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 load case name", GH_ParamAccess.item);
            int j = pManager.AddGenericParameter("Initial Case", "I", "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[j].Optional = true;
            pManager.AddGenericParameter("Load Patterns", "LN", "This is an array that includes the name of each load assigned to the load case.", GH_ParamAccess.list);
            pManager.AddNumberParameter("Scale Factor", "SF", "This is an array that includes the scale factor of each load assigned to the load case. [L/s2]", GH_ParamAccess.list);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Load Case", "LC", "The defined load case", 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 = "";
            GH_Case initialCase = null;
            var loadNames = new List<GH_Case>();
            var scaleFactor = new List<double>();

            if (!DA.GetData(0, ref name))
                return;
            DA.GetData(1, ref initialCase);
            if (!DA.GetDataList(2, loadNames))
                return;
            if (!DA.GetDataList(3, scaleFactor))
                return;

            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 (loadNames.Any(i => i.Value.GetType() != typeof(LoadCaseModel)))
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Load case must be a FeMM Load Case");
                return;
            }

            LoadCaseLinearStaticSAPModel.LoadTypes[] loadTypes = new LoadCaseLinearStaticSAPModel.LoadTypes[loadNames.Count];
            for (int i = 0; i < loadNames.Count; i++)            
                loadTypes[i] = LoadCaseLinearStaticSAPModel.LoadTypes.Load;
            
            var lc = new LoadCaseLinearStaticSAPModel(-1, name)
            {
                ScaleFactors = [.. scaleFactor],
                LoadNames = [.. loadNames.Select(j => (LoadCaseModel)j.Value)],
                LoadType = loadTypes
            };

            if (initialCase != null && initialCase.IsValid)
                lc.InitialCase = initialCase.Value;
            else
                lc.InitialCase = null;

            Message = name;

            DA.SetData(0, new GH_Case(lc));
        }

        public override GH_Exposure Exposure => GH_Exposure.senary;

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

        public override Guid ComponentGuid => new("0476a849-ad22-4769-aada-b62eec21fbfe");
    }
}
303 files24 directories