using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AR;
namespace Project
{
    public partial class FMain
    {
        DateTime tm500minute = DateTime.Now.AddDays(-1);
        DateTime tm250minute = DateTime.Now.AddDays(-1);
        DateTime tm1minute = DateTime.Now.AddDays(-1);
        DateTime tm5minute = DateTime.Now;
        Boolean displayOn = false;
        Boolean needShowSummary = false;


        private void _DisplayTimer(object sender, EventArgs e)
        {
            if (displayOn == false) displayOn = true;
            else
            {
                PUB.log.AddAT("Display Timer Overlab");// Console.WriteLine("display overlab");
                return;
            }

            //lbOnline.Text = COMM.SETTING.Data.OnlineMode ? "ON-LINE" : "OFF-LINE";
            //lbOnline.ForeColor = COMM.SETTING.Data.OnlineMode ? Color.Black : Color.Red;
            //toolStripLabel1.Visible =  Pub.Result.DryRun;
            //if(Pub.Result.DryRun)
            //{
            //    if (toolStripLabel1.ForeColor == Color.Red) toolStripLabel1.ForeColor = Color.Black;
            //    else toolStripLabel1.ForeColor = Color.Red;
            //}
            try
            {
                UpdateDisplayTimerData();
            }
            catch
            {

            }




            //작업수량 업데이트
            UpdateJobCount();

            #region "250ms time 루틴"
            var ts250 = DateTime.Now - tm250minute;
            if (ts250.TotalMilliseconds >= 250)
            {
                _Display_Interval_250ms();
                tm250minute = DateTime.Now;
            }
            #endregion

            #region "500ms time 루틴"
            var ts500 = DateTime.Now - tm500minute;
            if (ts500.TotalMilliseconds >= 500)
            {
                _Display_Interval_500ms();
                tm500minute = DateTime.Now;
            }
            #endregion

            #region retgion"1분 time 루틴"
            var ts = DateTime.Now - tm1minute;
            if (ts.TotalMinutes >= 1)
            {
                //리셋카운트
                _Display_Interval_1min();
                tm1minute = DateTime.Now;
            }
            #endregion

            #region retgion"5분 time 루틴"
            ts = DateTime.Now - tm5minute;
            if (ts.TotalMinutes >= 5)
            {
                //남은디스크확인
                _Display_Interval_5min();
                tm5minute = DateTime.Now;
            }
            #endregion

            displayOn = false;
        }



        void UpdateDisplayTimerData()
        {
            if (PUB.sm.Step > eSMStep.INIT &&
                panTopMenu.Enabled == false) panTopMenu.Enabled = true;

            //쓰레드로인해서 메인에서 진행하게한다. SPS는 메인쓰레드에서 진행 됨
            //팝을 제거 혹은 표시하는 기능
            if (PUB.popup.needShow) PUB.popup.showMessage();
            else if (PUB.popup.needClose) PUB.popup.Visible = false; // .setVision(false)();



            ////업로드화면을 표시해야하느 ㄴ경우
            if (needShowSummary)
            {
                ShowSummary();
                needShowSummary = false;
            }

            //시스템 정보를 메인 UI에 표시한다
            UpdateLoaderDisplay();


        }
        void UpdateJobCount()
        {

        }



