using irisHelper;
using irisRefactor.FrmService;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading;

namespace irisRefactor.IrisThread
{
    public class IdentifyTh
    {
        private static readonly IdentifyTh identiyfTh = new IdentifyTh();
        private IdentifyTh() { }
        public static IdentifyTh GetInstance
        {
            get => identiyfTh;
        }

        public void Identify()
        {
            LogHelper.WriteInfoLog(MethodBase.GetCurrentMethod().DeclaringType, "Identify Thread Started");
            ProMemory.irisConfig.IdentifyAccess = true;
            ProMemory.irisConfig.IdentifyThreadRun = true;

            // 用于计算操作耗时
            Stopwatch sw = new Stopwatch();

            try
            {
                int iMatchNumber = 0;
                float iHammingDistance = 1;
                int iLeftRight = 0;

                while (ProMemory.identifyConfig.IdentifyTaskId != "" && iMatchNumber == 0 &&
                       !ProMemory.goingToCloseForm && !ProMemory.isSyning)
                {
                    if (ProMemory.irisConfig.QueueIdentify.Count > 0)
                    {
                        LogHelper.WriteDebugLog(MethodBase.GetCurrentMethod().DeclaringType, "QueueIdentify：" + ProMemory.irisConfig.QueueIdentify.Count);
                        lock (ProMemory.irisConfig.QueueIdentify)
                        {
                            if (ProMemory.irisConfig.QueueIdentify.Count > 0)
                            {
                                ProMemory.irisConfig.QualifiedIrisBuffer = ProMemory.irisConfig.QueueIdentify.Pop();
                            }
                        }
                        if (ProMemory.irisConfig.QualifiedIrisBuffer != null)
                        {
                            //加载匹配列表
                            sw.Restart();
                            iMatchNumber = ProMemory.proConfig.CodeListMgr.MatchOneEye(ProMemory.irisConfig.QualifiedIrisBuffer, out iHammingDistance, out iLeftRight);
                            sw.Stop();
                            LogHelper.WriteDebugLog(MethodBase.GetCurrentMethod().DeclaringType, "识别[" + iMatchNumber + "]：" + sw.ElapsedMilliseconds);
                        }
                    }
                }
                
                //跳出循环，条件是时间到、匹配成功或者正在关闭程序      
                ProMemory.identifyConfig.FlagFindingEye = false;

                if (ProMemory.appConfig.AuthConfig.HasTempDtAuthority)
                   ProMemory.tempRegion = Form1.GetInstance().obtainTemp();

                //匹配成功
                if (iMatchNumber > 0)
                {
                    string personId = ProMemory.proConfig.CodeListMgr.GetUserForIndex(iMatchNumber);// personId

                    long timeLast = DateTime.Now.Ticks / 10000 - ProMemory.identifyConfig.IdentifyStart;
                    LogHelper.WriteInfoLog(MethodBase.GetCurrentMethod().DeclaringType, "匹配成功[" + ProMemory.identifyConfig.IdentifyTaskId + "][耗时" + timeLast + "ms]");

                    // 结束识别
                    ProMemory.identifyConfig.EndIdentify();

                    if (ProMemory.formType == "1")
                        Form1.GetInstance().Success(personId);
                    else if (ProMemory.formType == "2")
                        Form2.GetInstance().Success(personId);

                    // 识别成功界面显示一段时间
                    Thread.Sleep(ProMemory.appConfig.ShowTimeSuccess);

                    // 清除识别队列
                    lock(ProMemory.irisConfig.QueueIdentify)
                    {
                        ProMemory.irisConfig.QueueIdentify.Clear();
                    }

                    lock(ProMemory.irisConfig.QueueFace)
                    {
                        ProMemory.irisConfig.QueueFace.Clear();
                    }
                }

                ProMemory.identifyConfig.InitConfig();
                //ProMemory.jobRuning = false;
                ProMemory.cameraController.ContinuousShot_TriggerOff();

            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "虹膜匹配线程CatchError:" + ex);
            }
        }


    }
}