using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using UICommon.DataType;

namespace DeviceManagement.ViewModels;

public partial class DeviceManagerViewModel : ObservableObject
{
    public DeviceManagerViewModel(DeviceCollection deviceCollection, IDialogService dialogService)
    {
        this._DeviceCollection = deviceCollection;
        this._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;
    private readonly DeviceCollection _DeviceCollection;

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



    [RelayCommand]
    private void CheckDetail(DeviceInfo_VM deviceInfo)
    {
        string path = Path.Combine(System.Environment.CurrentDirectory, "Analizer");
        try
        {
            object o = deviceInfo.DeviceModel switch
            {
                DeviceModel.JetKepler => Process.Start(Path.Combine(path, "Kepler", "KeplerAnalizer.exe")),
                DeviceModel.Proxima => Process.Start(Path.Combine(path, "Proxima", "ProximaAnalizer.exe")),
                _ => MessageBox.Show("Undefined Type")
            };
        }
        catch
        {

        }

    }

    [RelayCommand]
    private void Remote(DeviceInfo_VM deviceInfo)
    {
        //string path = Path.Combine(System.Environment.CurrentDirectory, "Analizer");
        try
        {
            object o = deviceInfo.DeviceModel switch
            {
                //DeviceModel.JetKepler => Process.Start(Path.Combine(path, "Kepler", "KeplerAnalizer.exe")),
                DeviceModel.Proxima => Process.Start(@"E:\Furnace\Furnace\FurnaceUI\FurnaceUI.exe"),
                _ => MessageBox.Show("Undefined Type")
            };
        }
        catch
        {

        }
    }


    [RelayCommand]
    private void Delete(DeviceInfo_VM deviceInfo)
    {
        if (deviceInfo.Guid is null || deviceInfo.Guid == Guid.Empty)
            return;

        MessageBoxResult result = MessageBox.Show($"确认删除 {deviceInfo.DeviceModel}-{deviceInfo.DeviceName} ?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Question);
        if (result != MessageBoxResult.Yes)
            return;

        this._DeviceCollection.DeviceList.Remove(deviceInfo.Guid.Value);
        MessageBox.Show($"{deviceInfo.DeviceModel}-{deviceInfo.DeviceName} 已删除");
    }

    [RelayCommand]
    private void Setting(DeviceInfo_VM deviceInfo)
    {
        IDialogParameters para = new DialogParameters() { { "Device", deviceInfo } };

        this._DialogService.Show("EditDevice", para, null);
    }
}
