
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 PRINTER_OFF(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 CVMode = VAR.BOOL[eVarBool.Use_Conveyor];

            //option check
            var OPT_PrinterOff = PUB.OPT_PRINTEROFF(target);
            var OPT_CameraOff = PUB.OPT_CAMERA();
            //var OPT_BYPASS = PUB.OPT_BYPASS(target);


            //####################################################
            //###	인터락 확인
            //####################################################
            Boolean MotMoveM, MotMoveZ;
            AR.CInterLock iLockM, iLockZ;
            eAxis axisM = target == eWorkPort.Left ? eAxis.PL_MOVE : eAxis.PR_MOVE;
            eAxis axisZ = target == eWorkPort.Left ? eAxis.PL_UPDN : eAxis.PR_UPDN;
            if (target == eWorkPort.Left)
            {
                MotMoveM = PUB.mot.IsMotion((int)eAxis.PL_MOVE);
                MotMoveZ = PUB.mot.IsMotion((int)eAxis.PL_UPDN);
                iLockM = PUB.iLock[(int)eAxis.PL_MOVE];
                iLockZ = PUB.iLock[(int)eAxis.PL_UPDN];
            }
            else
            {
                MotMoveM = PUB.mot.IsMotion((int)eAxis.PR_MOVE);
                MotMoveZ = PUB.mot.IsMotion((int)eAxis.PR_UPDN);
                iLockM = PUB.iLock[(int)eAxis.PR_MOVE];
                iLockZ = PUB.iLock[(int)eAxis.PR_UPDN];
            }

            if (iLockM.IsEmpty() == false && MotMoveM)
            {
                var locklistX = MOT.GetActiveLockList(axisM, iLockM);
                PUB.mot.MoveStop("ILock(" + string.Join(",", locklistX) + ")", (short)axisM);
            }

            if (iLockZ.IsEmpty() == false && MotMoveZ)
            {
                var locklistZ = MOT.GetActiveLockList(axisZ, iLockZ);
                PUB.mot.MoveStop("ILock(" + string.Join(",", locklistZ) + ")", (short)axisZ);
            }



            //####################################################
            //###	작업시작
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                //PUB.log.Add($"[{target}] 프린트 OFF 작업 시작");
                PUB.sm.seq.Update(cmdIndex);
                return false;
            }

            //####################################################
            //###	피커Y축 위치 확인
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                var PosX = MOT.GetPXPos(ePXLoc.PICKON);
                var OffX = MOT.getPositionOffset(PosX);
                if (target == eWorkPort.Left)
                {
                    if (OffX < -1) return false;    //충돌조건이므로 Y축을 움직이면 안된다
                }
                else
                {
                    if (OffX > 1) return false;//충돌조건이므로 Y축을 움직이면 안된다
                }

                //PUB.log.Add($"[{target}] 프린트 OFF 작업 시작");
                PUB.sm.seq.Update(cmdIndex);
                return false;
            }


            //####################################################
            //###	프린터Z축 위치 확인
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (iLockZ.IsEmpty() == false) return false;
                sPositionData Pos;
                if (target == eWorkPort.Left)
                {
                    Pos = MOT.GetLZPos(eLZLoc.READY);
                }
                else
                {
                    Pos = MOT.GetRZPos(eRZLoc.READY);
                }
                if (MOT.CheckMotionPos(seqTime, Pos, funcName) == false) return false;

                //PUB.log.Add($"[{target}] 프린트 OFF 작업 시작");
                PUB.sm.seq.Update(cmdIndex);
                return false;
            }


            //####################################################
            //###	Y이동
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                sPositionData Pos;

                if (OPT_PrinterOff == false)
                {
                    //릴크기없으면 오류
                    var reelSize = item.VisionData.ReelSize;
                    if (reelSize == eCartSize.None)
                    {
                        PUB.Result.SetResultMessage(eResult.OPERATION, eECode.NOREELSIZE, eNextStep.PAUSE, target);
                        return false;
                    }

                    //부착위치
                    var PrintPutPos = item.VisionData.GetPrintPutPosition();
                    if (PrintPutPos == ePrintPutPos.None)
                    {
                        PUB.Result.SetResultMessage(eResult.OPERATION, eECode.NOPUTPOSITION, eNextStep.PAUSE, target);
                        return false;
                    }

                    if (target == eWorkPort.Left)
                    {
                        //위에 찍을지 아래에 찍을지 위치를 결정한다
                        if (reelSize == eCartSize.Inch13)
                        {
                            if (PrintPutPos == ePrintPutPos.Middle) Pos = MOT.GetLMPos(eLMLoc.PRINTM13);
                            else if (PrintPutPos == ePrintPutPos.Bottom) Pos = MOT.GetLMPos(eLMLoc.PRINTL13);
                            else Pos = MOT.GetLMPos(eLMLoc.PRINTH13);
                        }
                        else
                        {
                            if (PrintPutPos == ePrintPutPos.Middle) Pos = MOT.GetLMPos(eLMLoc.PRINTM07);
                            else if (PrintPutPos == ePrintPutPos.Bottom) Pos = MOT.GetLMPos(eLMLoc.PRINTL07);
                            else Pos = MOT.GetLMPos(eLMLoc.PRINTH07);
                        }
                    }
                    else //Right Move
                    {
                        //위에 찍을지 아래에 찍을지 위치를 결정한다
                        if (reelSize == eCartSize.Inch13)
                        {
                            if (PrintPutPos == ePrintPutPos.Middle) Pos = MOT.GetRMPos(eRMLoc.PRINTM13);
                            else if (PrintPutPos == ePrintPutPos.Bottom) Pos = MOT.GetRMPos(eRMLoc.PRINTL13);
                            else Pos = MOT.GetRMPos(eRMLoc.PRINTH13);
                        }
                        else
                        {
                            if (PrintPutPos == ePrintPutPos.Middle) Pos = MOT.GetRMPos(eRMLoc.PRINTM07);
                            else if (PrintPutPos == ePrintPutPos.Bottom) Pos = MOT.GetRMPos(eRMLoc.PRINTL07);
                            else Pos = MOT.GetRMPos(eRMLoc.PRINTH07);
                        }
                    }

                    if (iLockM.IsEmpty() == false) return false;
                    //if (iLockZ.IsEmpty() == false) return false;

                    //위치오류확인
                    if (Pos.isError == false)
                    {
                        if (MOT.CheckMotionPos(seqTime, Pos, funcName) == false) return false;
                    }
                    else
                    {
                        PUB.Result.SetResultMessage(eResult.OPERATION, eECode.POSITION_ERROR, eNextStep.PAUSE, "PRINTPOS");
                        return false;
                    }
                }



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

            //####################################################
            //###	실린더 전/후진 상태를 전환한다  -- 210207
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (OPT_PrinterOff == false)
                {
                    var Cylinder = item.VisionData.NeedCylinderForward;
                    if (Cylinder == true)
                    {
                        //전진이 필요하다
                        if (target == eWorkPort.Left)
                            DIO.SetOutput(eDOName.PRINTL_FWD, true);
                        else
                            DIO.SetOutput(eDOName.PRINTR_FWD, true);

                    }
                    else
                    {
                        //전진이 필요하지 않다.
                        if (target == eWorkPort.Left)
                            DIO.SetOutput(eDOName.PRINTL_FWD, false);
                        else
                            DIO.SetOutput(eDOName.PRINTR_FWD, false);
                    }

                }


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

            //####################################################
            //###	카트모드에서는 추가 실린더 down 해야한다 230809
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (OPT_PrinterOff == false)
                {
                    if (SETTING.Data.Enable_PickerCylinder)
                    {
                        //전진이 필요하다
                        if (target == eWorkPort.Left)
                            DIO.SetOutput(eDOName.L_CYLDN, true);
                        else
                            DIO.SetOutput(eDOName.R_CYLDN, true);
                    }
                    else
                    {
                        if (target == eWorkPort.Left)
                            DIO.SetOutput(eDOName.L_CYLDN, false);
                        else
                            DIO.SetOutput(eDOName.R_CYLDN, false);
                    }
                }

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

            //####################################################
            //###	카트상태확인
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (OPT_PrinterOff == false)
                {
                    eVarBool fg = target == eWorkPort.Left ? eVarBool.FG_RDY_PORT_PL : eVarBool.FG_RDY_PORT_PR;
                    if (PUB.flag.get(fg) == false) return false;
                }

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

            //####################################################
            //###	실린더상태확인    -- 210207
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (OPT_PrinterOff == false)
                {
                    var Cylinder = item.VisionData.NeedCylinderForward;
                    if (Cylinder == true)
                    {
                        //전진이 필요하다
                        if (target == eWorkPort.Left)
                        {
                            if (DIO.checkDigitalO(eDIName.L_PICK_FW, seqTime, true) != eNormalResult.True)
                                return false;
                        }
                        else
                        {
                            if (DIO.checkDigitalO(eDIName.R_PICK_FW, seqTime, true) != eNormalResult.True)
                                return false;
                        }
                    }
                    else
                    {
                        //전진이 필요하지 않다.
                        if (target == eWorkPort.Left)
                        {
                            if (DIO.checkDigitalO(eDIName.L_PICK_BW, seqTime, true) != eNormalResult.True)
                                return false;
                        }
                        else
                        {
                            if (DIO.checkDigitalO(eDIName.R_PICK_BW, seqTime, true) != eNormalResult.True)
                                return false;
                        }
                    }
                }


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

            //####################################################
            //###	피커 down  실린더상태확인    -- 230809
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (OPT_PrinterOff == false && SETTING.Data.Enable_PickerCylinder)
                {
                    //전진이 필요하다
                    if (target == eWorkPort.Left)
                    {
                        if (DIO.checkDigitalO(eDIName.L_CYLDN, seqTime, true, timeoutcode: eECode.PICKER_LCYL_NODOWN) != eNormalResult.True)
                            return false;
                    }
                    else
                    {
                        if (DIO.checkDigitalO(eDIName.R_CYLDN, seqTime, true, timeoutcode: eECode.PICKER_RCYL_NODOWN) != eNormalResult.True)
                            return false;
                    }
                }

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

            //####################################################
            //###	Z값을 내린다.
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (iLockZ.IsEmpty() == false) return false;

                if (OPT_PrinterOff == false)
                {
                    sPositionData Pos;
                    if (target == eWorkPort.Left)
                        Pos = MOT.GetLZPos(eLZLoc.PICKOFF);
                    else
                        Pos = MOT.GetRZPos(eRZLoc.PICKOFF);
                    if (MOT.CheckMotionPos(seqTime, Pos, funcName) == false) return false;
                }


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

            //####################################################
            //###	진공파기
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (OPT_PrinterOff == false)
                {
                    if (target == eWorkPort.Left)
                    {
                        DIO.SetPrintLVac(ePrintVac.exhaust);
                    }
                    else
                    {
                        DIO.SetPrintRVac(ePrintVac.exhaust);
                    }
                }

                if (target == eWorkPort.Left)
                {
                    VAR.BOOL[eVarBool.LEFT_ITEM_PICKOFF] = true;
                    VAR.TIME[eVarTime.LEFT_ITEM_PICKOFF] = DateTime.Now;
                    VAR.DBL[eVarDBL.LEFT_ITEM_PICKOFF] = 0;
                    VAR.I32[eVarInt32.LEFT_ITEM_COUNT] += 1;
                }
                else
                {
                    VAR.BOOL[eVarBool.RIGT_ITEM_PICKOFF] = true;
                    VAR.TIME[eVarTime.RIGT_ITEM_PICKOFF] = DateTime.Now;
                    VAR.DBL[eVarDBL.RIGT_ITEM_PICKOFF] = 0;
                    VAR.I32[eVarInt32.RIGT_ITEM_COUNT] += 1;
                }
                PUB.log.Add($"[{target}] Placing reel down");


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

            //####################################################
            //###	잠시대기
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (OPT_PrinterOff == false)
                {
                    if (seqTime.TotalMilliseconds < AR.SETTING.Data.PrintVacOffPurgeMS) return false;

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

            //####################################################
            //###	저속으로올린다
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (iLockZ.IsEmpty() == false) return false;
                if (OPT_PrinterOff == false)
                {
                    sPositionData Pos;
                    if (target == eWorkPort.Left)
                        Pos = MOT.GetLZPos(eLZLoc.PICKOFF);
                    else
                        Pos = MOT.GetRZPos(eRZLoc.PICKOFF);
                    Pos.Position -= 30;
                    Pos.Speed = 30;
                    if (MOT.CheckMotionPos(seqTime, Pos, funcName) == false) return false;
                }

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

            //####################################################
            //###	카트모드에서는 추가 실린더 down 해야한다 230809
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                //if (ByPassMode == false)
                //{
                // if (CVMode == false)
                //{
                //전진이 필요하다
                if (target == eWorkPort.Left)
                    DIO.SetOutput(eDOName.L_CYLDN, false);
                else
                    DIO.SetOutput(eDOName.R_CYLDN, false);
                //}
                // }

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

            //####################################################
            //###	Z값 대기위치로
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (iLockZ.IsEmpty() == false) return false;
                if (OPT_PrinterOff == false)
                {
                    sPositionData Pos;
                    if (target == eWorkPort.Left)
                        Pos = MOT.GetLZPos(eLZLoc.READY);
                    else
                        Pos = MOT.GetRZPos(eRZLoc.READY);
                    if (MOT.CheckMotionPos(seqTime, Pos, funcName) == false) return false;
                }

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

            //####################################################
            //###	AIR해제
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (target == eWorkPort.Left)
                    DIO.SetPrintLVac(ePrintVac.off);
                else
                    DIO.SetPrintRVac(ePrintVac.off);
                PUB.sm.seq.Update(cmdIndex);
                return false;
            }

            //####################################################
            //###	실린더 복귀 확인    -- 230809
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (SETTING.Data.Enable_PickerCylinder)
                {
                    //상승이 필요하다
                    if (target == eWorkPort.Left)
                    {
                        if (DIO.checkDigitalO(eDIName.L_CYLUP, seqTime, true, timeoutcode: eECode.PICKER_LCYL_NOUP) != eNormalResult.True)
                            return false;
                    }
                    else
                    {
                        if (DIO.checkDigitalO(eDIName.R_CYLUP, seqTime, true, timeoutcode: eECode.PICKER_RCYL_NOUP) != eNormalResult.True)
                            return false;
                    }

                }


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


            //####################################################
            //###	
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (OPT_PrinterOff == false)
                {
                    if (target == eWorkPort.Left)
                    {
                        if (DIO.checkDigitalO(eDIName.L_PICK_VAC, seqTime, false) != eNormalResult.True) return false;
                        VAR.I32[eVarInt32.LPickOfCount] += 1;
                    }
                    else
                    {
                        if (DIO.checkDigitalO(eDIName.R_PICK_VAC, seqTime, false) != eNormalResult.True) return false;
                        VAR.I32[eVarInt32.RPickOfCount] += 1;
                    }
                    //PUB.log.AddAT($"용지 떨어짐 확인 필요");		
                }

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

            //####################################################
            //###	
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (target == eWorkPort.Left)
                {
                    PUB.flag.set(eVarBool.FG_PL_ITEMON, false, funcName);
                }
                else
                {
                    PUB.flag.set(eVarBool.FG_PR_ITEMON, false, funcName);
                }

                //프린터 사용여부 확인
                if (OPT_PrinterOff == false)
                {
                    item.PrintAttach = true;
                    item.Attachtime = DateTime.Now;
                }

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



            return true;
        }
    }
}