using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;

namespace SPC.Kiosk.Common
{
    public static class AiScannerProcess
    {
        [DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string SClassName, string SWindowName);
        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr findname);
        [DllImport("user32.dll")]
        private static extern bool ShowWindowAsync(IntPtr findname, int howShow);
        [DllImport("user32.dll")]
        private static extern bool BringWindowToTop(IntPtr findname);
        [DllImport("user32.dll")]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        const UInt32 SWP_NOSIZE = 0x0001;
        const UInt32 SWP_NOMOVE = 0x0002;
        const UInt32 SWP_SHOWWINDOW = 0x0040;

        private static Process AiScannerExe = null;

        public static bool Excuted
        {
            get
            {
                if (AiScannerExe != null)
                {
                    return !AiScannerExe.HasExited;
                }
                else
                {
                    return false;
                }
            }
        }

        public static void ExcuteAiScanner(IntPtr _openner)
        {
            try
            {
                if (Excuted || AiScannerExe != null) StopAiScanner();
                var fileName = Path.Combine(ResourceManager.BinPath, CommonValue.AiScannerPath);
                if (File.Exists(fileName))
                {
                    var newStartInfo = new ProcessStartInfo
                    {
                        FileName = fileName,
                        UseShellExecute = false,
                        //WindowStyle= ProcessWindowStyle.Hidden,
                        ErrorDialog = false,
                        CreateNoWindow = true,
                    };
                    AiScannerExe = new Process();
                    AiScannerExe.StartInfo = newStartInfo;
                    if (AiScannerExe.Start())
                    {
                        AiScannerExe.WaitForInputIdle(500);
                        SetParent(AiScannerExe.MainWindowHandle, _openner);
                        //AiScannerExe.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                    }
                }
                else
                {
                    CommonLog.ErrorLogWrite("SPC.Kiosk.Common", "AiScannerProcess", "ExcuteAiScanner()", "File Not Exists !!", string.Format("\nFile Path: {0}", fileName));
                }

            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite("SPC.Kiosk.Common", "AiScannerProcess", "ExcuteAiScanner()", "Fail !!", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
            }
        }
        public static void StopAiScanner()
        {
            if (Excuted)
            {
                AiScannerExe.Kill();
                AiScannerExe.WaitForExit();
                AiScannerExe.Dispose();
                AiScannerExe = null;
            }
            else
            {
                if (AiScannerExe != null) AiScannerExe = null;
            }
        }
    }
}