using System;
using System.Collections.Generic;

namespace MECF.Framework.Common.Aitex.Core.RT.EMS
{

    /// <summary>
    /// 帮助Maintainer管理 维护项
    /// </summary>
    public class MaintainerItemCollection
    {
        #region Variables

        List<MaintainerItem> _items = new List<MaintainerItem>();

        List<MaintainerItemInfo> _itemInfos = new ();

        public delegate void ListChanged(); 
        public event ListChanged OnListChanged;

        int _unmaintainCount = 0;

        public int Count
        {
            get
            {
                int count = 0;
                foreach (var i in _items)
                {
                    if (i.Enable)
                    {
                        count++;
                    }
                }
                return count;
            }
        }

        public int UnMaintainCount => _unmaintainCount;

        #endregion

        #region Constructors
        public MaintainerItemCollection()
        {

        }

        #endregion

        #region Functions

        public void SortInfo()
        {
            _itemInfos.Sort((a, b) => a.Config.Index.CompareTo(b.Config.Index));
        }

        public void CheckAllItemMaintained(DateTime date)
        {
            int count = 0;
            foreach (var i in _items)
            {
                if (!i.CheckMaintained(date) && i.Enable)
                {
                    count++;
                }
            }
            _unmaintainCount = count;
        }

        public bool SetMaintain(string itemid, DateTime date)
        {
            var item = _items.Find(i => i.UID == itemid);
            if (item != null)
            {
                item.Maintained(date);
                var item2 = _itemInfos.Find(i => i.UID == itemid);
                if (item2!= null)
                {
                    item2.MaintainedDate = date;
                }
                return true;
            }
            else return false;
        }

        public bool SetMaintainerItemEnable(string itemid, bool enable)
        {
            var item = _items.Find(i => i.UID == itemid);
            if (item != null)
            {
                item.SetEnable(enable);
                var iteminfo = _itemInfos.Find(i => i.UID == itemid);
                if (item.Enable && iteminfo == null)
                {
                    _itemInfos.Add(item.GetInfo());
                    SortInfo();
                }
                else if (!item.Enable && iteminfo != null)
                {
                    _itemInfos.Remove(iteminfo);
                }
                return true;
            }
            else return false;
        }

        public void AddMaintainerItem(MaintainerItem item)
        {
            _items.Add(item);
            if (item.Enable)
            {
                _itemInfos.Add(item.GetInfo());
            }
        }

        public bool RemoveMaintainerItem(string uid)
        {
            var item1 = _itemInfos.Find(i => i.UID == uid);
            if (item1 != null)
            {
                _itemInfos.Remove(item1);
                var item3 = _items.Find(i => i.UID == uid);
                if (item3 != null)
                {
                    _items.Remove(item3);
                    return true;
                }
                return false;
            }
            return false;
        }

        public void AddMaintainerItemList(List<MaintainerItem> items)
        {
            _items.AddRange(items);
            foreach (var item in items)
            {
                if (item.Enable)
                {
                    _itemInfos.Add(item.GetInfo());
                }
            }
        }

        public List<MaintainerItem> GetAllItems()
        {
            return _items;
        }

        public List<MaintainerItemInfo> GetAllItemInfos()
        {
            return _itemInfos;
        }

        #endregion

    }
}
