using System.Windows;
using ConeCalorimeter.Services;
using ConeCalorimeter.ViewModels;

namespace ConeCalorimeter
{
    public partial class MainWindow : Window
    {
        private readonly ITcpDeviceConnectionService _tcpDeviceConnectionService;
        private readonly IScaleService _scaleService;

        public MainWindow()
        {
            InitializeComponent();

            _tcpDeviceConnectionService = new TcpDeviceConnectionService(
                TcpDeviceConnectionOptions.FromEnvironment());
            _scaleService = new ModbusRtuScaleService(
                SerialScaleOptions.FromEnvironment());
            _ = _tcpDeviceConnectionService.StartAsync();

            var experimentDataService = new ExperimentDataService(
                new ModbusRealtimeDataService(_tcpDeviceConnectionService, _scaleService));
            DataContext = new MainViewModel(
                experimentDataService,
                _tcpDeviceConnectionService,
                new NpoiReportExportService(),
                new NpoiRealtimeDataExportService(),
                new HelpDialogService());
        }

        protected override async void OnClosed(EventArgs e)
        {
            _scaleService.Dispose();
            await _tcpDeviceConnectionService.DisposeAsync();
            base.OnClosed(e);
        }
    }
}
