using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Project.StateMachine;
using COMM;
using AR;

namespace Project
{
    public partial class fMain
    {
        public Boolean _SM_RUN_RESET(bool isFirst, TimeSpan stepTime)
        {
            if (runStepisFirst)
            {
                PUB.Speak(Lang.장비상태초기화);

                //충전기검색시퀀스 OFF
                //PUB.flag.set(EFlag.FLAG_GO_CHAGER_TEMP, false);
            }

            var idx = 1;
            if (PUB.sm.RunStepSeq == idx++)
            {
                //충전중이라면 충전off코드를 넣는다
                PUB.AGV.AGVCharge(PUB.setting.ChargerID, false,PUB.setting.ChargetWaitSec);
                LastCommandTime = DateTime.Now;
                PUB.AddEEDB($"초기화");
                PUB.sm.UpdateRunStepSeq();
                return false;
            }
            else if (PUB.sm.RunStepSeq == idx++)
            {
                UpdateProgressStatus(stepTime.TotalSeconds, 5, "SYNC : 수동 모드 확인 중");
                if (VAR.BOOL[eVarBool.FLAG_AUTORUN] == false)
                    PUB.sm.UpdateRunStepSeq();
                else
                {
                    //일정시간동안 명령을 재전송한다
                    var tsCmd = DateTime.Now - LastCommandTime;
                    if (tsCmd.TotalMilliseconds >= 1999)
                    {
                        VAR.BOOL[eVarBool.FLAG_AUTORUN] = false;
                        LastCommandTime = DateTime.Now;
                    }
                }
                return false;
            }
            else if (PUB.sm.RunStepSeq == idx++)
            {
                //모션을 멈춥니다
                //PUB.PLC.Move(Device.PLC.Rundirection.Stop,"RESET");
                PUB.AGV.AGVMoveStop("_sm_run_reset");
                LastCommandTime = DateTime.Now;
                PUB.sm.UpdateRunStepSeq();
                return false;
            }
            else if (PUB.sm.RunStepSeq == idx++)
            {
                //모션이 멈출때까지 기다린다
                if (PUB.AGV.system1.agv_stop == false)
                {
                    PUB.sm.UpdateRunStepSeq();
                }
                else
                {
                    //모션이 멈추지 않는다면 멈춤코드를 계속 넣는다
                    var tsCmd = DateTime.Now - LastCommandTime;
                    if (tsCmd.TotalMilliseconds >= 1999)
                    {
                        //PUB.PLC.Move(Device.PLC.Rundirection.Stop,"RESET(Re)");
                        PUB.AGV.AGVMoveStop("_sm_run_reset");
                        LastCommandTime = DateTime.Now;
                    }
                }
                return false;
            }
            else if (PUB.sm.RunStepSeq == idx++)
            {
                //대상위치 초기화
                PUB.Result.TargetPos = ePosition.NONE;
                PUB.Result.CurrentPos = ePosition.NONE;
                PUB.Result.CurrentPosCW = "0";
                PUB.sm.UpdateRunStepSeq();
                return false;
            }
            else if (PUB.sm.RunStepSeq == idx++)
            {
                //자동 스탑 플래그 해제
                //VAR.BOOL[eVarBool.FLAG_NEXTSTOP_ALIGN] = false;//"reset #6");
                VAR.BOOL[eVarBool.FLAG_NEXTSTOP_ALIGN] = false;

                LastCommandTime = DateTime.Now;
                PUB.sm.UpdateRunStepSeq();
                return false;
            }
            else if (PUB.sm.RunStepSeq == idx++)
            {
                UpdateProgressStatus(stepTime.TotalSeconds, 5, "자동멈춤해제(ALIGN)");
                if (VAR.BOOL[eVarBool.FLAG_NEXTSTOP_ALIGN] == false)
                    PUB.sm.UpdateRunStepSeq();
                else
                {
                    //일정시간동안 명령을 재전송한다
                    var tsCmd = DateTime.Now - LastCommandTime;
                    if (tsCmd.TotalMilliseconds >= 1999)
                    {
                        VAR.BOOL[eVarBool.FLAG_NEXTSTOP_ALIGN] = false;// "reset #7");
                        //VAR.BOOL[eVarBool.FLAG_NEXTSTOP_ALIGN] = false;//
                        LastCommandTime = DateTime.Now;
                    }
                }
                return false;
            }
            else if (PUB.sm.RunStepSeq == idx++)
            {
                //자동 스탑 플래그 해제
                //VAR.BOOL[eVarBool.FLAG_NEXTSTOP_MARK] = false;// "reset #8");

                LastCommandTime = DateTime.Now;
                PUB.sm.UpdateRunStepSeq();
                return false;
            }
            else if (PUB.sm.RunStepSeq == idx++)
            {
                //설정모드가 되어있다면 해제 해준다
                VAR.BOOL[eVarBool.FLAG_SETUP] = false;
                VAR.BOOL[eVarBool.ITEMON] = false;
                VAR.BOOL[eVarBool.WAIT_COVER_DOWN] = false;
                VAR.BOOL[eVarBool.WAIT_COVER_UP] = false;

                LastCommandTime = DateTime.Now;
                PUB.sm.UpdateRunStepSeq();
                return false;
            }
            else if (PUB.sm.RunStepSeq == idx++)
            {
                UpdateProgressStatus(stepTime.TotalSeconds, 5, "설정모드해제(MARK)");
                
                if (VAR.BOOL[eVarBool.FLAG_SETUP] == false)
                    PUB.sm.UpdateRunStepSeq();
                else
                {
                    //일정시간동안 명령을 재전송한다
                    var tsCmd = DateTime.Now - LastCommandTime;
                    if (tsCmd.TotalMilliseconds >= 1999)
                    {
                        VAR.BOOL[eVarBool.FLAG_SETUP] = false;
                        LastCommandTime = DateTime.Now;
                    }
                }
                return false;
            }
            return true;
        }
    }
}