        void UpdateLoaderDisplay()
        {
            hmi1.L_PICK_FW = DIO.GetIOInput(eDIName.L_PICK_FW);  //피커의 실린더 전후진 상태
            hmi1.L_PICK_BW = DIO.GetIOInput(eDIName.L_PICK_BW);  //피커의 실린더 전후진 상태
            hmi1.R_PICK_FW = DIO.GetIOInput(eDIName.R_PICK_FW);  //피커의 실린더 전후진 상태
            hmi1.R_PICK_BW = DIO.GetIOInput(eDIName.R_PICK_BW);  //피커의 실린더 전후진 상태

            hmi1.PrintLPICK = DIO.GetIOInput(eDIName.L_PICK_VAC);
            hmi1.PrintRPICK = DIO.GetIOInput(eDIName.R_PICK_VAC);

            hmi1.arPortLItemOn = PUB.flag.get(eVarBool.FG_PORTL_ITEMON);
            hmi1.arPortRItemOn = PUB.flag.get(eVarBool.FG_PORTR_ITEMON);

            hmi1.arPLItemON = PUB.flag.get(eVarBool.FG_PL_ITEMON);
            hmi1.arPRItemON = PUB.flag.get(eVarBool.FG_PR_ITEMON);

            hmi1.arFGVision0RDY = PUB.flag.get(eVarBool.FG_PRC_VISIONL);
            hmi1.arFGVision1RDY = false;// PUB.flag.get(eVarBool.RDY_VISION1);
            hmi1.arFGVision2RDY = PUB.flag.get(eVarBool.FG_PRC_VISIONR);

            hmi1.arFGVision0END = PUB.flag.get(eVarBool.FG_END_VISIONL);
            hmi1.arFGVision1END = false;// PUB.flag.get(eVarBool.END_VISION1);
            hmi1.arFGVision2END = PUB.flag.get(eVarBool.FG_END_VISIONR);

            hmi1.arFGPrinter0RDY = PUB.flag.get(eVarBool.FG_RUN_PRINTL);
            hmi1.arFGPrinter1RDY = PUB.flag.get(eVarBool.FG_RUN_PRINTR);

            hmi1.arFGPrinter0END = PUB.flag.get(eVarBool.FG_OK_PRINTL);
            hmi1.arFGPrinter1END = PUB.flag.get(eVarBool.FG_OK_PRINTR);

            hmi1.arMagnet0 = DIO.GetIOOutput(eDOName.PORTL_MAGNET);
            hmi1.arMagnet1 = DIO.GetIOOutput(eDOName.PORTC_MAGNET);
            hmi1.arMagnet2 = DIO.GetIOOutput(eDOName.PORTR_MAGNET);

            hmi1.arPickerSafeZone = DIO.GetIOInput(eDIName.PICKER_SAFE);

            hmi1.arMotPosNamePX = MOT.GetPosName(eAxis.PX_PICK);
            hmi1.arMotPosNamePZ = MOT.GetPosName(eAxis.PZ_PICK);
            hmi1.arMotPosNameLM = MOT.GetPosName(eAxis.PL_MOVE);
            hmi1.arMotPosNameLZ = MOT.GetPosName(eAxis.PL_UPDN);
            hmi1.arMotPosNameRM = MOT.GetPosName(eAxis.PR_MOVE);
            hmi1.arMotPosNameRZ = MOT.GetPosName(eAxis.PR_UPDN);

            hmi1.arMotILockPKX = PUB.iLock[(int)eAxis.PX_PICK].IsEmpty() == false;
            hmi1.arMotILockPKZ = PUB.iLock[(int)eAxis.PZ_PICK].IsEmpty() == false;
            hmi1.arMotILockPLM = PUB.iLock[(int)eAxis.PL_MOVE].IsEmpty() == false;
            hmi1.arMotILockPLZ = PUB.iLock[(int)eAxis.PL_UPDN].IsEmpty() == false;
            hmi1.arMotILockPRM = PUB.iLock[(int)eAxis.PR_MOVE].IsEmpty() == false;
            hmi1.arMotILockPRZ = PUB.iLock[(int)eAxis.PR_UPDN].IsEmpty() == false;

            hmi1.arMotILockPRL = PUB.iLockPRL.IsEmpty() == false;
            hmi1.arMotILockPRR = PUB.iLockPRR.IsEmpty() == false;
            hmi1.arMotILockVS0 = PUB.iLockVS0.IsEmpty() == false;
            hmi1.arMotILockVS1 = PUB.iLockVS1.IsEmpty() == false;
            hmi1.arMotILockVS2 = PUB.iLockVS2.IsEmpty() == false;
            hmi1.arMotILockCVL = PUB.iLockCVL.IsEmpty() == false;
            hmi1.arMotILockCVR = PUB.iLockCVR.IsEmpty() == false;

            hmi1.arIsRunning = PUB.sm.isRunning;
            hmi1.arUnloaderSeq = PUB.Result.UnloaderSeq;

            //연결상태            
            hmi1.arConn_MOT = PUB.mot.IsInit;
            hmi1.arConn_DIO = PUB.dio.IsInit;
            hmi1.arAIRDetect = DIO.GetIOInput(eDIName.AIR_DETECT);
            hmi1.arJobEND = PUB.flag.get(eVarBool.FG_JOB_END);
            hmi1.arConn_REM = PUB.remocon.IsOpen();

            hmi1.arDI_Emergency = DIO.GetIOInput(eDIName.BUT_EMGF) == true;
            hmi1.arDI_SaftyOk = DIO.isSaftyDoorF();

            hmi1.arVisionProcessL = PUB.flag.get(eVarBool.FG_PRC_VISIONL);
            hmi1.arVisionProcessC = false;// PUB.flag.get(eVarBool.RDY_VISION1);
            hmi1.arVisionProcessR = PUB.flag.get(eVarBool.FG_PRC_VISIONR);

            try
            {
                hmi1.arVision_RID = PUB.Result.ItemDataC.VisionData.RID;    //200925
                hmi1.arVision_SID = PUB.Result.ItemDataC.VisionData.SID; //200925
                hmi1.arVar_Picker[0].PortPos = PUB.Result.ItemDataL.PortPos; //200925
            }
            catch { }


            for (short i = 0; i < 3; i++)
            {
                if (PUB.mot != null && PUB.mot.IsInit)
                {
                    hmi1.arMOT_HSet[i] = PUB.mot.IsHomeSet(i);
                    hmi1.arMOT_Alm[i] = PUB.mot.IsServAlarm(i);
                    hmi1.arMOT_LimUp[i] = PUB.mot.IsLimitP(i);
                    hmi1.arMOT_LimDn[i] = PUB.mot.IsLimitN(i);
                    hmi1.arMOT_Origin[i] = PUB.mot.IsOrg(i);
                    hmi1.arMOT_SVOn[i] = PUB.mot.IsServOn(i);
                }
                else
                {
                    hmi1.arMOT_HSet[i] = false;
                    hmi1.arMOT_Alm[i] = false;
                    hmi1.arMOT_LimUp[i] = false;
                    hmi1.arMOT_LimDn[i] = false;
                    hmi1.arMOT_Origin[i] = false;
                    hmi1.arMOT_SVOn[i] = false;
                }
            }

            //포트 리밋센서
            hmi1.arVar_Port[0].LimitLower = DIO.GetIOInput(eDIName.PORTL_LIM_DN);        //front-left
            hmi1.arVar_Port[0].LimitUpper = DIO.GetIOInput(eDIName.PORTL_LIM_UP);
            hmi1.arVar_Port[0].DetectUp = DIO.GetIOInput(eDIName.PORTL_DET_UP);
            hmi1.arVar_Port[0].SaftyErr = !DIO.isSaftyDoorF(0, false);
            hmi1.arVar_Port[0].reelCount = SETTING.Counter.CountP0;
            hmi1.arVar_Port[0].CartSize = (int)DIO.getCartSize(0);

            hmi1.arVar_Port[1].LimitLower = DIO.GetIOInput(eDIName.PORTC_LIM_DN);        //front-right
            hmi1.arVar_Port[1].LimitUpper = DIO.GetIOInput(eDIName.PORTC_LIM_UP);
            hmi1.arVar_Port[1].DetectUp = DIO.GetIOInput(eDIName.PORTC_DET_UP);
            hmi1.arVar_Port[1].SaftyErr = !DIO.isSaftyDoorF(1, false);
            hmi1.arVar_Port[1].reelCount = SETTING.Counter.CountP1;
            hmi1.arVar_Port[1].CartSize = (int)DIO.getCartSize(1);

            hmi1.arVar_Port[2].LimitLower = DIO.GetIOInput(eDIName.PORTR_LIM_DN);        //front-left
            hmi1.arVar_Port[2].LimitUpper = DIO.GetIOInput(eDIName.PORTR_LIM_UP);
            hmi1.arVar_Port[2].DetectUp = DIO.GetIOInput(eDIName.PORTR_DET_UP);
            hmi1.arVar_Port[2].SaftyErr = !DIO.isSaftyDoorF(2, false);
            hmi1.arVar_Port[2].reelCount = SETTING.Counter.CountP2;
            hmi1.arVar_Port[2].CartSize = (int)DIO.getCartSize(2);

            hmi1.arCountPrint0 = SETTING.Counter.CountPrintL;
            hmi1.arCountPrint1 = SETTING.Counter.CountPrintR;

            if (VAR.BOOL[eVarBool.Use_Conveyor])
            {
                hmi1.arVar_Port[0].reelCount = VAR.I32[eVarInt32.LEFT_ITEM_COUNT];
                hmi1.arVar_Port[2].reelCount = VAR.I32[eVarInt32.RIGT_ITEM_COUNT];
            }

            hmi1.arCountV1 = SETTING.Counter.CountV1;
            hmi1.arCountV0 = SETTING.Counter.CountV0;
            hmi1.arCountV2 = SETTING.Counter.CountV2;

            //피커 백큠감지 상태(Front)
            hmi1.arVar_Picker[0].VacOutput[0] = DIO.GetIOOutput(eDOName.PICK_VAC1);
            hmi1.arVar_Picker[0].VacOutput[1] = DIO.GetIOOutput(eDOName.PICK_VAC2);
            hmi1.arVar_Picker[0].VacOutput[2] = DIO.GetIOOutput(eDOName.PICK_VAC3);
            hmi1.arVar_Picker[0].VacOutput[3] = DIO.GetIOOutput(eDOName.PICK_VAC4);
            hmi1.arVar_Picker[0].ItemOn = PUB.flag.get(eVarBool.FG_PK_ITEMON);
        }


