using MainShell.Hardware;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MainShell.EventArgsFolder
{
    public class DeviceIoChangedEventArgs : EventArgs
    {
        public DeviceIoChangedEventArgs(IReadOnlyList<DeviceIoPointState> changedPoints, IReadOnlyDictionary<string, DeviceIoPointState> allPoints)
        {
            ChangedPoints = changedPoints ?? new List<DeviceIoPointState>();
            AllPoints = allPoints ?? new Dictionary<string, DeviceIoPointState>();
        }

        public IReadOnlyList<DeviceIoPointState> ChangedPoints { get; private set; }

        public IReadOnlyDictionary<string, DeviceIoPointState> AllPoints { get; private set; }

        public bool TryGetById(int id, out DeviceIoPointState point)
        {
            point = AllPoints.Values.FirstOrDefault(x => x.Id == id);
            return point != null;
        }

        public bool TryGetByPointKey(string pointKey, out DeviceIoPointState point)
        {
            return AllPoints.TryGetValue(pointKey, out point);
        }

        public DeviceIoPointState FindByName(string name)
        {
            return AllPoints.Values.FirstOrDefault(x => string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase));
        }

        public IReadOnlyList<DeviceIoPointState> FindByModule(string module)
        {
            if (string.IsNullOrWhiteSpace(module))
            {
                return new List<DeviceIoPointState>();
            }

            return AllPoints.Values
                .Where(x => string.Equals(x.Module, module, StringComparison.OrdinalIgnoreCase))
                .ToList();
        }
    }
}
