using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using System;
using System.Runtime.Versioning;

using AppsClient.Profiles;
using FeMM.Grasshopper.DataTypes.FeMM;
using Grasshopper.Documentation;
using Grasshopper.Kernel.Types;

namespace FeMM.Grasshopper.Components.Sections
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class NomenclaturePropertiesComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the BeamPropertyComponent class.
        /// </summary>
        public NomenclaturePropertiesComponent()
          : base("Maffeis Name Property v2.0", "MP", "Extract a property by its name from nomenclature.", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_SECTIONS)
        {

        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddGenericParameter("Section or Name", "S", "The section or its name with Maffeis Nomenclature", GH_ParamAccess.item);
            pManager.AddTextParameter("Property Name", "P", "The name of the property to extract", GH_ParamAccess.item, "");

        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddTextParameter("Property Value", "PV", "The value of the selected property if exists or null.", GH_ParamAccess.item);
            pManager.AddTextParameter("All properties", "AP", "All available properties for this profile", 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)
        {
            var sectionObject = new Object();
            string maffeisName = "";
            string propertyName = "";

            if (!DA.GetData(0, ref sectionObject))
                return; 
            if (!DA.GetData(1, ref propertyName))
                return;
            
            if (sectionObject.GetType().Name == "GH_String")
            {
                var ghstring = (GH_String)sectionObject;
                maffeisName = ghstring.Value;
            }
            else if (sectionObject.GetType().Name == "GH_Text")
            {
                var ghtxt = (GH_Text)sectionObject;
                maffeisName = ghtxt.Text;
            }
            else if (sectionObject.GetType().Name == "GH_BeamSection")
            {
                var ghsec = (GH_BeamSection)sectionObject;
                maffeisName = ghsec.MaffeisName;
            }
            else {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid section input");
                return;
            }


            var profile = Profiles.GetProfileByMaffeisName(maffeisName);
            if (profile == null) { 
                return;
            }

            string propVal = null;
            string allProps = "";
            foreach (var propInfo in profile.GetType().GetProperties()) {
                var val = profile.GetType().GetProperty(propInfo.Name).GetValue(profile);
                if (val != null) {
                    var spaces = " ";
                    for (int i = 0; i < 16 - propInfo.Name.Length; ++i) {
                        spaces += " ";
                    }

                    allProps += ($"{propInfo.Name}{spaces}{val.ToString()}\n");

                    if (propertyName == propInfo.Name) {
                        propVal = val.ToString();
                    }
                }
            }

            DA.SetData(0, propVal);
            DA.SetData(1, allProps);
        }

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

        public override GH_Exposure Exposure => GH_Exposure.tertiary;

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("6fffa1a1-3bcb-4821-bd0e-920e514071f9");
    }
}
303 files24 directories