
using Common;
using MegaRobo.C00225155App.DevicesViewModels;
using MegaRobo.C00225155App.MenuViewModels;
using MegaRobo.Contract;
using MegaRobo.Logger;
using MegaRobo.WpfInfrastructure.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MegaRobo.C00225155App
{
    public class ViewModelLocator
    {
        private static ViewModelLocator _instance;
        public static ViewModelLocator Instance => _instance ??= new ViewModelLocator();

        public static ICoreService CoreService { get; internal set; }

        #region 主要界面
        public MainWindowViewModel MainWindowVM => this.GetViewModelInstance<MainWindowViewModel>();
        public HomeViewModel HomeViewModel => this.GetViewModelInstance<HomeViewModel>();
        public HardwareDevicesDebugViewModel hardwareDevicesDebugViewModel => this.GetViewModelInstance<HardwareDevicesDebugViewModel>();

        public FlowConfigViewModel FlowConfigVM => this.GetViewModelInstance<FlowConfigViewModel>();

        #endregion

        #region 硬件设备Device定位
        public LabXToolViewModel LabXToolVM => this.GetViewModelInstance<LabXToolViewModel>();
        
        public HighTemperatureShakeViewModel HighTemperatureShakeVM => this.GetViewModelInstance<HighTemperatureShakeViewModel>();
        public LowTemperatureMagneticStirViewModel LowTemperatureMagneticStirVM => this.GetViewModelInstance<LowTemperatureMagneticStirViewModel>();

        public Weight_TS02_ViewModel Weight_TS02_VM => this.GetViewModelInstance<Weight_TS02_ViewModel>();
        #endregion

        #region 其他
        public EditSampleBottlePropertiesViewModel EditSampleBottlePropertiesVM => this.GetViewModelInstance<EditSampleBottlePropertiesViewModel>();

        public DataResultViewModel DoseDataResultVM=>this.GetViewModelInstance<DataResultViewModel>();

        public DeviceStatusViewModel DeviceStatusVM => this.GetViewModelInstance<DeviceStatusViewModel>();
        public DeviceStatusViewModel_Detect DeviceStatusVM_Detect => this.GetViewModelInstance<DeviceStatusViewModel_Detect>();

        public AdditivePipetteConfigViewModel AdditivePipetteConfigVM => this.GetViewModelInstance<AdditivePipetteConfigViewModel>();
        public SuckFluidViewModel SuckFluidVM => this.GetViewModelInstance<SuckFluidViewModel>();
        #endregion

        private T GetViewModelInstance<T>(string title = "", string serviceName = null) where T : ViewModelBaseEx
        {
            T instance = null;
            try
            {
                string typeName = serviceName ?? typeof(T).FullName;
                if (CoreService.ServiceContainer.TryGetInstance(typeof(T), typeName) is T vm)
                    return vm;
                CoreService.Logger.LogDebug($"正在往IOC中注册{serviceName ?? typeof(T).Name}", LogEvent.Create);
                instance = Activator.CreateInstance<T>();
                instance.Initialize(CoreService, title);
                CoreService.ServiceContainer.RegisterInstance(instance, typeName);
                CoreService.Logger.LogDebug($"往IOC中注册{serviceName ?? typeof(T).Name}完成", LogEvent.Create);
            }
            catch (Exception ex)
            {
                CoreService.Logger.LogException(ex, $"往IOC中注册{serviceName ?? typeof(T).Name}失败");
            }
            return instance;
        }

    }
}