        Color StatusBackColor = Color.FromArgb(150, 150, 150);


        /// <summary>
        /// 상태표시라벨의 진행값을 변경 합니다
        /// </summary>
        /// <param name="idx"></param>
        /// <param name="value"></param>
        /// <param name="max"></param>
        /// <param name="title"></param>
        void SetStatusProgress(double value, string title, double max = 0, Color? textColor = null, Color? shadowColor = null)
        {
            arCtl.arLabel lbl = lbMsg;//idx == 0 ? lbMsgL : (idx == 1 ? lbMsgC : (idx == 2 ? lbMsgR : lbMsg));

            if (lbl.ProgressEnable == false) lbl.ProgressEnable = true;
            if (lbl.ProgressValue != value) lbl.ProgressValue = (float)value;
            if (lbl.Text != title) lbl.Text = title;

            if (max != 0)
            {
                if (lbl.ProgressMax != max) lbl.ProgressMax = (float)max;
            }
            if (textColor != null)
            {
                if (lbl.ProgressForeColor != textColor) lbl.ProgressForeColor = (Color)textColor;
            }
            if (shadowColor != null)
            {
                if (lbl.ShadowColor != shadowColor) lbl.ShadowColor = (Color)shadowColor;
            }
        }

        ///// <summary>
        ///// 상태표시라벨의 메세지를 변경 합니다
        ///// </summary>
        ///// <param name="idx"></param>
        ///// <param name="dispmsg"></param>
        ///// <param name="bColor"></param>
        ///// <param name="bColor2"></param>
        ///// <param name="fcolor"></param>
        ///// <param name="shadow"></param>
        ///// <param name="blank"></param>
        //void SetStatusMessage(string dispmsg, Color bColor, Color bColor2, Color fcolor, Color? shadow = null, Boolean blank = false)
        //{
        //	arCtl.arLabel lbl = lbMsg;//idx == 0 ? lbMsgL : (idx == 1 ? lbMsgC : (idx == 2 ? lbMsgR : lbMsg));
        //	if (shadow == null) shadow = Color.Transparent;
        //	if (lbl.ProgressValue != 0 || dispmsg != lbl.Text || lbl.ForeColor != fcolor || lbl.ShadowColor != (Color)shadow || (lbl.BackColor != bColor && lbl.BackColor2 != bColor))
        //	{
        //		lbl.ProgressValue = 0;
        //		lbl.BackColor = bColor;
        //		lbl.BackColor2 = bColor2;
        //		lbl.ForeColor = fcolor;
        //		lbl.ShadowColor = (Color)shadow;
        //		lbl.Text = dispmsg;
        //		if (blank) lbl.Tag = "BLINK";
        //		else lbl.Tag = null;
        //	}
        //}

