using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoIt;

namespace ScanningRpcService
{
    public class NativeWindowsActivateFocusHelper
    {
        private readonly string[] _windowsToActivate = new string[] {"[TITLE:Configure Device Settings]"};
        private readonly ISet<string> _alreadyActivatedWindows = new HashSet<string>();

        public async Task StartListening()
        {
            while (true)
            {
                foreach (var window in _windowsToActivate)
                {
                    if (AutoItX.WinExists(window) == 1 && AutoItX.WinActive(window) <= 0 &&
                        !_alreadyActivatedWindows.Contains(window))
                    {
                        var handle = AutoItX.WinGetHandle(window);
                        AutoItX.WinActivate(handle);
                        _alreadyActivatedWindows.Add(window);
                    }

                    await Task.Delay(100);
                }


                foreach (var window in _alreadyActivatedWindows)
                {
                    if (AutoItX.WinExists(window) <= 0)
                    {
                        _alreadyActivatedWindows.Remove(window);
                    }

                    await Task.Delay(100);
                }
            }
        }
    }
}

