using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using St7API;
using System;
using System.Collections.Generic;
using System.Text;

namespace FeMM.Grasshopper.Components.ST2R
{
    public class St7ReadGroupComponent : GH_Component
    {
        private bool _run = false;

        public St7ReadGroupComponent()
            : base("Read Groups", "ReadGroups",
                  "Read group names",
                  CategoryNameConstants.CATEGORY_F2R, CategoryNameConstants.SUBCATEGORY_ST2R_BAKE)
        {
        }

        public override void CreateAttributes()
        {
            var buttonAttributes = new ComponentAttributes.ComponentOneButtonAttributes(this, "Read");
            buttonAttributes.ButtonPressed += () =>
            {
                _run = true;
                ExpireSolution(true);
            };
            m_attributes = buttonAttributes;
        }

        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddIntegerParameter("uID", "ID",
                "The uID of the already opened Straus7 model", GH_ParamAccess.item);
        }

        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddTextParameter("Groups", "Groups", "All groups in the model", GH_ParamAccess.list);
            pManager.AddTextParameter("Log", "Log", "Debug log messages", GH_ParamAccess.list);
        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            int uID = 0;

            if (!DA.GetData(0, ref uID)) return;

            List<string> log = [];
            List<string> groups = [];

            if (!_run)
            {
                DA.SetDataList(0, groups);
                DA.SetDataList(1, log);
                return;
            }

            log.Add($"Reading model with uID = {uID}");

            // Read groups
            int index = 1;
            while (true)
            {
                StringBuilder gName = new(256);
                int groupID = 0;

                int err = St7.St7GetGroupByIndex(uID, index, gName, gName.Capacity, ref groupID);

                if (err != St7.ERR7_NoError)
                {
                    log.Add("Stopped reading groups (API returned error).");
                    break;
                }

                groups.Add($"{gName.ToString().TrimEnd('\0')}");
                log.Add($"Found group {index}: {gName}");

                index++;
            }

            if (groups.Count == 0)
                log.Add("No groups found (or model not opened).");

            // Output: first groups, then log
            DA.SetDataList(0, groups);
            DA.SetDataList(1, log);

            _run = false;
        }

        protected override System.Drawing.Bitmap Icon => Properties.Resources.St7ReadGroupsIcon;

        public override Guid ComponentGuid =>
            new("bb1e2ab3-22c1-4b77-9c77-112233445566");
    }
}
303 files24 directories