using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using AR;
namespace Project
{
	public partial class FMain
	{
		private void Dio_IOValueChanged(object sender, arDev.DIO.IOValueEventArgs e)
		{
			if (e.Direction == arDev.DIO.eIOPINDIR.INPUT)
			{
				var diPin = DIO.Pin.input.Where(t=>t.terminalno == e.ArrIDX).FirstOrDefault();
                if(diPin == null)
                {
                    PUB.log.AddE($"No terminal target found for DI INDEX:{e.ArrIDX}");
                }
                else
                {
                    var pin = (eDIName)diPin.idx;
                    if (AR.SETTING.Data.Log_DI)
                    {
                        PUB.log.Add("DIO", String.Format("DI:IDX={0},DIR={1},VAL={2},NM={3}", e.ArrIDX, e.Direction, e.NewValue, diPin));
                    }
                    var value =  DIO.GetIOInput(pin);
                    _DIO_INPUT_VALUE_CHANGED(pin, value);
                }
			}
			else
			{
                var doPin = DIO.Pin.output.Where(t => t.terminalno == e.ArrIDX).FirstOrDefault();
                if (doPin == null)
                {
                    PUB.log.AddE($"No terminal target found for DO INDEX:{e.ArrIDX}");
                }
                else
                {
                    var pin = (eDOName)doPin.idx;

                    if (AR.SETTING.Data.Log_DO)
                    {
                        //타워램프는 제외하낟.
                        if (e.ArrIDX != (byte)eDOName.TWR_GRNF &&
                            e.ArrIDX != (byte)eDOName.TWR_REDF &&
                            e.ArrIDX != (byte)eDOName.TWR_YELF &&
                            e.ArrIDX != (byte)eDOName.BUT_STARTF &&
                            e.ArrIDX != (byte)eDOName.BUT_STOPF &&
                            e.ArrIDX != (byte)eDOName.BUT_RESETF)
                            PUB.log.Add("DIO", String.Format("DO:IDX={0},DIR={1},VAL={2}", e.ArrIDX, e.Direction, e.NewValue));
                    }
                    _DIO_OUTPUT_VALUE_CHANGED(pin, e.NewValue);

                }
              
			}
		}

		void _DIO_IOMessage(object sender, arDev.DIO.MessageEventArgs e)
		{
			if (e.IsError)
			{
				if (e.Message.ToLower().IndexOf("inposi") != -1 || e.Message.ToLower().IndexOf("동일위치") != -1)
				{
					PUB.log.AddAT("DIO:" + e.Message);
				}
				else
				{
					PUB.log.AddE("DIO:" + e.Message);
				}
			}
			else PUB.log.Add("DIO:" + e.Message);
		}

		//void IO_SaftySensor_Changed(eDIName pin, eFlag flag, eVar_Date varDt_On, eVar_Date varDt_Off, bool value)
		//{
		//    //안전센서는 일정 시간 동작하는것을 체크한다
		//    Pub.log.AddAT(string.Format("{0} Sensor : {1}", pin, value));
		//    if (value) Pub.Var_dateTime[(int)varDt_On] = DateTime.Now;
		//    else Pub.Var_dateTime[(int)varDt_Off] = DateTime.Now;
		//}

		void IncCount(int seq, int port, int value = 1)
		{
			if (SETTING.Counter.DateStr != DateTime.Now.ToShortDateString())
			{
				if (port == 0) SETTING.Counter.CountDP1 = value;
				if (port == 1) SETTING.Counter.CountDP2 = value;
				if (port == 2) SETTING.Counter.CountDP3 = value;
				if (port == 3) SETTING.Counter.CountDP4 = value;
				SETTING.Counter.DateStr = DateTime.Now.ToShortDateString();
			}
			else
			{
				if (port == 0) SETTING.Counter.CountDP1 += value;
				if (port == 1) SETTING.Counter.CountDP2 += value;
				if (port == 2) SETTING.Counter.CountDP3 += value;
				if (port == 3) SETTING.Counter.CountDP4 += value;
			}


			//각 포트별 수량은 차수별 작업이므로 차수가 변경되면 리셋됨
			//리셋되는 코드 필요함
			if (port == 0) SETTING.Counter.CountP0 += value;
			if (port == 1) SETTING.Counter.CountP1 += value;
			if (port == 2) SETTING.Counter.CountP2 += value;
			if (port == 3) SETTING.Counter.CountPrintR += value;
			else
			{
				PUB.log.AddAT(string.Format("[{0}] Cannot increase quantity as it is an unspecified port", seq));
			}

			//Pub.log.AddI("수량정보가 저장 되었습니다");
			SETTING.Counter.Save();
		}



		
	}
}
