using FeMM.Common.Models;
using FeMM.Grasshopper.Components.Parameters;
using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
using System;
using System.Collections.Generic;

namespace FeMM.Grasshopper.Components.Definitions
{
    public class ConstrainBodyComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the GroupComponent class.
        /// </summary>
        public ConstrainBodyComponent()
          : base("Body Constraint", "B", "Defines a Body Constraint", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_DEFINITIONS)
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddTextParameter("Name", "N", "The load case name", GH_ParamAccess.item);
            int i = 0;
            i = pManager.AddGenericParameter("Transational", "T", "The transational restraints", GH_ParamAccess.list);
            pManager[i].Optional = true;
            i = pManager.AddGenericParameter("Rotation", "R", "The rotation restraints", GH_ParamAccess.list);
            pManager[i].Optional = true;
            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);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Body", "B", "The defined Body", 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 = "";
            var trans = new List<IGH_Goo>();
            var rot = new List<IGH_Goo>();
            GH_FunctionDefinition cs = null;
            if (!DA.GetData(0, ref name))
                return;
            DA.GetDataList(1, trans);
            DA.GetDataList(2, rot);
            bool has_cs = DA.GetData("Coordinate System", ref cs);

            Message = name;

            var nodeBodyConstraintModel = new NodeBodyConstraintModel(name);
            if(has_cs)
            {
                if(cs.Value != null && cs.Value is CoordinateSystemModel csm)
                    nodeBodyConstraintModel .CoordinateSystem = csm;
            }
            else
                nodeBodyConstraintModel.CoordinateSystem = CoordinateSystemModel.Global;

            for (int i = 0; i < trans.Count && i < 3; i++)
            {
                if (trans[i].CastTo(out bool b))
                {
                    if (i == 0)
                        nodeBodyConstraintModel.Ux = b;
                    else if (i == 1)
                        nodeBodyConstraintModel.Uy = b;
                    else if (i == 2)
                        nodeBodyConstraintModel.Uz = b;
                    continue;
                }
                if (trans[i].CastTo(out double n))
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Defines the displacement using Displacement load");
                    DA.AbortComponentSolution();
                    return;
                }
                if (trans[i].CastTo(out string s))
                {
                    if (bool.TryParse(s, out b))
                    {
                        if (i == 0)
                            nodeBodyConstraintModel.Ux = b;
                        else if (i == 1)
                            nodeBodyConstraintModel.Uy = b;
                        else if (i == 2)
                            nodeBodyConstraintModel.Uz = b;
                    }
                    else if (double.TryParse(s, out n))
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Defines the displacement using Displacement load");
                        DA.AbortComponentSolution();
                        return;
                    }
                    continue;
                }
            }

            for (int i = 0; i < rot.Count && i < 3; i++)
            {
                if (rot[i].CastTo(out bool b))
                {
                    if (i == 0)
                        nodeBodyConstraintModel.Rx = b;
                    else if (i == 1)
                        nodeBodyConstraintModel.Ry = b;
                    else if (i == 2)
                        nodeBodyConstraintModel.Rz = b;
                }
                if (rot[i].CastTo(out double n))
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Defines the displacement using Displacement load");
                    DA.AbortComponentSolution();
                    return;
                }
                if (rot[i].CastTo(out string s))
                {
                    if (bool.TryParse(s, out b))
                    {
                        if (i == 0)
                            nodeBodyConstraintModel.Rx = b;
                        else if (i == 1)
                            nodeBodyConstraintModel.Ry = b;
                        else if (i == 2)
                            nodeBodyConstraintModel.Rz = b;
                    }
                    else if (double.TryParse(s, out n))
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Defines the displacement using Displacement load");
                        DA.AbortComponentSolution();
                        return;
                    }
                }
            }

            var fd = new GH_FunctionDefinition(nodeBodyConstraintModel);
            DA.SetData(0, fd);
        }

        public override GH_Exposure Exposure => GH_Exposure.secondary;

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("cbcc5c62-2b89-4ddc-9ffe-4fabb296159b");
    }
}
303 files24 directories