        void SetStatusMessage(string dispmsg, Color fcolor, Color backcolor, Color? backColor2 = null, Color? shadow = null, Boolean blank = false)
        {
            arCtl.arLabel lbl = lbMsg;//idx == 0 ? lbMsgL : (idx == 1 ? lbMsgC : (idx == 2 ? lbMsgR : lbMsg));

            if (shadow == null) shadow = Color.Transparent;
            if (backColor2 == null) backColor2 = backcolor;

            var bg1ok = lbl.BackColor == backcolor && lbl.BackColor2 == backColor2;
            var bg2ok = lbl.BackColor2 == backcolor && lbl.BackColor == backColor2;


            if (lbl.ProgressValue != 0 || dispmsg != lbl.Text ||
                lbl.ForeColor != fcolor || lbl.ShadowColor != (Color)shadow || (bg1ok == false && bg2ok == false))
            {
                lbl.ProgressValue = 0;
                lbl.BackColor = backcolor;
                lbl.BackColor2 = (Color)backColor2;

                lbl.ForeColor = fcolor;
                lbl.ShadowColor = (Color)shadow;
                lbl.Text = dispmsg;
                if (blank) lbl.Tag = "BLINK";
                else lbl.Tag = null;
            }
            else if (lbl.ProgressEnable)
            {
                lbl.ProgressEnable = false;
            }
        }


    }
}
