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 StagedConstructionComponent : GH_Component
    {
        protected List<CaseModel> _loadCases;

        /// <summary>
        /// Initializes a new instance of the StagedConstructionComponent class.
        /// </summary>
        public StagedConstructionComponent()
          : base("Staged Construction ", "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 stage", GH_ParamAccess.item);
            int i = pManager.AddGenericParameter("Groups", "G", "The groups to assign to the beam", GH_ParamAccess.list);
            pManager[i].Optional = true;
            pManager.AddBooleanParameter("Morph", "M", "Morph the elements to their new positions", GH_ParamAccess.item, false);
            pManager.AddBooleanParameter("Fixed Node", "FN", "Move fixed nodes option", GH_ParamAccess.item, false);
            pManager.AddBooleanParameter("Rotate clusters", "RC", "Remove the rigid body rotation of each cluster of elements", GH_ParamAccess.item, false);
            pManager.AddBooleanParameter("Reset", "R", "Stage reset option", GH_ParamAccess.item, false);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Staged Construction", "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;
            var groups = new List<GH_Group>();
            bool morph = false;
            bool rotate = false;
            bool fixedNode = false;
            bool reset = false;
            int index = 0;

            int nnn = 0;
            if (!DA.GetData(nnn++, ref name))
                return;
            if (!DA.GetDataList(nnn++, groups))
                return;
            DA.GetData(nnn++, ref morph);
            DA.GetData(nnn++, ref fixedNode);
            DA.GetData(nnn++, ref rotate);
            DA.GetData(nnn++, ref reset);

            // Connect the LoadCombinationsComponent
            IEnumerable<IGH_Param> combinations_params = Params.Input[2].Sources.Where(s => s.GetType() == typeof(LoadCombinationsParam));
            foreach (LoadCombinationsParam param in combinations_params.Cast<LoadCombinationsParam>())
                param.UpdateLoadCases(_loadCases);

            var sc = new StagedModel
            {
                Name = name,
                FixedNode = fixedNode,
                Morph = morph,
                Reset = reset,
                Rotate = rotate,
                Index = index,
                Groups = [.. groups.Select(g => g.Value)],
            };
            Message = name;

            DA.SetData(0, new GH_FunctionDefinition(sc));
        }

        public void SetLoadCases(IEnumerable<CaseModel> loadCases)
        {
            _loadCases.Clear();
            _loadCases.AddRange(loadCases);
        }

        public override GH_Exposure Exposure => GH_Exposure.quarternary;

        /// <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("60d90bc8-d44e-4f07-a8a6-cdebf508cd23");
    }
}
303 files24 directories