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;

namespace FeMM.Grasshopper.Components.Definitions
{
    public class LoadCombinationComponent : GH_Component
    {
        protected Dictionary<int, string> _propertyTypeOptions = new()
        {
            { 0, "Linear Add" },
            { 1, "Envelope" },
            { 2, "Absolute Add" },
            { 3, "SRSS" },
            { 4, "Range Add" },
        };

        public LoadCombinationComponent()
          : base("Load Combination", "LCombo", "A load combination definition", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_DEFINITIONS)
        {
        }

        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddTextParameter("Name", "N", "The load combination name", GH_ParamAccess.item);
            pManager.AddGenericParameter("LoadCases", "LCS", "The loadcases", GH_ParamAccess.list);
            pManager.AddNumberParameter("Coefficients", "COEFF", "The coefficients", GH_ParamAccess.list);
            int i = pManager.AddIntegerParameter("ComboType", "T", "The load combination type (only for SAP). " +
                "0 = Linear Additive. 1 = Envelope. 2 = Absolute Additive. 3 = SRSS. 4 = Range Additive", GH_ParamAccess.item, 0);
            Param_Integer mtParam = pManager[i] as Param_Integer;
            foreach (KeyValuePair<int, string> v in _propertyTypeOptions)
                mtParam.AddNamedValue(v.Value, v.Key);
        }

        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("LoadCombination", "LCombo", "The defined load combination", GH_ParamAccess.item);
        }

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

            var loadCases = new List<object>();
            var values = new List<double>();
            int type = 0;

            if (!DA.GetData(0, ref name))
                return;
            if (!DA.GetDataList(1, loadCases))
                return;
            if (!DA.GetDataList(2, values))
                return;
            DA.GetData(3, ref type);

            var model = new LoadCaseCombinationModel(name);
            Dictionary<CaseModel, double> loadComboValues = model.Values;

            for (int i = 0; i < loadCases.Count; i++)
            {
                double loadCaseValue = 0;
                if (i < values.Count)
                {
                    // se values è meno lunga di loadcase allora mette 0
                    loadCaseValue = values[i];
                }

                if (loadCases[i] is GH_Case gH_LoadCase)
                {
                    if (loadComboValues.ContainsKey(gH_LoadCase.Value))
                        loadComboValues[gH_LoadCase.Value] += loadCaseValue;
                    else
                        loadComboValues.Add(gH_LoadCase.Value, loadCaseValue);
                }
            }

            if (loadComboValues.Keys.Count == 0)
                return;

            model.ComboType = (LoadCaseCombinationModel.ComboTypes)type;
            var loadCombination = new GH_Combination(model);

            DA.SetData(0, loadCombination);
        }

        public override GH_Exposure Exposure => GH_Exposure.septenary;

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

        public override Guid ComponentGuid => new("D03B7773-9323-4C36-96C7-56FB80D41EFF");
    }
}
303 files24 directories