using System;
using System.Runtime.InteropServices;

namespace Valg.Eva.Skanning.Scan.Presentation
{
    public class WindowHider : IWindowHider
    {
        private const string readSoftWindowName = "ReadSoft FORMS Scan";

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool ShowWindowAsync(IntPtr handle, int showType);

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        [return: MarshalAs(UnmanagedType.SysInt)]
        private static extern IntPtr FindWindow(string className, string windowName);

        public void Hide()
        {
            const int hide = 0;
            var readSoftWindowHandle = FindWindow(null, readSoftWindowName);
            try
            {
                if (!ShowWindowAsync(readSoftWindowHandle, hide))
                    throw new Exception("Windows was not able to hide window.");
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public void Unhide()
        {
            const int normal = 1;
            var readSoftWindowHandle = FindWindow(null, readSoftWindowName);
            if (!ShowWindowAsync(readSoftWindowHandle, normal))
                throw new Exception("Windows was not able to show window.");
        }
    }
}

