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

namespace ITC81DB_2.Datastore
{
    public class AlarmList
    {
        #region Field
        private bool m_Flag;

        private bool m_IsLoadCellError;
        private bool m_IsEntrySensorError;
        private bool m_IsInverterError;
        private bool m_IsPressureSensingError;
        private bool m_IsEmergencyStop;
        private bool m_IsDoorInterlock;
        private bool m_IsSorterAError;
        private bool m_IsStackUpSensorError;
        private bool m_IsWindProofError;
        private bool m_IsSorterBError;
        private bool m_IsEntryNotDetected;

        public event Define.EventHandlerAlarmStateChange EventAlarmStateChange;
        #endregion

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

        #region Property
        public bool Flag
        {
            get { return this.m_Flag; }
            set { this.m_Flag = value; }
        }

        public bool IsLoadCellError
        {
            get { return this.m_IsLoadCellError; }
            set { this.m_IsLoadCellError = value; }
        }
        public bool IsEntrySensorError
        {
            get { return this.m_IsEntrySensorError; }
            set { this.m_IsEntrySensorError = value; }
        }
        public bool IsInverterError
        {
            get { return this.m_IsInverterError; }
            set { this.m_IsInverterError = value; }
        }
        public bool IsPressureSensingError
        {
            get { return this.m_IsPressureSensingError; }
            set { this.m_IsPressureSensingError = value; }
        }
        public bool IsEmergencyStop
        {
            get { return this.m_IsEmergencyStop; }
            set { this.m_IsEmergencyStop = value; }
        }
        public bool IsDoorInterlock
        {
            get { return this.m_IsDoorInterlock; }
            set { this.m_IsDoorInterlock = value; }
        }
        public bool IsSorterAError
        {
            get { return this.m_IsSorterAError; }
            set { this.m_IsSorterAError = value; }
        }
        public bool IsStackUpSensorError
        {
            get { return this.m_IsStackUpSensorError; }
            set { this.m_IsStackUpSensorError = value; }
        }
        public bool IsWindProofError
        {
            get { return this.m_IsWindProofError; }
            set { this.m_IsWindProofError = value; }
        }
        public bool IsSorterBError
        {
            get { return this.m_IsSorterBError; }
            set { this.m_IsSorterBError = value; }
        }
        public bool IsEntryNotDetected
        {
            get { return this.m_IsEntryNotDetected; }
            set { this.m_IsEntryNotDetected = value; }
        }
        #endregion

        #region Method
        private void Initialize()
        {
            this.Flag = false;

            this.IsLoadCellError = false;
            this.IsEntrySensorError = false;
            this.IsInverterError = false;
            this.IsPressureSensingError = false;
            this.IsEmergencyStop = false;
            this.IsDoorInterlock = false;
            this.IsSorterAError = false;
            this.IsStackUpSensorError = false;
            this.IsWindProofError = false;
            this.IsSorterBError = false;
            this.IsEntryNotDetected = false;
        }

        public bool IsAlarmOccured(string alarm)
        {
            bool occur = false;

            if (alarm != "00000000")
                occur = true;

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

            this.Flag = false;

            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-제품진입 미감지
            if (sValue5[2] == '0')
            {
                if (this.IsEntryNotDetected != false)
                {
                    this.IsEntryNotDetected = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a18_EntryNotDetected, "OFF");
                }
            }
            else
            {
                if (this.IsEntryNotDetected != true)
                {
                    this.IsEntryNotDetected = true;
                    this.Flag = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a18_EntryNotDetected, "ON");
                }
            }
            // 알람 17-사용X
            if (sValue5[3] == '0')
            { }
            else
            { }

