using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

namespace GTPCorrgir
{
    public class KeyboardHelper
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);

        private const int KEYEVENTF_EXTENDEDKEY = 0x0001;
        private const int KEYEVENTF_KEYUP = 0x0002;
        private const int VK_CONTROL = 0x11;
        private const int VK_A = 0x41;
        private const int VK_C = 0x43;
        private const int VK_V = 0x56;

        private IntPtr previousWindow;

        public void SaveCurrentWindow()
        {
            previousWindow = GetForegroundWindow();
        }

        public async Task AutoCopyFromActiveWindow()
        {
            try
            {
                // Asegurarse de que la ventana anterior tenga el foco
                SetForegroundWindow(previousWindow);

                // Pequeña pausa para asegurar que la ventana tiene el foco
                await Task.Delay(100);

                if (Opciones.Instance.FuncionesOpcionales == Opciones.funcionesOpcionales.CtrlA)
                {
                    // Simular Ctrl+A
                    keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY, UIntPtr.Zero);
                    keybd_event(VK_A, 0, KEYEVENTF_EXTENDEDKEY, UIntPtr.Zero);
                    keybd_event(VK_A, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, UIntPtr.Zero);
                    keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, UIntPtr.Zero);
                }
                // Pequeña pausa entre comandos
                await Task.Delay(50);

                // Simular Ctrl+C
                keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY, UIntPtr.Zero);
                keybd_event(VK_C, 0, KEYEVENTF_EXTENDEDKEY, UIntPtr.Zero);
                keybd_event(VK_C, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, UIntPtr.Zero);
                keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, UIntPtr.Zero);

                // Dar tiempo para que se complete la operación de copiado
                await Task.Delay(100);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error en AutoCopyFromActiveWindow: {ex.Message}");
            }
        }

        public async Task RestoreAndSimulatePaste()
        {
            try
            {
                // Asegurarse de que la ventana anterior tenga el foco
                SetForegroundWindow(previousWindow);

                // Pequeña pausa para asegurar que la ventana tiene el foco
                await Task.Delay(100);

                // Simular Ctrl+V
                keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY, UIntPtr.Zero);
                keybd_event(VK_V, 0, KEYEVENTF_EXTENDEDKEY, UIntPtr.Zero);
                keybd_event(VK_V, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, UIntPtr.Zero);
                keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, UIntPtr.Zero);

                // Dar tiempo para que se complete la operación de pegado
                await Task.Delay(100);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error en RestoreAndSimulatePaste: {ex.Message}");
            }
        }
    }
}