using FeMM.Common.Models;
using FeMM.Grasshopper.Components.Parameters;
using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using GH_IO.Serialization;
using Grasshopper.Kernel;
using Rhino.Geometry;
using System;
using System.Runtime.Versioning;

namespace FeMM.Grasshopper.Components.Loads
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class BeamPointMomentComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the BeamPointMomentComponent class.
        /// </summary>
        public BeamPointMomentComponent()
          : base("Beam Point Moment", "BPM", "Defines the beam point load moment", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_LOADS)
        {
        }

        public override void CreateAttributes()
        {
            var attr = new ComponentAttributes.ComponentWithImageAttributes(this, Properties.Resources.BeamPointMomentImage);
            attr.AddOverlappedBitmap(new System.Drawing.PointF(5, -65), Properties.Resources.LocalAxisImage);
            m_attributes = attr;
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddGenericParameter("Beam", "B", "The beam where add the Force point moment load", GH_ParamAccess.item);
            pManager.AddGenericParameter("LoadCase", "LC", "The load case", GH_ParamAccess.item);
            var cs_param = new CoordinateSystemParam
            {
                Optional = true
            };
            cs_param.ObjectChanged += Param_CoordinateSystem_ObjectChanged;
            pManager.AddParameter(cs_param, "Coordinate System", "CS", "The coordinate system of the load. Default value: Global Coordinate System", GH_ParamAccess.item);
            pManager.AddNumberParameter("M1", "M1", "The load in the 1 axis", GH_ParamAccess.item, 0);
            pManager.AddNumberParameter("M2", "M2", "The load in the 2 axis", GH_ParamAccess.item, 0);
            pManager.AddNumberParameter("M3", "M3", "The load in the 3 axis", GH_ParamAccess.item, 0);
            pManager.AddNumberParameter("a", "a", "The distance ratio (0-1) from the starting node", GH_ParamAccess.item);
        }

        private void Param_CoordinateSystem_ObjectChanged(IGH_DocumentObject sender, GH_ObjectChangedEventArgs e)
        {
            SetInputsName();
        }

        private void SetInputsName()
        {
            CoordinateSystemParam cs_param = (CoordinateSystemParam)Params.Input[2];
            if (cs_param.PersistentData.Branches.Count == 0)
            {
                Params.Input[3].NickName = "M1";
                Params.Input[4].NickName = "M2";
                Params.Input[5].NickName = "M3";
            }
            else
            {
                Params.Input[3].NickName = "MX";
                Params.Input[4].NickName = "MY";
                Params.Input[5].NickName = "MZ";
            }
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Beam", "B", "The modified beam", 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)
        {
            SetInputsName();

            GH_Beam beam = null;
            GH_Case lc = null;
            GH_FunctionDefinition cs = null;
            double p1 = 0, p2 = 0, p3 = 0;
            double a = 0;

            if (!DA.GetData("Beam", ref beam))
                return;
            if (!DA.GetData("LoadCase", ref lc))
                return;
            bool has_cs = DA.GetData("Coordinate System", ref cs);
            bool p1_has_value = DA.GetData("M1", ref p1);
            bool p2_has_value = DA.GetData("M2", ref p2);
            bool p3_has_value = DA.GetData("M3", ref p3);
            bool a_has_value = DA.GetData("a", ref a);

            LoadCaseModel loadCaseModel = null;
            if (lc.Value != null && lc.Value is LoadCaseModel lccc)
                loadCaseModel = new LoadCaseModel(lccc);
            else
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Input case must be a load case");
                return;
            }


            if (loadCaseModel.NonStructuralMassAcceleration)
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "The connected LoadCase have the Non-structural mass flag set to on");

            var load_value = new BeamPointLoadModel.LoadData()
            {
                P = new Vector3d(p1, p2, p3),
                A = a
            };
            if (cs != null && cs.Value is CoordinateSystemModel css)
                load_value.CoordinateSystem = css;
            var load = new BeamPointLoadModel(loadCaseModel, load_value, BeamPointLoadModel.LoadType.Moment)
            {
                CoordinateSystem = load_value.CoordinateSystem
            };
            var newBeam = new GH_Beam(beam);
            newBeam.Value.Loads.Add(load);
            Message = string.Format("Coordinate System: {0}", load.CoordinateSystem.Name);

            DA.SetData(0, newBeam);
        }

        public override bool Read(GH_IReader reader)
        {
            bool success = base.Read(reader);

            SetInputsName();

            return success;
        }

        public override GH_Exposure Exposure => GH_Exposure.secondary;

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("a5aacb7a-9b2a-429f-8e27-09e2222369bf");
    }
}
303 files24 directories