using Rhino;
using Rhino.Geometry;
using Rhino.Input.Custom;
using Rhino.Input;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmreIstikam001.component.TehranOffice.Components.ÇİZİM_YARDIMCILARI.DetailSymbol
{
    public static class RhinoGetPointHelper
    {
        /// <summary>
        /// Allows the user to select two points to define a rectangle.
        /// </summary>
        /// <param name="prompt1">The prompt for the first corner.</param>
        /// <param name="prompt2">The prompt for the opposite corner.</param>
        /// <returns>A Polyline representing the rectangle, or null if cancelled.</returns>
        public static Polyline GetRectangleFromUser(string prompt1, string prompt2)
        {
            var doc = RhinoDoc.ActiveDoc;
            var points = new List<Point3d>();
            Point3d pt0;

            // Get the first corner of the rectangle
            using (var getPointAction = new GetPoint())
            {
                getPointAction.SetCommandPrompt(prompt1);
                if (getPointAction.Get() != GetResult.Point)
                {
                    RhinoApp.WriteLine("No point was selected.");
                    return null;
                }
                pt0 = getPointAction.Point();
            }

            Point3d pt1;

            // Get the opposite corner with dynamic drawing
            using (var getPointAction = new GetPoint())
            {
                getPointAction.SetCommandPrompt(prompt2);
                getPointAction.SetBasePoint(pt0, true);
                getPointAction.DynamicDraw += (sender, e) =>
                {
                    Point3d currentPt = e.CurrentPoint;

                    // Define the rectangle corners based on pt0 and currentPt
                    var rectangle = new Polyline(new[] {
                    pt0,
                    new Point3d(currentPt.X, pt0.Y, pt0.Z),
                    currentPt,
                    new Point3d(pt0.X, currentPt.Y, pt0.Z),
                    pt0 // Close the rectangle
                });

                    // Define other three corners based on pt0 and currentPt
                    Point3d pt2 = new Point3d(currentPt.X, pt0.Y, pt0.Z);
                    Point3d pt3 = new Point3d(pt0.X, currentPt.Y, pt0.Z);

                    // Draw temporary rectangle
                    e.Display.DrawPatternedLine(pt0, pt2, Color.CornflowerBlue, 61680, 2);
                    e.Display.DrawPatternedLine(pt2, currentPt, Color.CornflowerBlue, 61680, 2);
                    e.Display.DrawPatternedLine(currentPt, pt3, Color.CornflowerBlue, 61680, 2);
                    e.Display.DrawPatternedLine(pt3, pt0, Color.CornflowerBlue, 61680, 2);
                };

                if (getPointAction.Get() != GetResult.Point)
                {
                    RhinoApp.WriteLine("No end point was selected.");
                    return null;
                }
                pt1 = getPointAction.Point();
            }

            // Create the rectangle polyline
            var rectanglePolyline = new Polyline(new[] {
            pt0,
            new Point3d(pt1.X, pt0.Y, pt0.Z),
            pt1,
            new Point3d(pt0.X, pt1.Y, pt0.Z),
            pt0 // Close the rectangle
        });

            return rectanglePolyline;
        }

        /// <summary>
        /// Allows the user to select points for a leader annotation.
        /// </summary>
        /// <param name="prompt1">The prompt for the first point.</param>
        /// <param name="prompt2">The prompt for the second point.</param>
        /// <returns>A list of Point3d representing the leader points, or null if cancelled.</returns>
        public static void GetLeaderPointsFromUser(string prompt, RhinoInteractionHandler interactionHandler)
        {
            Point3d startPoint = Point3d.Unset;
            using (var getPointAction = new GetPoint())
            {
                getPointAction.SetCommandPrompt(prompt);
                getPointAction.AcceptNothing(true); // Allow pressing Enter to finish
                getPointAction.DynamicDraw += (sender, e) =>
                {
                    if (startPoint.IsValid)
                    {
                        // Draw the line from startPoint to current point
                        e.Display.DrawLine(startPoint, e.CurrentPoint, Color.CornflowerBlue, 2);
                    }
                };

                while (true)
                {
                    var res = getPointAction.Get();
                    if (interactionHandler.IsCancelled)
                    {
                        RhinoApp.WriteLine("Operation cancelled by user.");
                        break;
                    }

                    if (res == GetResult.Point)
                    {
                        if (!startPoint.IsValid)
                        {
                            // First point of the pair
                            startPoint = getPointAction.Point();
                            getPointAction.SetCommandPrompt("Select next point:");
                            getPointAction.SetBasePoint(startPoint, true);
                        }
                        else
                        {
                            // Second point of the pair
                            Point3d endPoint = getPointAction.Point();

                            // Process the pair
                            interactionHandler.ProcessLeaderPointPair(startPoint, endPoint, interactionHandler.IsCancelled);

                            // Reset startPoint
                            startPoint = Point3d.Unset;

                            // Reset command prompt
                            getPointAction.SetCommandPrompt("Select new Location or press Enter to finish:");
                        }
                    }
                    else if (res == GetResult.Nothing)
                    {
                        interactionHandler.GetGeometryData().Bake = true;
                        // User pressed Enter
                        break;
                    }
                    else
                    {
                        // Cancelled or error
                        RhinoApp.WriteLine("Point selection cancelled or invalid.");
                        break;
                    }
                }
            }
        }
    }
}


private void OnBakeClicked(object sender, EventArgs e)
{
    Bake = true;
    // Expire the solution to trigger SolveInstance
    rhinoInteractionHandler.CancelOperation();
    //rhinoInteractionHandler.ResetState(); // Implement this method as needed
    ExpireSolution(true);
    formManager.Dispose();
    conduit.Disable();
    RhinoDoc.ActiveDoc.Views.Redraw();
}

I want to exit an active command if the Bake Event is trigered