using Prism.Events;
using System.Drawing.Printing;

namespace ProximaAnalizer.ViewModels.DBInfoAlarmRegions;

partial class AlarmDataViewModel : ObservableObject
{
    public AlarmDataViewModel(IEventAggregator eventAggregator)
    {
        this._eventAggregator = eventAggregator;
        this.DataInit();
        eventAggregator.GetEvent<UpdateAlarmData>().Subscribe(UpdateData);
    }
    private readonly IEventAggregator _eventAggregator;

    private void UpdateData()
    {
        this.DataChanged(_eventAggregator.GetEvent<UpdateAlarmData>().PmData, _eventAggregator.GetEvent<UpdateAlarmData>().SystemData);
    }


    public void DataInit()
    {
        this.MFCs.Clear();
        this.Heaters.Clear();
        this.GaslineHeaters.Clear();
        this.FFUs.Clear();
        this.Valves.Clear();
        this.LeakChecks.Clear();
        this.Pressures.Clear();
        this.APCs.Clear();
        this.APCVATGVs.Clear();
    }

    private void DataChanged(List<dynamic>? pmData, List<dynamic>? systemData)
    {
        App.Current.Dispatcher?.Invoke(() =>
        {
            //Parallel.Invoke(
            //() => this.MFCs.UpdateData(pmData),
            //() => this.Heaters.UpdateData(systemData),
            //() => this.GaslineHeaters.UpdateData(pmData),
            //() => this.FFUs.UpdateData(pmData),
            //() => this.Valves.UpdateData(pmData),

            //() => this.LeakChecks.UpdateData(pmData),
            //() => this.Pressures.UpdateData(pmData),
            //() => this.APCs.UpdateData(pmData),
            //() => this.APCVATGVs.UpdateData(pmData));

            this.MFCs.UpdateData(pmData);
            this.Heaters.UpdateData(systemData);
            this.GaslineHeaters.UpdateData(pmData);
            this.FFUs.UpdateData(pmData);
            this.Valves.UpdateData(pmData);

            this.LeakChecks.UpdateData(pmData);
            this.Pressures.UpdateData(pmData);
            this.APCs.UpdateData(pmData);
            this.APCVATGVs.UpdateData(pmData);

            this.Selected ??= this.Heaters.Data.FirstOrDefault();

        });
    }

    [ObservableProperty]
    private DisplayHistroyHelper2 _Heaters = new(1, 2, "Heater");
    [ObservableProperty]
    private DisplayHistroyHelper2 _MFCs = new(1, 2, "MFC");
    [ObservableProperty]
    private DisplayHistroyHelper2 _Valves = new(2, 3, "IoValve");
    [ObservableProperty]
    private DisplayHistroyHelper2 _FFUs = new(1, 2, "FFU");
    [ObservableProperty]
    private DisplayHistroyHelper2 _GaslineHeaters = new(1, 2, "GaslineHeater");

    [ObservableProperty]
    private DisplayHistroyHelper1 _LeakChecks = new(1, 1, ["LeakCheck"]);
    [ObservableProperty]
    private DisplayHistroyHelper1 _Pressures = new(1, 1, ["VG", "PS", "PG"]);
    [ObservableProperty]
    private DisplayHistroyHelper1 _APCs = new(1, 2, ["APC"]);
    [ObservableProperty]
    private DisplayHistroyHelper1 _APCVATGVs = new(1, 2, ["APCVATGV"]);


    [ObservableProperty]
    private object? _Selected;

    [RelayCommand]
    private void SelectData(object? para)
    {
        _eventAggregator.GetEvent<UpdateSelectData>().Selected = this.Selected;
        _eventAggregator.GetEvent<UpdateSelectData>().KeyNames = para;
        _eventAggregator.GetEvent<UpdateSelectData>().Publish();
    }
}
