using System.Collections.Generic;
using Aitex.Core.RT.Event;
using Aitex.Core.UI.View.Common;
using Aitex.Core.Utilities;
using MECF.Framework.UI.Client.ClientBase;

namespace MECF.Framework.UI.Client.CenterViews.Alarms.Alarm
{
 
    public class AlarmViewModel : UiViewModelBase 
    {
        [IgnorePropertyChange]
        public List<AlarmItem> AlarmEvents { get; set; }


        public AlarmViewModel() 
        {
            Subscribe("System.LiveAlarmEvent");
        }

        protected override void Poll()
        {
            base.Poll();
        }

        protected override void InvokeBeforeUpdateProperty(Dictionary<string, object> data)
        {
            if (data.TryGetValue("System.LiveAlarmEvent", out var value))
                UpdateAlarmEvent((List<EventItem>)value);
        }

        public void UpdateAlarmEvent(List<EventItem> alarmsFromRt)
        {
            var alarmEvents = new List<AlarmItem>();
            foreach (var item in alarmsFromRt)
            {
            
                var alarmItem = new AlarmItem()
                {
                    Level = item.Level,
                    Source = item.Source,
                    OccuringTime = item.OccuringTime.ToString("yyyy/MM/dd HH:mm:ss"),
                    Description = item.Description,
                    EventEnum = item.EventEnum,
                    EventId = item.Id,
                    Explaination = item.Explaination,
                    Solution = item.Solution,
                    IsAcknowledged = item.IsAcknowledged,
                    AcknowledgeTime = item.AcknowledgedTime
                };
                alarmEvents.Add(alarmItem);
            }
            if (AlarmEvents == null || (alarmEvents.Count != AlarmEvents.Count))
            {
                AlarmEvents = alarmEvents;
            }
            else
            {
                bool isEqual = true;
                if (alarmEvents.Count == AlarmEvents.Count)
                {
                    for (int i = 0; i < alarmEvents.Count; i++)
                    {
                        if (!alarmEvents[i].IsEqualTo(AlarmEvents[i]))
                        {
                            isEqual = false;
                            break;
                        }
                    }
                }
                if (!isEqual)
                    AlarmEvents = alarmEvents;
            }

            NotifyOfPropertyChange(nameof(AlarmEvents));
        }
    }
}