using FeMM.Common.Models;
using FeMM.Grasshopper.Components.Parameters;
using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Versioning;

namespace FeMM.Grasshopper.Components.Definitions
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class StagedConstructionCombinationsComponent : GH_Component
    {
        protected List<CaseModel> _loadCases;

        /// <summary>
        /// Initializes a new instance of the StagedConstructionComponent class.
        /// </summary>
        public StagedConstructionCombinationsComponent()
          : base("Staged Construction Combinations", "SC", "Defines a staged construction", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_DEFINITIONS)
        {
            _loadCases = [];
        }

        /// <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 increment stage", GH_ParamAccess.item);
            pManager.AddGenericParameter("Staged Construction", "SM", "The staged Construction model", GH_ParamAccess.item);
            pManager.AddGenericParameter("Load Combinations", "LC", "The load combination", GH_ParamAccess.list);
            pManager.AddGenericParameter("Freedom Case Combinations", "FC", "The freedom case combination", GH_ParamAccess.list);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Staged Combinations", "SC", "The defined staged contruction", 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;
            GH_FunctionDefinition gH_FunctionDefinition = null;
            var combinations = new List<GH_Combination>();
            var freedomCases = new List<GH_Combination>();

            int nnn = 0;
            if (!DA.GetData(nnn++, ref name))
                return;
            if (!DA.GetData(nnn++, ref gH_FunctionDefinition))
                return;
            if (!DA.GetDataList(nnn++, combinations))
                return;
            if(!DA.GetDataList(nnn++, freedomCases))
                return;

            if (combinations.Count == 0)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Input parameter Load Combinations failed to collect data");
                return;
            }

            if (combinations.Count != freedomCases.Count)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Load combinations and freedom cases combination must have the same length");
                return;
            }

            if(gH_FunctionDefinition == null || gH_FunctionDefinition.Value == null || gH_FunctionDefinition.Value.GetType() != typeof(StagedModel))
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Error Staged Construction input");
                return;
            }
            var model = new StagedConstructionCombinationModel(name)
            {
                StagedModel = (StagedModel)gH_FunctionDefinition.Value,
                LoadCombinations = [.. combinations.Where(g => g.Value is LoadCaseCombinationModel).Select(g => (LoadCaseCombinationModel)g.Value)],
                FreedomCases = freedomCases.Count == 0 ? FreedomCaseCombinationModel.DefaultFreedomCaseCombinations :
                [.. freedomCases.Where(g => g.Value is FreedomCaseCombinationModel).Select(g => (FreedomCaseCombinationModel)g.Value)],
            };
            Message = name;

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

        public override GH_Exposure Exposure => GH_Exposure.septenary;

        /// <summary>
        /// Provides an Icon for the component.
        /// </summary>
        protected override System.Drawing.Bitmap Icon => Properties.Resources.StagedConstructionIcon;

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("dc68b811-4480-47a8-a11f-3f4b7bfe8d8f");
    }
}
303 files24 directories