using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using AR;
using arDev;

namespace Project
{
    public partial class FMain
    {

        void Lock_ValueChanged(object sender, AR.InterfaceValueEventArgs e)
        {

            //인터락플래그 변경 이벤트
            var iL = sender as AR.CInterLock;
            var tagstr = (iL.Tag == null ? string.Empty : iL.Tag.ToString());
            var fgstr = $"[{e.ArrIDX}]";

            if (tagstr == "PKX") fgstr = ((eILock)e.ArrIDX).ToString();
            else if (tagstr == "PKZ") fgstr = ((eILock)e.ArrIDX).ToString();
            else if (tagstr == "PKT") fgstr = ((eILock)e.ArrIDX).ToString();
            else if (tagstr == "PLM") fgstr = ((eILock)e.ArrIDX).ToString();
            else if (tagstr == "PLZ") fgstr = ((eILock)e.ArrIDX).ToString();
            else if (tagstr == "PRM") fgstr = ((eILock)e.ArrIDX).ToString();
            else if (tagstr == "PRZ") fgstr = ((eILock)e.ArrIDX).ToString();
            else if (tagstr == "PRL") fgstr = ((eILockPRL)e.ArrIDX).ToString();
            else if (tagstr == "PRR") fgstr = ((eILockPRR)e.ArrIDX).ToString();
            else if (tagstr == "VS0") fgstr = ((eILockVS0)e.ArrIDX).ToString();
            else if (tagstr == "VS1") fgstr = ((eILockVS1)e.ArrIDX).ToString();
            else if (tagstr == "VS2") fgstr = ((eILockVS2)e.ArrIDX).ToString();

            else if (tagstr == "CVL") fgstr = ((eILockCV)e.ArrIDX).ToString();
            else if (tagstr == "CVR") fgstr = ((eILockCV)e.ArrIDX).ToString();

            //else fgstr = "?";

            //최초생성시에는 값을 표시한다
            if (PUB.sm.Step >= eSMStep.IDLE && PUB.sm.Step != eSMStep.HOME_FULL)
            {
                if (e.NewOn)
                {
                    if (tagstr == "PKX" && PUB.mot.IsMotion(0) == true)
                    {
                        Console.WriteLine("ㅠㅠ");
                    }
                    if (AR.SETTING.Data.Log_ILock)
                        PUB.log.Add(string.Format("[IL-ON:{2}] {0}:{1}", fgstr, e.Reason, tagstr));
                    if (AR.SETTING.Data.Enable_Log_ILock)
                        PUB.logILock.Add("ON", $"{fgstr}:{e.Reason}");
                }
                else if (e.NewOff)
                {
                    if (AR.SETTING.Data.Log_ILock)
                        PUB.log.Add(string.Format("[IL-OF:{2}] {0}:{1}", fgstr, e.Reason, tagstr));
                    if (AR.SETTING.Data.Enable_Log_ILock)
                        PUB.logILock.Add("OF", $"{fgstr}:{e.Reason}");
                }

                var logmsg = "#### ILOCK(" + tagstr + ")변경 : " + fgstr + "(" + e.ArrIDX.ToString() + ") 값:" + e.NewValue.ToString() + ",사유:" + e.Reason;

                //ILOck 로그에 기록
                if (AR.SETTING.Data.Enable_Log_ILock) PUB.logILock.Add(logmsg);

                //메인로그에 기록
                if (AR.SETTING.Data.Log_ILock) PUB.log.Add(logmsg);
            }

        }
        private void Flag_ValueChanged(object sender, VarData<bool>.ValueEventArgs e)
        {
            var fg = (eVarBool)e.Idx;

            if (fg == eVarBool.FG_PK_ITEMON)
            {
                // Pub.log.AddAT(string.Format("[FLAG:{0}] : {1}", fg, e.NewValue));
                hmi1.arVar_Picker[0].ItemOn = e.Value;
            }

            else if (fg == eVarBool.FG_RDY_PZ_LPICKOF) hmi1.arFG_RDY_YP_FPICKOF = e.Value;
            else if (fg == eVarBool.FG_RDY_PZ_RPICKOF) hmi1.arFG_RDY_YP_RPICKOF = e.Value;
            else if (fg == eVarBool.FG_CMD_YP_LPICKON) hmi1.arFG_CMD_YP_FPICKON = e.Value;
            else if (fg == eVarBool.FG_CMD_YP_RPICKON) hmi1.arFG_CMD_YP_RPICKON = e.Value;

            else if (fg == eVarBool.FG_MINSPACE) hmi1.arFlag_Minspace = e.Value;


            hmi1.arVar_Port[0].Ready = PUB.flag.get(eVarBool.FG_RDY_PORT_PL);
            hmi1.arVar_Port[1].Ready = PUB.flag.get(eVarBool.FG_RDY_PORT_PC);
            hmi1.arVar_Port[2].Ready = PUB.flag.get(eVarBool.FG_RDY_PORT_PR);

            if (PUB.sm.Step >= eSMStep.IDLE && PUB.sm.Step != eSMStep.HOME_FULL)
            {
                var logmsg = string.Format("[{0}] {1} : {2} => {3}", e.Idx, fg, e.Value, e.Message);
                //ILOck 로그에 기록
                if (AR.SETTING.Data.Enable_Log_Flag) PUB.logFlag.Add(logmsg);

                //메인로그에 기록
                if (AR.SETTING.Data.Log_flag) PUB.log.Add(logmsg);
            }

        }
    }
}
