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

namespace INT_PT002.DataStore
{
    public class AlarmList
    {
        #region Field
        private bool m_IsServo1Alarm;
        private bool m_IsServo2Alarm;
        private bool m_IsServo1TorqueAlarm;
        private bool m_IsServo2TorqueAlarm;
        private bool m_IsPressureError;
        private bool m_IsServoEmergencyStop;

        private Collection<bool> m_CollectionIsSensorBoardError;

        public event Define.EventHandlerAlarmStateChange EventAlarmStateChange;
        #endregion

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

        #region Property
        public bool IsServo1Alarm
        {
            get { return this.m_IsServo1Alarm; }
            set { this.m_IsServo1Alarm = value; }
        }
        public bool IsServo2Alarm
        {
            get { return this.m_IsServo2Alarm; }
            set { this.m_IsServo2Alarm = value; }
        }
        public bool IsServoTorque1AlarmTorque
        {
            get { return this.m_IsServo1TorqueAlarm; }
            set { this.m_IsServo1TorqueAlarm = value; }
        }
        public bool IsServoTorque2AlarmTorque
        {
            get { return this.m_IsServo2TorqueAlarm; }
            set { this.m_IsServo2TorqueAlarm = value; }
        }
        public bool IsPressureError
        {
            get { return this.m_IsPressureError; }
            set { this.m_IsPressureError = value; }
        }
        public bool IsServoEmergencyStop
        {
            get { return this.m_IsServoEmergencyStop; }
            set { this.m_IsServoEmergencyStop = value; }
        }

        public Collection<bool> CollectionIsSensorBoardError
        {
            get { return this.m_CollectionIsSensorBoardError; }
            set { this.m_CollectionIsSensorBoardError = value; }
        }
        #endregion

