using BOB.Converters;
using BOB.Models;
using BOB.Singleton;
using BOB.ViewModels;
using BOB.ViewModels.Dialogs;
using BOB.Views;
using BOB.Views.Dialogs;
using Castle.DynamicProxy;
using Model.SQLModel;
using ORM;
using Service.Implement;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace BOB
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<ShellView>();
        }
        protected override void OnStartup(StartupEventArgs e)
        {
            if (e.Args.Length > 0)
            {
                string deviceName = e.Args[0];
                SystemConfig.Instance.Title = deviceName;
                InitDataBase(deviceName);
                //Debugger.Launch();
            }
            else
            {
                SystemConfig.Instance.Title = "设备2";//模拟打开的设备
                InitDataBase("设备2");
            }
            base.OnStartup(e);
        }

        private void InitDataBase(string deviceName)
        {
            int tenantId = 0;
            string dbFileName; 
            switch (deviceName)
            {
                case "设备1":
                    tenantId = 10001;
                    dbFileName = "设备1.db";
                    break;
                case "设备2":
                    tenantId = 10002;
                    dbFileName = "设备2.db";
                    break;
                case "设备3":
                    tenantId = 10003;
                    dbFileName = "设备3.db";
                    break;
                case "水冷机和环境箱":
                    tenantId = 10004;
                    dbFileName = "水冷机和环境箱.db";
                    break;
                default:
                    throw new Exception("未知设备");
            }

            // 设置租户 ID，初始化雪花 ID WorkId/DatacenterId
            DatabaseConfig.SetTenant(tenantId);

            // 设置数据库路径
            string baseDir = AppDomain.CurrentDomain.BaseDirectory;
            string folder = Path.Combine(baseDir, "SQLDB");
            if (!Directory.Exists(folder))
                Directory.CreateDirectory(folder);
            string dbPath = Path.Combine(folder, dbFileName);
            DatabaseConfig.SetDbConnection($"Data Source={dbPath}");

            // 创建数据库文件并检查连接
            DatabaseConfig.CreateDatabaseAndCheckConnection(createDatabase: true, checkConnection: true);
            SqlSugarContext.DbContext.CodeFirst.InitTables(typeof(BackFeedData), typeof(EAELD9080Data), typeof(IT6724CData),typeof(PSB1100Data));
        }


        protected override void OnInitialized()
        {
            base.OnInitialized();
            var regionManager = Container.Resolve<IRegionManager>();
            regionManager.RequestNavigate("ShellViewManager", "MainView");
        }
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            //注册视图
            containerRegistry.RegisterForNavigation<MainView>("MainView");
            containerRegistry.RegisterForNavigation<MonitorView>("MonitorView");
            containerRegistry.RegisterForNavigation<Views.DataView>("DataView");
            containerRegistry.RegisterForNavigation<UpdateInfoView>("UpdateInfoView");
            //注册弹窗
            containerRegistry.RegisterDialog<MessageBoxView, MessageBoxViewModel>("MessageBox");
            containerRegistry.RegisterDialog<ParameterSetting, ParameterSettingViewModel>("ParameterSetting");
            containerRegistry.RegisterDialog<BackfeedView, BackfeedViewModel>("Backfeed");
            containerRegistry.RegisterDialog<EAEL9080View, EAEL9080ViewModel>("EAEL9080");
            containerRegistry.RegisterDialog<IOBoardView, IOBoardViewModel>("IOBoard");
            containerRegistry.RegisterDialog<IT6724CView, IT6724CViewModel>("IT6724C");
            containerRegistry.RegisterDialog<LQ7500_DView, LQ7500_DViewModel>("LQ7500_D");
            containerRegistry.RegisterDialog<PSB11000View, PSB11000ViewModel>("PSB11000");
            containerRegistry.RegisterDialog<SQ0030G1DView, SQ0030G1DViewModel>("SQ0030G1D");
            containerRegistry.RegisterDialog<WS_68030_380TView, WS_68030_380TViewModel>("WS_68030_380T");
            containerRegistry.RegisterDialog<ZXKSView, ZXKSViewModel>("ZXKS");
            containerRegistry.RegisterDialog<CANView, CANViewModel>("CAN");
            //注册全局单例变量
            containerRegistry.RegisterSingleton<GlobalVariables>();
            containerRegistry.RegisterSingleton<Devices>();
            containerRegistry.RegisterSingleton<StepRunning>();
            // 注册仓储
            containerRegistry.RegisterScoped(typeof(SqlSugarRepository<>));
            // 注册服务
            containerRegistry.RegisterScoped(typeof(BackFeedService));
            containerRegistry.RegisterScoped(typeof(EAEL9080Service));
            containerRegistry.RegisterScoped(typeof(IT6724CService));
            containerRegistry.RegisterScoped(typeof(LQ7500DService));
            containerRegistry.RegisterScoped(typeof(PSB11000Service));
            containerRegistry.RegisterScoped(typeof(SQ0030Service));
            containerRegistry.RegisterScoped(typeof(WS680Service));
            containerRegistry.RegisterScoped(typeof(ZXKXService));
        }
        protected override void OnExit(ExitEventArgs e)
        {
            var devices = Container.Resolve<Devices>();
            devices.Dispose();
            base.OnExit(e);
        }
    }

}
