using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using AR;

namespace Project
{
    public partial class FMain
    {
        public Boolean QR_VALIDATION(eWorkPort target, eSMStep cmdIndex)
        {
            UInt16 idx = 1;
            var mv = PUB.Result.vModel;
            var mc = PUB.Result.mModel;
            var funcName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            var seqTime = PUB.sm.seq.GetTime(cmdIndex);
            var msgType = (Class.eStatusMesage)target;
            var item = target == eWorkPort.Left ? PUB.Result.ItemDataL : PUB.Result.ItemDataR;
            var WS = target == eWorkPort.Left ? PUB.wsL : PUB.wsR;

            //####################################################
            //###	작업시작
            //####################################################
            var CIDX = PUB.sm.seq.Get(cmdIndex);
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                //작업 플래그 설정
                if (target == eWorkPort.Left)
                {
                    VAR.BOOL[eVarBool.VisionL_Retry] = false;
                    PUB.flag.set(eVarBool.FG_END_VISIONL, false, funcName);
                    PUB.flag.set(eVarBool.FG_PRC_VISIONL, true, funcName);
                    //PUB.iLockCVL.set((int)eILockCV.VISION, true, funcName);
                }
                else
                {
                    VAR.BOOL[eVarBool.VisionR_Retry] = false;
                    PUB.flag.set(eVarBool.FG_END_VISIONR, false, funcName);
                    PUB.flag.set(eVarBool.FG_PRC_VISIONR, true, funcName);
                    //PUB.iLockCVR.set((int)eILockCV.VISION, true, funcName);
                }

                PUB.sm.seq.Update(cmdIndex);
                return false;
            }

            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                //카메라 트리거 전송
                var sendok = WS_Send(target, WS, item.guid, "TRIG", item.VisionData.PrintQRData);
                PUB.log.Add($"Barcode trigger transmission ({target}) = {sendok}");
                if(sendok==false) //230512 전송실패시 오류로 한다
                {
                    PUB.Result.SetResultMessage(eResult.HARDWARE, eECode.VISION_TRIGERROR, eNextStep.PAUSE);
                    return false;
                }
                PUB.sm.seq.Update(cmdIndex);
                return false;
            }

            //####################################################
            //### 수신된 바코드 값을 기다린다.
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                //타임아웃시간을 넘어서면 오류처리한다
                if (seqTime.TotalMilliseconds >= AR.SETTING.Data.Timeout_VisionProcessU)
                {
                    //
                    if (target == eWorkPort.Left)
                    {
                        if (VAR.BOOL[eVarBool.VisionL_Retry] == false)
                        {
                            PUB.log.AddAT("Vision (L) failed once, retrying");
                            VAR.BOOL[eVarBool.VisionL_Retry] = true;
                            PUB.sm.seq.Update(cmdIndex, -1);
                            return false;
                        }

                        if (item.VisionData.RID2.isEmpty() == false && item.VisionData.MatchValidation == false)
                        {
                            PUB.Result.SetResultMessage(eResult.OPERATION, eECode.QRDATAMISSMATCHL, eNextStep.PAUSE, target, item.VisionData.RID, item.VisionData.RID2);
                        }
                        else
                        {
                            PUB.Result.SetResultMessage(eResult.OPERATION, eECode.VISION_NORECV, eNextStep.PAUSE, target);
                        }
                    }
                    else
                    {
                        if (VAR.BOOL[eVarBool.VisionR_Retry] == false)
                        {
                            PUB.log.AddAT("Vision (R) failed once, retrying");
                            VAR.BOOL[eVarBool.VisionR_Retry] = true;
                            PUB.sm.seq.Update(cmdIndex, -1);
                            return false;
                        }


                        if (item.VisionData.RID2.isEmpty() == false && item.VisionData.MatchValidation == false)
                        {
                            PUB.Result.SetResultMessage(eResult.OPERATION, eECode.QRDATAMISSMATCHR, eNextStep.PAUSE, target, item.VisionData.RID, item.VisionData.RID2);
                        }
                        else
                        {
                            PUB.Result.SetResultMessage(eResult.OPERATION, eECode.VISION_NORECV, eNextStep.PAUSE, target);
                        }
                    }


                    PUB.sm.seq.Update(cmdIndex, -1);    // 재시작시 트리거 전송할 수 있게 위로 옮김
                    return false;
                }
                else
                {
                    //타임아웃되지 않은 조건

                    if (target == eWorkPort.Left)
                    {
                        if (PUB.flag.get(eVarBool.FG_END_VISIONL))    //종료신호가 설정되어있다면 완료된 경우다
                        {
                            PUB.log.AddI($"{target} Completed processing due to vision end signal");
                            item.PrintQRValid = true;
                        }
                        else if (PUB.flag.get(eVarBool.FG_PRC_VISIONL) == false) //사용자가 취소했다면 넘어간다
                        {
                            PUB.log.AddAT($"{target} Skip QR verification due to user cancellation");
                            item.PrintQRValid = false;
                        }
                        else return false; //아직 완료전이므로 리턴한다
                    }
                    else
                    {
                        if (PUB.flag.get(eVarBool.FG_END_VISIONR))
                        {
                            PUB.log.AddI($"{target} Completed processing due to vision end signal");
                            item.PrintQRValid = true;
                        }
                        else if (PUB.flag.get(eVarBool.FG_PRC_VISIONR) == false) //사용자가 취소했다면 넘어간다
                        {
                            PUB.log.AddAT($"{target} Skip QR verification due to user cancellation");
                            item.PrintQRValid = false;
                        }
                        else return false; //아직 완료전이므로 리턴한다
                    }
                }

                PUB.sm.seq.Update(cmdIndex);
                return false;
            }


            //####################################################
            //###	
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                PUB.log.Add($"{target} Vision verification completed (Result: {item.PrintQRValid})");

                if (target == eWorkPort.Left)
                    PUB.flag.set(eVarBool.FG_PRC_VISIONL, false, funcName);
                else
                    PUB.flag.set(eVarBool.FG_PRC_VISIONR, false, funcName);

                PUB.sm.seq.Update(cmdIndex);
                return false;
            }


            return true;
        }
    }
}