        #region Method
        private void Initialize()
        {
            this.IsServo1Alarm = false;
            this.IsServo2Alarm = false;
            this.IsServoTorque1AlarmTorque = false;
            this.IsServoTorque2AlarmTorque = false;
            this.IsPressureError = false;
            this.IsServoEmergencyStop = false;

            this.CollectionIsSensorBoardError = new Collection<bool>();
            for (int i = 0; i < 10; i++)
                this.CollectionIsSensorBoardError.Add(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');

            // 알람 16-센서보드10 에러
            if (sValue4[0] == '0')
            {
                if (this.CollectionIsSensorBoardError[9] != false)
                {
                    this.CollectionIsSensorBoardError[9] = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a16_SensorBoard10Error, "OFF");
                }
            }
            else
            {
                if (this.CollectionIsSensorBoardError[9] != true)
                {
                    this.CollectionIsSensorBoardError[9] = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a16_SensorBoard10Error, "ON");
                }
            }
            // 알람 15-센서보드9 에러
            if (sValue4[1] == '0')
            {
                if (this.CollectionIsSensorBoardError[8] != false)
                {
                    this.CollectionIsSensorBoardError[8] = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a15_SensorBoard9Error, "OFF");
                }
            }
            else
            {
                if (this.CollectionIsSensorBoardError[8] != true)
                {
                    this.CollectionIsSensorBoardError[8] = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a15_SensorBoard9Error, "ON");
                }
            }
            // 알람 14-센서보드8 에러
            if (sValue4[2] == '0')
            {
                if (this.CollectionIsSensorBoardError[7] != false)
                {
                    this.CollectionIsSensorBoardError[7] = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a14_SensorBoard8Error, "OFF");
                }
            }
            else
            {
                if (this.CollectionIsSensorBoardError[7] != true)
                {
                    this.CollectionIsSensorBoardError[7] = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a14_SensorBoard8Error, "ON");
                }
            }
            // 알람 13-센서보드7 에러
            if (sValue4[3] == '0')
            {
                if (this.CollectionIsSensorBoardError[6] != false)
                {
                    this.CollectionIsSensorBoardError[6] = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a13_SensorBoard7Error, "OFF");
                }
            }
            else
            {
                if (this.CollectionIsSensorBoardError[6] != true)
                {
                    this.CollectionIsSensorBoardError[6] = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a13_SensorBoard7Error, "ON");
                }
            }

            // 알람 12-센서보드6 에러
            if (sValue3[0] == '0')
            {
                if (this.CollectionIsSensorBoardError[5] != false)
                {
                    this.CollectionIsSensorBoardError[5] = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a12_SensorBoard6Error, "OFF");
                }
            }
            else
            {
                if (this.CollectionIsSensorBoardError[5] != true)
                {
                    this.CollectionIsSensorBoardError[5] = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a12_SensorBoard6Error, "ON");
                }
            }
            // 알람 11-센서보드5 에러
            if (sValue3[1] == '0')
            {
                if (this.CollectionIsSensorBoardError[4] != false)
                {
                    this.CollectionIsSensorBoardError[4] = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a11_SensorBoard5Error, "OFF");
                }
            }
            else
            {
                if (this.CollectionIsSensorBoardError[4] != true)
                {
                    this.CollectionIsSensorBoardError[4] = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a11_SensorBoard5Error, "ON");
                }
            }
            // 알람 10-센서보드4 에러
            if (sValue3[2] == '0')
            {
                if (this.CollectionIsSensorBoardError[3] != false)
                {
                    this.CollectionIsSensorBoardError[3] = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a10_SensorBoard4Error, "OFF");
                }
            }
            else
            {
                if (this.CollectionIsSensorBoardError[3] != true)
                {
                    this.CollectionIsSensorBoardError[3] = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a10_SensorBoard4Error, "ON");
                }
            }
            // 알람 9-센서보드3 에러
            if (sValue3[3] == '0')
            {
                if (this.CollectionIsSensorBoardError[2] != false)
                {
                    this.CollectionIsSensorBoardError[2] = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a9_SensorBoard3Error, "OFF");
                }
            }
            else
            {
                if (this.CollectionIsSensorBoardError[2] != true)
                {
                    this.CollectionIsSensorBoardError[2] = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a9_SensorBoard3Error, "ON");
                }
            }

            // 알람 8-센서보드2 에러
            if (sValue2[0] == '0')
            {
                if (this.CollectionIsSensorBoardError[1] != false)
                {
                    this.CollectionIsSensorBoardError[1] = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a8_SensorBoard2Error, "OFF");
                }
            }
            else
            {
                if (this.CollectionIsSensorBoardError[1] != true)
                {
                    this.CollectionIsSensorBoardError[1] = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a8_SensorBoard2Error, "ON");
                }
            }
            // 알람 7-센서보드1 에러
            if (sValue2[1] == '0')
            {
                if (this.CollectionIsSensorBoardError[0] != false)
                {
                    this.CollectionIsSensorBoardError[0] = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a7_SensorBoard1Error, "OFF");
                }
            }
            else
            {
                if (this.CollectionIsSensorBoardError[0] != true)
                {
                    this.CollectionIsSensorBoardError[0] = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a7_SensorBoard1Error, "ON");
                }
            }
            // 알람 6-압력에러
            if (sValue2[2] == '0')
            {
                if (this.IsPressureError != false)
                {
                    this.IsPressureError = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a6_PressureError, "OFF");
                }
            }
            else
            {
                if (this.IsPressureError != true)
                {
                    this.IsPressureError = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a6_PressureError, "ON");
                }
            }
            // 알람 5-서보2 토크
            if (sValue2[3] == '0')
            {
                if (this.IsServoTorque2AlarmTorque != false)
                {
                    this.IsServoTorque2AlarmTorque = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a5_Servo2TorqueAlarm, "OFF");
                }
            }
            else
            {
                if (this.IsServoTorque2AlarmTorque != true)
                {
                    this.IsServoTorque2AlarmTorque = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a5_Servo2TorqueAlarm, "ON");
                }
            }

            // 알람 4-서보1 토크
            if (sValue1[0] == '0')
            {
                if (this.IsServoTorque1AlarmTorque != false)
                {
                    this.IsServoTorque1AlarmTorque = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a4_Servo1TorqueAlarm, "OFF");
                }
            }
            else
            {
                if (this.IsServoTorque1AlarmTorque != true)
                {
                    this.IsServoTorque1AlarmTorque = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a4_Servo1TorqueAlarm, "ON");
                }
            }
            // 알람 3-서보2 알람
            if (sValue1[1] == '0')
            {
                if (this.IsServo2Alarm != false)
                {
                    this.IsServo2Alarm = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a3_Servo2Alarm, "OFF");
                }
            }
            else
            {
                if (this.IsServo2Alarm != true)
                {
                    this.IsServo2Alarm = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a3_Servo2Alarm, "ON");
                }
            }
            // 알람 2-서보1 알람
            if (sValue1[2] == '0')
            {
                if (this.IsServo1Alarm != false)
                {
                    this.IsServo1Alarm = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a2_Servo1Alarm, "OFF");
                }
            }
            else
            {
                if (this.IsServo1Alarm != true)
                {
                    this.IsServo1Alarm = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a2_Servo1Alarm, "ON");
                }
            }
            // 알람 1-서보OFF
            if (sValue1[3] == '0')
            {
                if (this.IsServoEmergencyStop != false)
                {
                    this.IsServoEmergencyStop = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a1_ServoEmergencyStop, "OFF");
                }
            }
            else
            {
                if (this.IsServoEmergencyStop != true)
                {
                    this.IsServoEmergencyStop = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a1_ServoEmergencyStop, "ON");
                }
            }
        }
        #endregion
    }
}
