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_ON(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 Printer = target == eWorkPort.Left ? PUB.PrinterL : PUB.PrinterR;
            //var Jobtype = VAR.STR[eVarString.JOB_TYPE];
            //var ByPassMode = Jobtype == "BP";
            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();
            //####################################################
            //###	인터락 확인
            //####################################################
            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}] Print ON operation started");
                PUB.sm.seq.Update(cmdIndex);
                return false;
            }

            //####################################################
            //### 종이감지확인
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                var 용지감지센서 = target == eWorkPort.Left ? eDIName.L_PICK_VAC : eDIName.R_PICK_VAC;
                var 용지감지기능 = target == eWorkPort.Left ? AR.SETTING.Data.Detect_PrintL : AR.SETTING.Data.Detect_PrintR;

                if (OPT_PrinterOff == false)
                {
                    if (용지감지기능 == false)
                    {
                        if (seqTime.TotalMilliseconds < 100) return false;
                        PUB.log.AddAT($"[{target}] Paper detection function OFF(proceed after 3 seconds)");
                    }
                    else
                    {
                        var ioresult = DIO.checkDigitalO(용지감지센서, seqTime, true, 0, false);
                        if (ioresult == eNormalResult.Error)
                        {
                            var errCode = target == eWorkPort.Left ? eECode.NO_PAPER_DETECT_L : eECode.NO_PAPER_DETECT_R;
                            PUB.Result.SetResultMessage(eResult.HARDWARE, errCode, eNextStep.PAUSE, 용지감지센서);
                            return false;
                        }
                        else if (ioresult == eNormalResult.False) return false;
                    }

                }

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

            //####################################################
            //###	Z올리기(slow)
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (iLockZ.IsEmpty() == false) return false;
                if (OPT_BYPASS == false)
                {
                    if (seqTime.TotalMilliseconds < 3000) return false;
                    sPositionData PosZ;
                    if (target == eWorkPort.Left)
                    {
                        PosZ = MOT.GetLZPos(eLZLoc.PICKON);
                    }
                    else
                    {
                        PosZ = MOT.GetRZPos(eRZLoc.PICKON);
                    }
                    PosZ.Position -= 10;
                    if (PosZ.Position < 1) PosZ.Position = 1;
                    PosZ.Speed = 5;
                    if (MOT.CheckMotionPos(seqTime, PosZ, funcName) == false) return false;

                }

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

            //####################################################
            //### 종이감지확인
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                if (OPT_PrinterOff == false)
                {
                    var 용지감지센서 = target == eWorkPort.Left ? eDIName.L_PICK_VAC : eDIName.R_PICK_VAC;
                    var 용지감지기능 = target == eWorkPort.Left ? AR.SETTING.Data.Detect_PrintL : AR.SETTING.Data.Detect_PrintR;

                    if (용지감지기능 == false)
                    {
                        if (seqTime.TotalMilliseconds < 100) return false;
                        PUB.log.AddAT($"[{target}] Paper detection function OFF(proceed after 3 seconds)");
                    }
                    else
                    {
                        var ioresult = DIO.checkDigitalO(용지감지센서, seqTime, true, 0, false);
                        if (ioresult == eNormalResult.Error)
                        {
                            var errCode = target == eWorkPort.Left ? eECode.NO_PAPER_DETECT_L : eECode.NO_PAPER_DETECT_R;
                            PUB.Result.SetResultMessage(eResult.HARDWARE, errCode, eNextStep.PAUSE, 용지감지센서);
                            return false;
                        }
                        else if (ioresult == eNormalResult.False) 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_BYPASS == false)
                {
                    sPositionData PosZ;
                    if (target == eWorkPort.Left)
                    {
                        PosZ = MOT.GetLZPos(eLZLoc.READY);
                    }
                    else
                    {
                        PosZ = MOT.GetRZPos(eRZLoc.READY);
                    }
                    if (MOT.CheckMotionPos(seqTime, PosZ, funcName) == false) return false;
                }

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

            //####################################################
            //###	
            //####################################################
            if (PUB.sm.seq.Get(cmdIndex) == idx++)
            {
                //air off
                if (target == eWorkPort.Left)
                {
                    DIO.SetPrintLAir(false);
                    if (OPT_BYPASS == false)
                        VAR.I32[eVarInt32.LPickOnCount] += 1;
                    PUB.flag.set(eVarBool.FG_PL_ITEMON, true, funcName);
                }
                else
                {
                    DIO.SetPrintRAir(false);
                    if (OPT_BYPASS == false)
                        VAR.I32[eVarInt32.RPickOnCount] += 1;
                    PUB.flag.set(eVarBool.FG_PR_ITEMON, true, funcName);
                }

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

            return true;
        }
    }
}