namespace ConfigFileManager.ViewModels;

public partial class ConfigFileManagerViewModel : ObservableObject
{
    public ConfigFileManagerViewModel(DeviceCollection deviceCollection, IDialogService dialogService)
    {
        _DialogService = dialogService;
        deviceCollection.DeviceList.CollectionChanged += DeviceList_CollectionChanged;
        this.DeviceList_CollectionChanged(deviceCollection.DeviceList, null);
    }

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

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

    private readonly IDialogService _DialogService;


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


    [RelayCommand]
    private void Migrate(DeviceInfo_VM deviceInfo)
    {
        IDialogParameters para = new DialogParameters()
        {
            {"Device",deviceInfo }
        };
        this._DialogService.ShowDialog("Migrate", para);
    }

    [RelayCommand]
    private async Task Update(DeviceInfo_VM deviceInfo)
    {
        deviceInfo.ConfigUpdating = true;

        await Task.Delay(5000);

        deviceInfo.ConfigUpdating = false;
        deviceInfo.UpdateTime = DateTime.Now;
    }


    [RelayCommand]
    private void CheckRecipe(DeviceInfo_VM deviceInfo)
    {
        IDialogParameters para = new DialogParameters()
        {
            {"Device",deviceInfo }
        };
        this._DialogService.ShowDialog("Recipe", para);
    }

    [RelayCommand]
    private void CheckConfig(DeviceInfo_VM deviceInfo)
    {
        IDialogParameters para = new DialogParameters()
        {
            {"Device",deviceInfo }
        };
        this._DialogService.ShowDialog("Config", para);
    }
}
