using DevExpress.Blazor.Navigation.Internal;
using DevExpress.Xpo.DB.Helpers;
using DevExpress.XtraRichEdit.Model;
using DxBlazorApplication2.Data.Produktion;

namespace DxBlazorApplication2.Services.Produktion
{
    /// <summary>
    /// Störungen
    /// </summary>
    public class BDSTService : IDisposable
    {
        private readonly Dictionary<string, BDSTItem> _bdstItems;
        public delegate void BDSTServiceDelegate(object sender, BDSTServiceEventArgs args);
        public event BDSTServiceDelegate? OnItemChanged;
        private readonly Logger _logger = new Logger();

        public BDSTService()
        {
            _logger.Log("BDSTService()");
            _bdstItems = new Dictionary<string, BDSTItem>();
        }
        public void Dispose()
        {
            _logger.Log($"BDSTService.Dispose()");
        }

        public void AddItem(BDSTItem item)
        {
            _bdstItems[item.BTLINI] = item;
            if (this.OnItemChanged != null)
                this.OnItemChanged(this, new BDSTServiceEventArgs(item));
        }
        public void RemoveItem(BDSTItem item)
        {
            // Für die folgenden "OnItemChanged"
            item.Deleted = true;
            if(_bdstItems.ContainsKey(item.BTLINI))
                _bdstItems.Remove(item.BTLINI);
            if (this.OnItemChanged != null)
                this.OnItemChanged(this, new BDSTServiceEventArgs(item));
        }

        public IEnumerable<BDSTItem> GetItems()
        {
            return _bdstItems.Values;
        }
        public Dictionary<string, BDSTItem> GetDictionary()
        {
            return _bdstItems;
        }
    }

    public class BDSTServiceEventArgs : EventArgs
    {
        public BDSTItem DataItem { get; }
        public BDSTServiceEventArgs(BDSTItem item)
        {
            this.DataItem = item;
        }
    }
}
