using CommunityToolkit.Mvvm.Input;

namespace DashBoard.ViewModel;

internal partial class DashBoardMainViewModel : ObservableObject
{
    public DashBoardMainViewModel(DeviceCollection deviceCollection, IDialogService dialogService)
    {
        this._DialogService = dialogService;
        this.Devices = deviceCollection.DeviceList;
        deviceCollection.DeviceDataList.CollectionChanged += DeviceDataList_CollectionChanged;
        this.DeviceDataList_CollectionChanged(deviceCollection.DeviceDataList, null);
    }

    private void DeviceDataList_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs? e)
    {
        if (sender is not ObservableDictionary<Guid, DeviceData_VM> deviceList)
            return;

        this.DeviceData.Clear();
        foreach (DeviceData_VM device in deviceList.Values)
        {
            this.DeviceData[device.DeviceModel] ??= [];
            this.DeviceData[device.DeviceModel].Add(device);
        }
    }

    private readonly IDialogService _DialogService;

    [ObservableProperty]
    private ObservableDictionary<Guid, DeviceInfo_VM> _Devices = [];

    [ObservableProperty]
    private ObservableDictionary<DeviceModel, ObservableCollection<DeviceData_VM>> _DeviceData = [];

    [RelayCommand]
    private void Warning(DeviceData_VM device)
    {
        IDialogParameters para = new DialogParameters() { { "alarm", device.Alarms! } };
        this._DialogService.Show("WarningList", para, null);
    }
}