            // 알람 16-
            if (sValue4[0] == '0')
            { }
            else
            { }
            // 알람 15-
            if (sValue4[1] == '0')
            { }
            else
            { }
            // 알람 14-배출센서 B
            if (sValue4[2] == '0')
            {
                if (this.IsSorterBError != false)
                {
                    this.IsSorterBError = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a14_SorterBError, "OFF");
                }
            }
            else
            {
                if (this.IsSorterBError != true)
                {
                    this.IsSorterBError = true;
                    this.Flag = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a14_SorterBError, "ON");
                }
            }
            // 알람 13-방풍커버열림
            if (sValue4[3] == '0')
            {
                if (this.IsWindProofError != false)
                {
                    this.IsWindProofError = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a13_WindProofError, "OFF");
                }
            }
            else
            {
                if (this.IsWindProofError != true)
                {
                    this.IsWindProofError = true;
                    this.Flag = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a13_WindProofError, "ON");
                }
            }

            // 알람 12-서보알람토크
            if (sValue3[0] == '0')
            { }
            else
            { }
            // 알람 11-통신에러
            if (sValue3[1] == '0')
            { }
            else
            { }
            // 알람 10-서보알람
            if (sValue3[2] == '0')
            { }
            else
            { }
            // 알람 9-인버터알람
            if (sValue3[3] == '0')
            {
                if (this.IsInverterError != false)
                {
                    this.IsInverterError = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a9_InverterError, "OFF");
                }
            }
            else
            {
                if (this.IsInverterError != true)
                {
                    this.IsInverterError = true;
                    this.Flag = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a9_InverterError, "ON");
                }
            }

            // 알람 8-비상정지
            if (sValue2[0] == '0')
            {
                if (this.IsEmergencyStop != false)
                {
                    this.IsEmergencyStop = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a8_EmergencyStop, "OFF");
                }
            }
            else
            {
                if (this.IsEmergencyStop != true)
                {
                    this.IsEmergencyStop = true;
                    this.Flag = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a8_EmergencyStop, "ON");
                }
            }
            // 알람 7-리젝터알람(배출센서 A)
            if (sValue2[1] == '0')
            {
                if (this.IsSorterAError != false)
                {
                    this.IsSorterAError = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a7_SorterAError, "OFF");
                }
            }
            else
            {
                if (this.IsSorterAError != true)
                {
                    this.IsSorterAError = true;
                    this.Flag = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a7_SorterAError, "ON");
                }
            }
            // 알람 6-적체센서
            if (sValue2[2] == '0')
            {
                if (this.IsStackUpSensorError != false)
                {
                    this.IsStackUpSensorError = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a6_StackUpSensorError, "OFF");
                }
            }
            else
            {
                if (this.IsStackUpSensorError != true)
                {
                    this.IsStackUpSensorError = true;
                    this.Flag = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a6_StackUpSensorError, "ON");
                }
            }
            // 알람 5-입력센서2(넘어짐)
            if (sValue2[3] == '0')
            { }
            else
            { }

            // 알람 4-도어알람
            if (sValue1[0] == '0')
            {
                if (this.IsDoorInterlock != false)
                {
                    this.IsDoorInterlock = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a4_DoorInterlock, "OFF");
                }
            }
            else
            {
                if (this.IsDoorInterlock != true)
                {
                    this.IsDoorInterlock = true;
                    this.Flag = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a4_DoorInterlock, "ON");
                }
            }
            // 알람 3-공압에러
            if (sValue1[1] == '0')
            {
                if (this.IsPressureSensingError != false)
                {
                    this.IsPressureSensingError = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a3_PressureSensingError, "OFF");
                }
            }
            else
            {
                if (this.IsPressureSensingError != true)
                {
                    this.IsPressureSensingError = true;
                    this.Flag = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a3_PressureSensingError, "ON");
                }
            }
            // 알람 2-셀 에러
            if (sValue1[2] == '0')
            {
                if (this.IsLoadCellError != false)
                {
                    this.IsLoadCellError = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a2_LoadCellError, "OFF");
                }
            }
            else
            {
                if (this.IsLoadCellError != true)
                {
                    this.IsLoadCellError = true;
                    this.Flag = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a2_LoadCellError, "ON");
                }
            }

            // 알람 1-입력센서 에러
            if (sValue1[3] == '0')
            {
                if (this.IsEntrySensorError != false)
                {
                    this.IsEntrySensorError = false;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a1_EntrySensorError, "OFF");
                }
            }
            else
            {
                if (this.IsEntrySensorError != true)
                {
                    this.IsEntrySensorError = true;
                    this.Flag = true;
                    this.EventAlarmStateChange(Define.E_TrackingAlarm.a1_EntrySensorError, "ON");
                }
            }
        }
        #endregion
    }
}
