using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace INT_LKD_2.DataStore
{
    public class AlarmList
    {
        #region Field
        private bool m_A1_ServoEmergencyStop;
        private bool m_A2_PressureError;
        private bool m_A3_BoardStatusCOM;

        private bool m_A5_ServoAlarm1;
        private bool m_A6_ServoTorqueAlarm1;
        private bool m_A7_ServoAlarm2;
        private bool m_A8_ServoTorqueAlarm2;
        private bool m_A9_PressureSensorError1;
        private bool m_A10_PressureSensorError2;

        private bool m_A13_DiffSensorError1;
        private bool m_A14_DiffSensorError2;

        private bool m_A17_DispSensorError1;
        private bool m_A18_DispSensorError2;
     
        public event Define.EventHandlerAlarmStateChange EventAlarmStateChange;
        #endregion

        #region Constructor
        public AlarmList()
        {
            this.Initialize();
        }
        #endregion

        #region Property
        public bool A1_ServoEmergencyStop
        {
            get { return this.m_A1_ServoEmergencyStop; }
            private set { this.m_A1_ServoEmergencyStop = value; }
        }
        public bool A2_PressureError
        {
            get { return this.m_A2_PressureError; }
            private set { this.m_A2_PressureError = value; }
        }
        public bool A3_BoardStatusCOM
        {
            get { return this.m_A3_BoardStatusCOM; }
            private set { this.m_A3_BoardStatusCOM = value; }
        }

        public bool A5_ServoAlarm1
        {
            get { return this.m_A5_ServoAlarm1; }
            private set { this.m_A5_ServoAlarm1 = value; }
        }
        public bool A6_ServoTorqueAlarm1
        {
            get { return this.m_A6_ServoTorqueAlarm1; }
            private set { this.m_A6_ServoTorqueAlarm1 = value; }
        }
        public bool A7_ServoAlarm2
        {
            get { return this.m_A7_ServoAlarm2; }
            private set { this.m_A7_ServoAlarm2 = value; }
        }
        public bool A8_ServoTorqueAlarm2
        {
            get { return this.m_A8_ServoTorqueAlarm2; }
            private set { this.m_A8_ServoTorqueAlarm2 = value; }
        }
    
        public bool A9_PressureSensorError1
        {
            get { return this.m_A9_PressureSensorError1; }
            private set { this.m_A9_PressureSensorError1 = value; }
        }
        public bool A10_PressureSensorError2
        {
            get { return this.m_A10_PressureSensorError2; }
            private set { this.m_A10_PressureSensorError2 = value; }
        }

        public bool A13_DiffSensorError1
        {
            get { return this.m_A13_DiffSensorError1; }
            private set { this.m_A13_DiffSensorError1 = value; }
        }
        public bool A14_DiffSensorError2
        {
            get { return this.m_A14_DiffSensorError2; }
            private set { this.m_A14_DiffSensorError2 = value; }
        }

        public bool A17_DispSensorError1
        {
            get { return this.m_A17_DispSensorError1; }
            private set { this.m_A17_DispSensorError1 = value; }
        }
        public bool A18_DispSensorError2
        {
            get { return this.m_A18_DispSensorError2; }
            private set { this.m_A18_DispSensorError2 = value; }
        }

        #endregion

        #region Method
        private void Initialize()
        {
            this.A1_ServoEmergencyStop = false;
            this.A2_PressureError = false;
            this.A3_BoardStatusCOM = false;

            this.A5_ServoAlarm1 = false;
            this.A6_ServoTorqueAlarm1 = false;
            this.A7_ServoAlarm2 = false;
            this.A8_ServoTorqueAlarm2 = false;
            this.A9_PressureSensorError1 = false;
            this.A10_PressureSensorError2 = false;

            this.A13_DiffSensorError1 = false;
            this.A14_DiffSensorError2 = false;

            this.A17_DispSensorError1 = false;
            this.A18_DispSensorError2 = false;
        }

        public void SetAlarm(string alarm)
        {
            string sValue1 = "", sValue2 = "", sValue3 = "", sValue4 = "", sValue5 = "", sValue6 = "", sValue7 = "", sValue8 = "";

            if (alarm.Length != 8)
                return;

            sValue1 = Convert.ToString(Convert.ToInt16(alarm.Substring(0, 1), 16), 2).PadLeft((4 <= 1) ? 1 : 4, '0');
            sValue2 = Convert.ToString(Convert.ToInt16(alarm.Substring(1, 1), 16), 2).PadLeft((4 <= 1) ? 1 : 4, '0');
            sValue3 = Convert.ToString(Convert.ToInt16(alarm.Substring(2, 1), 16), 2).PadLeft((4 <= 1) ? 1 : 4, '0');
            sValue4 = Convert.ToString(Convert.ToInt16(alarm.Substring(3, 1), 16), 2).PadLeft((4 <= 1) ? 1 : 4, '0');
            sValue5 = Convert.ToString(Convert.ToInt16(alarm.Substring(4, 1), 16), 2).PadLeft((4 <= 1) ? 1 : 4, '0');
            sValue6 = Convert.ToString(Convert.ToInt16(alarm.Substring(5, 1), 16), 2).PadLeft((4 <= 1) ? 1 : 4, '0');
            sValue7 = Convert.ToString(Convert.ToInt16(alarm.Substring(6, 1), 16), 2).PadLeft((4 <= 1) ? 1 : 4, '0');
            sValue8 = Convert.ToString(Convert.ToInt16(alarm.Substring(7, 1), 16), 2).PadLeft((4 <= 1) ? 1 : 4, '0');

            // 알람 20-
            if (sValue5[0] == '0')
            {
            }
            else
            {
            }
            // 알람 19-
            if (sValue5[1] == '0')
            {
            }
            else
            {
            }
            // 알람 18-변위센서에러2
            if (sValue5[2] == '0')
            {
                if (this.A18_DispSensorError2 != false)
                {
                    this.A18_DispSensorError2 = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a18_DispSensorError2, "OFF");
                }
            }
            else
            {
                if (this.A18_DispSensorError2 != true)
                {
                    this.A18_DispSensorError2 = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a18_DispSensorError2, "ON");
                }
            }
            // 알람 17-변위센서에러1
            if (sValue5[3] == '0')
            {
                if (this.A17_DispSensorError1 != false)
                {
                    this.A17_DispSensorError1 = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a17_DispSensorError1, "OFF");
                }
            }
            else
            {
                if (this.A17_DispSensorError1 != true)
                {
                    this.A17_DispSensorError1 = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a17_DispSensorError1, "ON");
                }
            }

            // 알람 16-
            if (sValue4[0] == '0')
            {
            }
            else
            {
            }
            // 알람 15-
            if (sValue4[1] == '0')
            {
            }
            else
            {
            }
            // 알람 14-차압센서에러2
            if (sValue4[2] == '0')
            {
                if (this.A14_DiffSensorError2 != false)
                {
                    this.A14_DiffSensorError2 = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a14_DiffSensorError2, "OFF");
                }
            }
            else
            {
                if (this.A14_DiffSensorError2 != true)
                {
                    this.A14_DiffSensorError2 = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a14_DiffSensorError2, "ON");
                }
            }
            // 알람 13-차압센서에러1
            if (sValue4[3] == '0')
            {
                if (this.A13_DiffSensorError1 != false)
                {
                    this.A13_DiffSensorError1 = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a13_DiffSensorError1, "OFF");
                }
            }
            else
            {
                if (this.A13_DiffSensorError1 != true)
                {
                    this.A13_DiffSensorError1 = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a13_DiffSensorError1, "ON");
                }
            }

            // 알람 12-
            if (sValue3[0] == '0')
            {
            }
            else
            {
            }
            // 알람 11-
            if (sValue3[1] == '0')
            {
            }
            else
            {
            }
            // 알람 10-압력센서에러2
            if (sValue3[2] == '0')
            {
                if (this.A10_PressureSensorError2 != false)
                {
                    this.A10_PressureSensorError2 = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a10_PressureSensorError2, "OFF");
                }
            }
            else
            {
                if (this.A10_PressureSensorError2 != true)
                {
                    this.A10_PressureSensorError2 = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a10_PressureSensorError2, "ON");
                }
            }
            // 알람 9-압력센서에러1
            if (sValue3[3] == '0')
            {
                if (this.A9_PressureSensorError1 != false)
                {
                    this.A9_PressureSensorError1 = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a9_PressureSensorError1, "OFF");
                }
            }
            else
            {
                if (this.A9_PressureSensorError1 != true)
                {
                    this.A9_PressureSensorError1 = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a9_PressureSensorError1, "ON");
                }
            }

            // 알람 8-서보토크알람2
            if (sValue2[0] == '0')
            {
                if (this.A8_ServoTorqueAlarm2 != false)
                {
                    this.A8_ServoTorqueAlarm2 = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a8_ServoTorqueAlarm2, "OFF");
                }
            }
            else
            {
                if (this.A8_ServoTorqueAlarm2 != true)
                {
                    this.A8_ServoTorqueAlarm2 = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a8_ServoTorqueAlarm2, "ON");
                }
            }
            // 알람 7-서보알람2
            if (sValue2[1] == '0')
            {
                if (this.A7_ServoAlarm2 != false)
                {
                    this.A7_ServoAlarm2 = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a7_ServoAlarm2, "OFF");
                }
            }
            else
            {
                if (this.A7_ServoAlarm2 != true)
                {
                    this.A7_ServoAlarm2 = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a7_ServoAlarm2, "ON");
                }
            }
            // 알람 6-서보토크알람1
            if (sValue2[2] == '0')
            {
                if (this.A6_ServoTorqueAlarm1 != false)
                {
                    this.A6_ServoTorqueAlarm1 = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a6_ServoToruqeAlarm1, "OFF");
                }
            }
            else
            {
                if (this.A6_ServoTorqueAlarm1 != true)
                {
                    this.A6_ServoTorqueAlarm1 = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a6_ServoToruqeAlarm1, "ON");
                }
            }
            // 알람 5-서보알람1
            if (sValue2[3] == '0')
            {
                if (this.A5_ServoAlarm1 != false)
                {
                    this.A5_ServoAlarm1 = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a5_ServoAlarm1, "OFF");
                }
            }
            else
            {
                if (this.A5_ServoAlarm1 != true)
                {
                    this.A5_ServoAlarm1 = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a5_ServoAlarm1, "ON");
                }
            }

            // 알람 4-
            if (sValue1[0] == '0')
            {
            }
            else
            {
            }
            // 알람 3-MasterSlave Comm 연결
            if (sValue1[1] == '0')
            {
                if (this.A3_BoardStatusCOM != false)
                {
                    this.A3_BoardStatusCOM = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a3_BoardStatusCOM, "OFF");
                }
            }
            else
            {
                if (this.A3_BoardStatusCOM != true)
                {
                    this.A3_BoardStatusCOM = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a3_BoardStatusCOM, "ON");
                }
            }
            // 알람 2-공압 에러
            if (sValue1[2] == '0')
            {
                if (this.A2_PressureError != false)
                {
                    this.A2_PressureError = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a2_PressureError, "OFF");
                }
            }
            else
            {
                if (this.A2_PressureError != true)
                {
                    this.A2_PressureError = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a2_PressureError, "ON");
                }
            }
            // 알람 1-서보OFF
            if (sValue1[3] == '0')
            {
                if (this.A1_ServoEmergencyStop != false)
                {
                    this.A1_ServoEmergencyStop = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a1_ServoEmergencyStop, "OFF");
                }
            }
            else
            {
                if (this.A1_ServoEmergencyStop != true)
                {
                    this.A1_ServoEmergencyStop = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a1_ServoEmergencyStop, "ON");
                }
            }
        }
        #endregion
    }
}
