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

namespace Project
{
    public partial class FMain
    {//홈 맞춰 

        public void _STEP_HOME_QUICK_START(eSMStep step)
        {


            //모든 홈이 되어야 가능하다
            if (PUB.mot.IsInit && PUB.mot.HasHomeSetOff == true)
            {
                PUB.Result.SetResultMessage(eResult.MOTION, eECode.MOT_HSET, eNextStep.ERROR);
                return;
            }

            if (DIO.IsEmergencyOn() == true)
            {
                PUB.Result.SetResultMessage(eResult.HARDWARE, eECode.EMERGENCY, eNextStep.ERROR);
                return;
            }

            if (DIO.isSaftyDoorF() == false || DIO.isSaftyDoorR() == false)
            {
                PUB.Result.SetResultMessage(eResult.HARDWARE, eECode.DOORSAFTY, eNextStep.ERROR);
                return;
            }


            //실린더 상태 복귀
            DIO.SetOutput(eDOName.PRINTL_FWD, false);
            DIO.SetOutput(eDOName.PRINTR_FWD, false);
            DIO.SetOutput(eDOName.L_CYLDN, false);
            DIO.SetOutput(eDOName.R_CYLDN, false);
        }

        CommandBuffer cmds = new CommandBuffer();

        public StepResult _STEP_HOME_QUICK(eSMStep step, TimeSpan stepTime, TimeSpan seqTime)
        {

            byte idx = 1;
            var cmdIndex = eSMStep.HOME_QUICK;
            if (PUB.sm.seq.Get(step) < idx) PUB.sm.seq.Set(cmdIndex, idx);

            //사용자 스텝처리가 아닌경우에만 동작 중지를 검사 한다
            //중단조건 검사는 0번 축에만 동작하게 한다
            if (PUB.flag.get(eVarBool.FG_USERSTEP) == false && CheckPauseCondition(true) == false)
            {
                //가동불가 조건 확인
                if (PUB.Result.ResultCode == eResult.EMERGENCY)
                    PUB.sm.SetNewStep(eSMStep.ERROR);
                else
                    PUB.sm.SetNewStep(eSMStep.PAUSE);
                return StepResult.Wait;
            }

            //동작상태가 아니라면 처리하지 않는다.
            var userStep = PUB.flag.get(eVarBool.FG_USERSTEP);


            //*********************
            //**    하드웨어 조건 확인
            //*********************
            if (CheckHomeProcess_HW_Available(true) == false)
            {
                return StepResult.Error;
            }

            //시작정보
            if (PUB.sm.seq.Get(step) == idx++)
            {
                //210415
                if (DIO.GetPortMotorDir(0) == eMotDir.CW) DIO.SetPortMotor(0, eMotDir.CW, false, "POSRESET");
                if (DIO.GetPortMotorDir(1) == eMotDir.CW) DIO.SetPortMotor(1, eMotDir.CW, false, "POSRESET");
                if (DIO.GetPortMotorDir(2) == eMotDir.CW) DIO.SetPortMotor(2, eMotDir.CW, false, "POSRESET");
                PUB.sm.seq.Update(cmdIndex);
                return StepResult.Wait;
            }


            //피커 Z축을 0으로 이동한다
            if (PUB.sm.seq.Get(step) == idx++)
            {
                var Pos = MOT.GetPZPos(ePZLoc.READY);
                if (MOT.CheckMotionPos(seqTime, Pos, "#7") == false) return 0;
                PUB.sm.seq.Update(cmdIndex);
                return StepResult.Wait;
            }

            //피커 Y축을 안전지대로 이동한다
            if (PUB.sm.seq.Get(step) == idx++)
            {
                var Pos = MOT.GetPXPos(ePXLoc.PICKON);
                if (MOT.CheckMotionPos(seqTime, Pos, "#7", false) == false) return 0;
                PUB.sm.seq.Update(cmdIndex);
                return StepResult.Wait;
            }

            //PRINT-Z축을 0으로이동한다
            if (PUB.sm.seq.Get(step) == idx++)
            {
                var spdinfo = MOT.GetLZPos(eLZLoc.READY);
                MOT.Move(eAxis.PL_UPDN, spdinfo.Position, spdinfo.Speed, spdinfo.Acc, false, true, false);

                spdinfo = MOT.GetRZPos(eRZLoc.READY);
                MOT.Move(eAxis.PR_UPDN, spdinfo.Position, spdinfo.Speed, spdinfo.Acc, false, true, false);

                PUB.sm.seq.Update(cmdIndex);
                return StepResult.Wait;
            }


            //PRINT-Z축 완료대기
            if (PUB.sm.seq.Get(step) == idx++)
            {
                var PosL = MOT.GetLZPos(eLZLoc.READY);
                var PosR = MOT.GetRZPos(eRZLoc.READY);
                if (MOT.CheckMotionPos(seqTime, PosL, "#7", false) == false) return StepResult.Wait;
                if (MOT.CheckMotionPos(seqTime, PosR, "#8", false) == false) return StepResult.Wait;
                PUB.sm.seq.Update(cmdIndex);
                return StepResult.Wait;
            }

            //피커실린더 상승
            if (PUB.sm.seq.Get(step) == idx++)
            {
                DIO.SetOutput(eDOName.L_CYLDN, false);
                DIO.SetOutput(eDOName.R_CYLDN, false);
                PUB.sm.seq.Update(cmdIndex);
                return StepResult.Wait;
            }
            if (PUB.sm.seq.Get(step) == idx++)
            {
                if (AR.SETTING.Data.Enable_PickerCylinder)
                {
                    if (DIO.checkDigitalO(eDIName.L_CYLUP, seqTime, true) != eNormalResult.True) return StepResult.Wait;
                    if (DIO.checkDigitalO(eDIName.R_CYLUP, seqTime, true) != eNormalResult.True) return StepResult.Wait;
                }
                PUB.sm.seq.Update(cmdIndex);
                return StepResult.Wait;
            }


            //프린터부착헤드 후진
            if (PUB.sm.seq.Get(step) == idx++)
            {
                DIO.SetOutput(eDOName.PRINTL_FWD, false);
                DIO.SetOutput(eDOName.PRINTR_FWD, false);
                PUB.sm.seq.Update(cmdIndex);
                return StepResult.Wait;
            }

            //프린터헤드후진 확인
            if (PUB.sm.seq.Get(step) == idx++)
            {
                if (DIO.checkDigitalO(eDIName.L_PICK_BW, seqTime, true) != eNormalResult.True) return StepResult.Wait;
                if (DIO.checkDigitalO(eDIName.R_PICK_BW, seqTime, true) != eNormalResult.True) return StepResult.Wait;
                PUB.sm.seq.Update(cmdIndex);
                return StepResult.Wait;
            }


            //프린터 Y축 이동
            if (PUB.sm.seq.Get(step) == idx++)
            {
                var spdinfo = MOT.GetLMPos(eLMLoc.READY);
                MOT.Move(eAxis.PL_MOVE, spdinfo.Position, spdinfo.Speed, spdinfo.Acc, false, true, false);

                spdinfo = MOT.GetRMPos(eRMLoc.READY);
                MOT.Move(eAxis.PR_MOVE, spdinfo.Position, spdinfo.Speed, spdinfo.Acc, false, true, false);

                PUB.sm.seq.Update(cmdIndex);
                return StepResult.Wait;
            }

            //프린터 Y축 완료대기
            if (PUB.sm.seq.Get(step) == idx++)
            {
                var PosL = MOT.GetLMPos(eLMLoc.READY);
                var PosR = MOT.GetRMPos(eRMLoc.READY);
                if (MOT.CheckMotionPos(seqTime, PosL, "#7") == false) return 0;
                if (MOT.CheckMotionPos(seqTime, PosR, "#8") == false) return 0;
                PUB.sm.seq.Update(cmdIndex);
                return StepResult.Wait;
            }

            //연관플래그 소거
            if (PUB.sm.seq.Get(step) == idx++)
            {
                //진공상태를 초기화 해준다.
                DIO.SetPrintLVac(ePrintVac.off, true);
                DIO.SetPrintRVac(ePrintVac.off, true);
                DIO.SetOutput(eDOName.PRINTL_AIRON, false);
                DIO.SetOutput(eDOName.PRINTR_AIRON, false);

                FlagClear(true);
                PUB.sm.SetNewStep(eSMStep.HOME_CONFIRM);
                return StepResult.Wait;
            }

            PUB.sm.seq.Clear(step);
            PUB.sm.SetNewStep(eSMStep.HOME_CONFIRM);
            return StepResult.Complete;
        }
    }
}
