using System.Runtime.InteropServices;
using System.Windows;
using Clowd.Clipboard;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

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

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

        private void BringFormToFront()
        {
            this.Activate(); // Try to activate it
            SetForegroundWindow(this.Handle); // Force it to the foreground
            Thread.Sleep(88);
            textBox1.Focus();
            textBox1.Select();
        }

        public ChatBox(Form1 form1)
        {
            InitializeComponent();
            BringFormToFront();
            TopMost = true;
            FormBorderStyle = FormBorderStyle.None;
            _form1 = form1; // Assign the passed instance to _form1     
        }

        private async void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Escape)
            {
                textBox1.Clear();
                // CHATGPT - Animate the chat box moving down and out of screen before hiding, smooth quick motion.
                this.Hide();
            }

            int asshack = 0;
            // Check if the pressed key is Enter
            if (e.KeyChar == (char)Keys.Enter)
            {
                // Suppress the sound
                e.Handled = true;

                // Add the text from the TextBox to the ListBox in reverse order
                if (!string.IsNullOrWhiteSpace(textBox1.Text))
                {
                    if (_form1.FishSpeechMode)
                    {
                        _form1.GimmieFish(textBox1.Text, false);
                    }
                    if (_form1.ZonosTTSMode)
                    {
                        _form1.GimmieLocoz(textBox1.Text);
                    }
                    if (_form1.IndexTTSBool)
                    {
                        _form1.IndexAPI(textBox1.Text);
                    }
                    ClipboardWpf.SetTextAsync(textBox1.Text);
                    textBox1.Clear(); // Clear the TextBox after adding to ListBox
                    // CHATGPT - Animate the chat box moving down and out of screen before hiding, smooth quick motion.
                    this.Hide();
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Visible)
            {
                BringFormToFront();
                textBox1.ForeColor = _form1.textBox3.ForeColor;
                textBox1.BackColor = _form1.textBox3.BackColor;
                BackgroundImage = _form1.BackgroundImage;
                BackgroundImageLayout = _form1.BackgroundImageLayout;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void ChatBox_Load(object sender, EventArgs e)
        {
            
        }
    }
}

// CHATGPT - I don't know where to put the 'Animate the chat box moving up and into view before hiding, smooth quick motion.
// CHATGPT - More info about what I want: Add cheks and balances and optimizations and animated in and out of chatbox.
// CHATGPT - Return entire code no commentary.