using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using MegaRobo.Authentication;
using MegaRobo.Contract;
using MegaRobo.Entities;
using MegaRobo.Logger;
using MegaRobo.C00225155.Entities.Enums;
using MegaRobo.WpfComponents.Abstractions;
using Telerik.Windows.Controls;
using static Vanara.PInvoke.Kernel32;
using DeviceType = MegaRobo.PipetteTool.HamiltonConsole.Helpers.DeviceType;
using MegaRobo.C00225155App.MenuViews;
using MegaRobo.WpfComponents.MegaViews;
using Common;

namespace MegaRobo.C00225155App
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : RadWindow
    {
        public ICoreService CoreService { get; init; }
        private LoggerService _loggerService;
        private AuthService _authService;
        private ILogger Logger => this.CoreService.Logger;
        private MainWindowExtensions _mainWindowExtensions;

        public MainWindow()
        {
            this.InitializeComponent();
        }

        private async void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            this._mainWindowExtensions = new MainWindowExtensions(this.CoreService, this);
            this._mainWindowExtensions.EnableShowView(this.viewContainer, this.CreateView);

            this._authService = this.CoreService.GetServiceInstance<AuthService>();

            this._loggerService = this.CoreService.GetServiceInstance<LoggerService>();
            this._loggerService.BroadcastEvent += this.LoggerServiceOnBroadcastEvent;


            //this.MegaLogger.LoggerService = this._loggerService; //赋值后,设置LoggerService可以同步UI等级更改

            await Task.Delay(500);
            this.naviMenu.SelectedIndex = 0;
        }

        private void LoggerServiceOnBroadcastEvent(object sender, LogEntity entity)
        {
            //this.Dispatcher.BeginInvoke(DispatcherPriority.Background, () => { this.MegaLogger.Show(entity); });
        }

        private Control CreateView(ShowViewMessage msg)
        {
            var vn = Enum.Parse<ViewNames>(msg.ViewName);
            if (msg.ViewType == ViewType.View)
            {
                Control control = null;
                switch (vn)
                {
                    case ViewNames.HomeView:
                        {
                            control = new HomeView();
                            break;
                        }
                    case ViewNames.SettingView:
                        {
                            var view = new SettingView();
                            view.Initialize(this.CoreService);
                            control = view;
                            break;
                        }
                    case ViewNames.CommunicationDebugView:
                        {
                            control = new HardwareDevicesDebugView();
                            break;
                        }
                    case ViewNames.UserView:
                        {
                            var view = new Authentication.UserView();
                            view.Initialize(this.CoreService);
                            control = view;
                            break;
                        }
                    case ViewNames.FlowConfigView:
                        {
                            control = new FlowConfigView();
                            break;
                        }
                    case ViewNames.AdditivePipetteConfigView:
                        {
                            control = new AdditivePipetteConfigView();
                            break;
                        }
                }
                return control;
            }


            RadWindow radWindow = null;
            switch (vn)
            {
                //case ViewNames.ToolsWindow:
                //      radWindow = new ToolsWindow();
                //        break;
                case ViewNames.Pipette1mlWindow:
                    radWindow = new PipetteTool.HamiltonConsole.MainWindow(this.CoreService, (DeviceType)MegaRobo.C00225155.Entities.Enums.DeviceType.Type1ML);
                    break;
                case ViewNames.Pipette5mlWindow:
                    //radWindow = new PipetteTool.HamiltonConsole.MainWindow(this.CoreService, (DeviceType)C00225155.Entities.Enums.DeviceType.Type1ML);
                    radWindow = new PipetteTool.HamiltonConsole.Main5mlWindow(this.CoreService, (DeviceType)MegaRobo.C00225155.Entities.Enums.DeviceType.Type5ML);
                    break;
                    //case ViewNames.TipMessageDialog:
                    //    radWindow = new TipMessageDialog();
                    //    break;
                    //case ViewNames.MetalBathSettingView:
                    //    radWindow = new MetalBathSettingView();
                    //    break;
            }
            return radWindow;

        }

        private void RadButtonMix_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }
        private void RadButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

    }
}