using DevExpress.Blazor.Navigation.Internal;
using DevExpress.Utils.Filtering.Internal;
using DevExpress.Xpo.DB.Helpers;
using DevExpress.XtraRichEdit.Model;
using DxBlazorApplication2.Data.Produktion;
using Microsoft.AspNetCore.Components;
using System.Runtime.Intrinsics.X86;

namespace DxBlazorApplication2.Services.Produktion
{
    /// <summary>
    /// Cockpit
    /// </summary>
    public class CockpitService : IDisposable
    {
        private readonly Dictionary<string, CockpitItem> _cockpitItems;
        public delegate void CockpitServiceDelegate(object sender, CockpitServiceEventArgs args);
        public event CockpitServiceDelegate? OnItemChanged;
        private readonly TabellenService _tabellenService;
        private readonly PSDLService _psdlService;
        private readonly BDSTService _bdstService;
        private readonly Logger _logger = new Logger();

        public CockpitService(TabellenService tabellenService, PSDLService psdlService, BDSTService bdstService)
        {
            _logger.Log("CockpitService()");
            _tabellenService = tabellenService;
            _psdlService = psdlService;
            _bdstService = bdstService;
            _cockpitItems = new Dictionary<string, CockpitItem>();

            // Linien aus den Tabellen holen
            foreach (TabellenItem linie in _tabellenService.GetTabellenItems("LILINI"))
            {
                CockpitItem item = new CockpitItem(linie.TABKEY);
                item.WWFIRM = Convert.ToDouble(linie.TABDATA.Substring(0, 3));
                item.WWWERK = Convert.ToDouble(linie.TABDATA.Substring(3, 3));
                _cockpitItems.Add(linie.TABKEY, item);
            }
            // Die aktuellsten buchungen laden
            foreach (PSDLItem item in _psdlService.GetItems())
                if (_cockpitItems.ContainsKey(item.PDLINI))
                    _cockpitItems[item.PDLINI].Load(item);
            // Störungsstatus
            foreach (BDSTItem item in _bdstService.GetItems())
                if (_cockpitItems.ContainsKey(item.BTLINI))
                    _cockpitItems[item.BTLINI].Load(item);
            _psdlService.OnItemChanged += OnPSDLItemChanged;
            _bdstService.OnItemChanged += OnBDSTItemChanged;
        }

        public void Dispose()
        {
            _logger.Log($"CockpitService.Dispose()");
            _psdlService.OnItemChanged -= OnPSDLItemChanged;
            _bdstService.OnItemChanged -= OnBDSTItemChanged;
        }

        public IEnumerable<CockpitItem> GetItems()
        {
            return _cockpitItems.Values;
        }
        public void OnPSDLItemChanged(object sender, PSDLServiceEventArgs args)
        {
            if (_cockpitItems.ContainsKey(args.DataItem.PDLINI))
            {
                _cockpitItems[args.DataItem.PDLINI].Load(args.DataItem);
                if (this.OnItemChanged != null)
                    this.OnItemChanged(this, new CockpitServiceEventArgs(_cockpitItems[args.DataItem.PDLINI]));
            }
        }
        public void OnBDSTItemChanged(object sender, BDSTServiceEventArgs args)
        {
            if (_cockpitItems.ContainsKey(args.DataItem.BTLINI))
            {
                _cockpitItems[args.DataItem.BTLINI].Load(args.DataItem);
                if (this.OnItemChanged != null)
                    this.OnItemChanged(this, new CockpitServiceEventArgs(_cockpitItems[args.DataItem.BTLINI]));
            }
        }
    }

    public class CockpitServiceEventArgs : EventArgs
    {
        public CockpitItem DataItem { get; }
        public CockpitServiceEventArgs(CockpitItem item)
        {
            this.DataItem = item;
        }
    }

}
