using irisHelper;
using irisRefactor.FrmService;
using irisRefactor.IrisSound;
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;

namespace irisRefactor.IrisThread
{
    public class ImageQualityAssessTh
    {
        private static ImageQualityAssessTh _imageQualityAssessTh = null;
        private ImageQualityAssessTh() { }
        public static ImageQualityAssessTh GetInstance()
        {
            if (_imageQualityAssessTh == null)
                _imageQualityAssessTh = new ImageQualityAssessTh();

            return _imageQualityAssessTh;
        }

        public void ImageQualityAssess()
        {
            LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, "ImageQualityAssess");

            int N = 0;
            ProMemory.Tag_IdentifyTimer = 0;

            try
            {
                //读取配置，拒识时长
                int rejectTimeOut = ProMemory.appConfig.RejectTimeOut;

                //开启计时器，在时间到之前匹配成功才停止
                AutoResetEvent autoEvent = new AutoResetEvent(false);
                TimerCallback timer2Delegate = new TimerCallback(Timer2ThreadMethod);
                ProMemory.timer2 = new Timer(timer2Delegate, autoEvent, rejectTimeOut, 0);

                while (ProMemory.irisConfig.AssessThreadRun && ProMemory.Tag_IdentifyTimer == 0&&!ProMemory.isSyning)
                {
                    if (ProMemory.irisConfig.Q2.Count > 0 && ProMemory.irisConfig.Q3.Count > 0)
                    {
                        lock (ProMemory.irisConfig.Q2)
                        {
                            if (ProMemory.irisConfig.Q2.Count > 0)
                            {
                                ProMemory.irisConfig.IrisFaceBuffer = ProMemory.irisConfig.Q2.Pop();
                            }
                        }

                        lock (ProMemory.irisConfig.Q3)
                        {
                            if (ProMemory.irisConfig.Q3.Count > 0)
                            {
                                ProMemory.irisConfig.IrisPosBuffer = ProMemory.irisConfig.Q3.Pop();
                            }
                        }

                        N++;
                        if (N == 3) PlaySound.inGreenFrame();//请对准绿框

                        if (ProMemory.irisConfig.IrisFaceBuffer != null && ProMemory.irisConfig.IrisPosBuffer != null)
                        {
                            IntPtr ptrIrisFace = Marshal.AllocHGlobal(1280 * 960);
                            Marshal.Copy(ProMemory.irisConfig.IrisFaceBuffer, 0, ptrIrisFace, 1280 * 960);

                            int score = 0;
                            Byte[] irisBytesL = new byte[640 * 480];
                            Byte[] irisBytesR = new byte[640 * 480];

                            unsafe
                            {
                                fixed (byte* irisL = &irisBytesL[0])
                                {
                                    fixed (byte* irisR = &irisBytesR[0])
                                    {
                                        byte* ptrIrisL = irisL;
                                        byte* ptrIrisR = irisR;
                                        score = ProMemory.AssessFocus_Rec(ptrIrisFace, ProMemory.irisConfig.IrisPosBuffer, ref ptrIrisL, ref ptrIrisR);  //质量评估 0均不合格 1一幅合格 2均合格
                                    }
                                }
                            }

                            if (score > 0)
                            {
                                if (score == 1)
                                {
                                    lock (ProMemory.irisConfig.QueueIdentify)
                                    {
                                        ProMemory.irisConfig.QueueIdentify.Push(irisBytesL);
                                    }
                                }
                                else if (score == 2)
                                {
                                    lock (ProMemory.irisConfig.QueueIdentify)
                                    {
                                        ProMemory.irisConfig.QueueIdentify.Push(irisBytesL);
                                        ProMemory.irisConfig.QueueIdentify.Push(irisBytesR);
                                    }
                                }

                                if (ProMemory.irisConfig.Key_Identify == 0)
                                {
                                    ProMemory.irisConfig.Key_Identify = 1;

                                    ProMemory.irisConfig.IdentifyThreadRun = true;
                                    Thread m_IdentifyThread = new Thread(IdentifyTh.GetInstance.Identify);
                                    m_IdentifyThread.Name = "Identify";
                                    m_IdentifyThread.Start();
                                }
                            }

                            Marshal.FreeHGlobal(ptrIrisFace);

                        }
                    }

                    lock (ProMemory.irisConfig.QueueIdentify)
                    {
                        if (ProMemory.irisConfig.QueueIdentify.Count >= 30)
                        {
                            ProMemory.irisConfig.QueueIdentify.Clear(); //手动控制堆栈容量
                        }
                    }
                    
                }//end while

                if (ProMemory.Tag_IdentifyTimer == 1) //拒识时间到,同时会停止匹配线程
                {
                    if (ProMemory.irisConfig.IdentifyAccess == false)
                    {
                        ProMemory.irisConfig.AssessThreadRun = false;
                        ProMemory.irisConfig.EyeFinderThreadRun = false;
                        
                        if (ProMemory.appConfig.AuthConfig.HasTempDtAuthority)
                        {
                            Form1.GetInstance().obtainTemp();
                            Form1.GetInstance().Failure();
                        }
                        else {
                            Form2.GetInstance().Failure();
                        }

                        int time2 = ProMemory.appConfig.ShowTimeFailure;
                        Thread.Sleep(time2);

                        ProMemory.jobRuning = false;
                        ProMemory.cameraController.ContinuousShot_TriggerOff();
                    }
                }

                ProMemory.timer2.Dispose();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, "质量评估线程CatchError:" + ex);
            }

        }

        private void Timer2ThreadMethod(Object State)
        {
            ProMemory.Tag_IdentifyTimer = 1;
        
        }

    }
}