namespace EEMSUIClient.ViewModels;

public partial class MainWindowViewModel
    (ClientService clientService,
    RunningData running,
    AddressInfo address,
    ConfigService configService,
    LocalFilePathInfo localFilePathInfo,
    IClientBaseProvider clientBaseProvider) : ObservableObject
{

    private Dictionary<string, object> _realtimeData = [];

    [ObservableProperty]
    private TempTestData _TempTest = new();

    [ObservableProperty]
    private LocalFilePathInfo _localFilePathInfo = localFilePathInfo;

    [ObservableProperty]
    private RunningData _Running = running;

    [ObservableProperty]
    private AddressInfo _Address = address;

    [ObservableProperty]
    [NotifyPropertyChangedFor(nameof(IsNotConnected))]
    private bool _isConnected;
    public bool IsNotConnected => !IsConnected;


    [RelayCommand]
    private void SelectRecipe()
    {
        OpenFolderDialog dialog = new();

        if (dialog.ShowDialog() != true)
            return;

        this.LocalFilePathInfo.RecipePath = dialog.FolderName;
        clientBaseProvider.RecipePath = dialog.FolderName;
        if (!configService.SaveDirectoryInfo())
            MessageBox.Show("路径保存失败！");
    }

    [RelayCommand]
    private void SelectConfig()
    {
        OpenFolderDialog dialog = new();
        if (dialog.ShowDialog() != true)
            return;
        this.LocalFilePathInfo.ConfigPath = dialog.FolderName;
        clientBaseProvider.ConfigPath = dialog.FolderName;
        if (!configService.SaveDirectoryInfo())
            MessageBox.Show("路径保存失败！");
    }

    [RelayCommand]
    private void Connect()
    {
        if (!clientService.Initialize())
        {
            MessageBox.Show("初始化失败！");
            return;
        }

        IsConnected = true;

        if (!configService.SaveConnectionInfo())
            MessageBox.Show("设备保存失败！");
    }

    [RelayCommand]
    private void Disconnect()
    {
        clientService?.Dispose();
        IsConnected = false;
    }

    [RelayCommand]
    private void Register()
    {
        if (string.IsNullOrWhiteSpace(Running.DeviceInfo.DeviceSubModel) ||
            string.IsNullOrWhiteSpace(Running.DeviceInfo.DeviceName) ||
            string.IsNullOrWhiteSpace(Running.DeviceInfo.Position) ||
            string.IsNullOrWhiteSpace(Running.DeviceInfo.SoftwareVersion) ||
            string.IsNullOrWhiteSpace(Running.DeviceInfo.DBConnectionString))
        {
            MessageBox.Show("存在未被填写的项目！");
            return;
        }

        Guid guid = clientService.RegisterDevice(Running.DeviceInfo.Adapt<DeviceInfo>());
        if (guid == Guid.Empty)
        {
            MessageBox.Show("未能获取Guid，设备注册失败！");
            return;
        }

        Running.DeviceInfo.Guid = guid;
        MessageBox.Show("设备注册成功。");

        if (!configService.SaveDeviceInfo())
            MessageBox.Show("设备保存失败！");
    }

    [RelayCommand]
    private void Trigger()
    {
        _realtimeData.Clear();
        _realtimeData.Add("PJobID", TempTest.PJobID);
        _realtimeData.Add("CJboID", TempTest.CJobID);
        _realtimeData.Add("RecipeType", TempTest.RecipeType.ToString());
        _realtimeData.Add("Status", TempTest.DeviceStatus.ToString());

        try
        {
            clientService.UpdateRealTimeData(_realtimeData);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    [RelayCommand]
    private void StartDataService()
    {
        clientService.StartDataCollectionService();
    }
}


public partial class TempTestData : ObservableObject
{
    [ObservableProperty]
    private DeviceStatus _DeviceStatus;

    [ObservableProperty]
    private string _PJobID = string.Empty;

    [ObservableProperty]
    private string _CJobID = string.Empty;

    [ObservableProperty]
    private RecipeType _RecipeType;
}