the active window is not being set to the chatbox sometimes I want to be able to when it shows up always set it to focus the chatbox even if the app is minimized or more return entire code fix

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Clowd.Clipboard;

namespace ByteBreath
{
    public partial class ChatBox : Form
    {
        private Form1 _form1;

        private CancellationTokenSource animationCancellationTokenSource = new CancellationTokenSource();

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        public void BringFormToFront()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(BringFormToFront));
                return;
            }

            this.Activate();
            SetForegroundWindow(this.Handle);
            textBox1.Focus();
            textBox1.Select();
        }

        public ChatBox(Form1 form1)
        {
            InitializeComponent();

            TopMost = true;
            FormBorderStyle = FormBorderStyle.None;
            _form1 = form1;

            this.Load += ChatBox_Load;
            this.FormClosing += ChatBox_FormClosing;

            // Optional: Set desired client size to fit textbox properly if required here
        }

        public async void Poop()
        {
            var workingArea = Screen.FromControl(this).WorkingArea;

            // Position form horizontally centered
            this.Left = (workingArea.Left + (workingArea.Width - this.Width) / 2);

            // Calculate targetTop so that textbox1 is at screen Y=workingArea.Top + 886
            int desiredTextBoxScreenY = workingArea.Top + 886;
            int targetTop = desiredTextBoxScreenY - textBox1.Location.Y;

            this.Top = workingArea.Bottom; // Start below screen

            this.Opacity = 1;
            if (!this.Visible)
                this.Show();

            await AnimateToPositionAsync(targetTop, true);
        }

        private async void ChatBox_Load(object sender, EventArgs e)
        {
            var workingArea = Screen.FromControl(this).WorkingArea;

            // Position form horizontally centered
            this.Left = (workingArea.Left + (workingArea.Width - this.Width) / 2);

            // Calculate targetTop so that textbox1 is at screen Y=workingArea.Top + 886
            int desiredTextBoxScreenY = workingArea.Top + 886;
            int targetTop = desiredTextBoxScreenY - textBox1.Location.Y;

            this.Top = workingArea.Bottom; // Start below screen

            this.Opacity = 1;
            if (!this.Visible)
                this.Show();

            await AnimateToPositionAsync(targetTop, true);
        }

        public async Task AnimateToPositionAsync(int targetTop, bool isShowing)
        {
            animationCancellationTokenSource.Cancel();
            animationCancellationTokenSource.Dispose();
            animationCancellationTokenSource = new CancellationTokenSource();
            CancellationToken token = animationCancellationTokenSource.Token;

            var workingArea = Screen.FromControl(this).WorkingArea;
            int startTop = this.Top;
            int endTop = isShowing ? targetTop : workingArea.Bottom;

            int animationDurationMs = 180; // 180ms for snappy but smooth animation
            int frameIntervalMs = 1000 / 60; // 60 FPS
            int totalFrames = animationDurationMs / frameIntervalMs;

            for (int frame = 0; frame <= totalFrames; frame++)
            {
                if (token.IsCancellationRequested)
                    return;

                double progress = frame / (double)totalFrames;

                // Ease in/out cubic for smoothness
                double easedProgress = isShowing
                    ? 1 - Math.Pow(1 - progress, 3)   // ease out when showing
                    : Math.Pow(progress, 3);           // ease in when hiding

                int newTop = (int)(startTop + (endTop - startTop) * easedProgress);

                if (this.InvokeRequired)
                {
                    this.Invoke(new Action(() => this.Top = newTop));
                }
                else
                {
                    this.Top = newTop;
                }

                await Task.Delay(frameIntervalMs);
            }

            this.Top = endTop;

            if (!isShowing)
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new Action(() => this.Hide()));
                }
                else
                {
                    this.Hide();
                }
            }
            else
            {
                BringFormToFront();
                // Focus the TextBox after animation is done
                if (this.InvokeRequired)
                    this.Invoke(new Action(() => { textBox1.Focus(); textBox1.Select(); }));
                else
                    textBox1.Focus();
            }
        }

        private async void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Escape)
            {
                e.Handled = true;
                textBox1.Clear();
                await AnimateToPositionAsync(Screen.FromControl(this).WorkingArea.Bottom, false);
                return;
            }

            if (e.KeyChar == (char)Keys.Enter)
            {
                e.Handled = true;

                string text = textBox1.Text.Trim();
                if (!string.IsNullOrEmpty(text))
                {
                    if (_form1.FishSpeechMode)
                        _form1.GimmieFish(text, false);
                    if (_form1.ZonosTTSMode)
                        _form1.GimmieLocoz(text);
                    if (_form1.IndexTTSBool)
                        _form1.IndexAPI(text);

                    await ClipboardWpf.SetTextAsync(text);
                    textBox1.Clear();

                    await AnimateToPositionAsync(Screen.FromControl(this).WorkingArea.Bottom, false);
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Visible)
            {
                BringFormToFront();

                // Sync colors and background from _form1 as needed
                textBox1.ForeColor = _form1.textBox3.ForeColor;
                textBox1.BackColor = _form1.textBox3.BackColor;
                BackgroundImage = _form1.BackgroundImage;
                BackgroundImageLayout = _form1.BackgroundImageLayout;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            // Optional logic on textbox changes
        }

        private void ChatBox_FormClosing(object sender, FormClosingEventArgs e)
        {
            animationCancellationTokenSource.Cancel();
            animationCancellationTokenSource.Dispose();
        }
    }
}