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

namespace FeMM.Grasshopper.Components.Loads
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class PlatePressureComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the PlatePressureComponent class.
        /// </summary>
        public PlatePressureComponent()
          : base("Plate Pressure", "PP", "Assign a plate pressure", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_LOADS)
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddGenericParameter("Plate", "P", "The plate where add the pressure", GH_ParamAccess.item);
            pManager.AddGenericParameter("LoadCase", "LC", "The load case", GH_ParamAccess.item);

            var cs_param = new CoordinateSystemParam
            {
                Optional = true
            };
            pManager.AddParameter(cs_param, "Coordinate System", "CS", "The coordinate system of the load. Default value: Global Coordinate System", GH_ParamAccess.item);

            pManager.AddIntegerParameter("Face", "F", "Face", GH_ParamAccess.item, 0);
            Param_Integer type_param = (Param_Integer)pManager[3];
            type_param.AddNamedValue("Top", 0);
            type_param.AddNamedValue("Bottom", 1);

            pManager.AddGenericParameter("Value", "V", "The value of pressure", GH_ParamAccess.item);
            pManager.AddBooleanParameter("Projected", "P", "Tell if the pressure value is projected", GH_ParamAccess.item, false);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Plate", "P", "The modified plate", 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)
        {
            GH_Plate plate = null;
            GH_Case lc = null;
            GH_FunctionDefinition cs = null;
            int face = 0;
            if (!DA.GetData("Plate", ref plate))
                return;
            if (!DA.GetData("LoadCase", ref lc))
                return;
            bool has_cs = DA.GetData("Coordinate System", ref cs);
            if (!DA.GetData("Face", ref face))
                return;
            object obj = null;
            if (!DA.GetData("Value", ref obj))
                return;
            bool projected = false;
            if (!DA.GetData("Projected", ref projected))
                return;

            Vector3d value = Vector3d.Unset;
            if (obj.GetType() == typeof(GH_Number))
            {
                double val = ((GH_Number)obj).Value;
                value = new Vector3d(0, 0, val);
            }
            else if (obj.GetType() == typeof(GH_String))
            {
                double val = Convert.ToDouble(((GH_String)obj).Value);
                value = new Vector3d(0, 0, val);
            }
            else if (obj.GetType() == typeof(GH_Vector))
            {
                value = ((GH_Vector)obj).Value;
            }
            else
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid Value");
                return;
            }

            var load_value = new PlatePressureModel.LoadData()
            {
                Face = (PlatePressureModel.LoadFace)face,
                P = value,
                Projected = projected
            };
            if (cs != null && cs.Value is CoordinateSystemModel css)
                load_value.CoordinateSystem = css;
            var load = new PlatePressureModel((LoadCaseModel)lc.Value, load_value)
            {
                CoordinateSystem = load_value.CoordinateSystem
            };
            var newPlate = new GH_Plate(plate);
            newPlate.Value.Loads.Add(load);
            Message = string.Format("Coordinate System: {0}", load.CoordinateSystem.Name);
            DA.SetData(0, newPlate);
        }

        public override GH_Exposure Exposure => GH_Exposure.tertiary;

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("95bf1a7e-b6de-4322-ba8b-6ec4bb3bee8e");
    }
}
303 files24 directories