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.Definitions
{
    public class FreedomCaseCombinationComponent : GH_Component
    {
        public FreedomCaseCombinationComponent()
          : base("Freedom Case Combination", "FCombo", "A freedom case 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("Freedom Case", "FCS", "The freedom cases", GH_ParamAccess.list);
            pManager.AddNumberParameter("Coefficients", "COEFF", "The coefficients", GH_ParamAccess.list);
        }

        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Freedom case combination", "FCombo", "The defined freedom case combination", GH_ParamAccess.item);
        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            string name = "";
            var loadCases = new List<GH_FreedomCase>();
            var values = new List<double>();

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

            var model = new FreedomCaseCombinationModel(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 (loadComboValues.ContainsKey(loadCases[i].Value))
                {
                    loadComboValues[loadCases[i].Value] += loadCaseValue;
                }
                else
                {
                    loadComboValues.Add(loadCases[i].Value, loadCaseValue);
                }
            }

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

            var freedomCaseCombination = new GH_Combination(model);

            DA.SetData(0, freedomCaseCombination);
        }

        public override GH_Exposure Exposure => GH_Exposure.septenary;

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

        public override Guid ComponentGuid => new("95ed7d29-c6e7-47f9-a0d2-8b2b248d8ddb");
    }
}
303 files24 directories